結果

問題 No.848 なかよし旅行
ユーザー finefine
提出日時 2020-04-25 23:56:59
言語 Rust
(1.77.0 + proconio)
結果
WA  
実行時間 -
コード長 3,289 bytes
コンパイル時間 18,318 ms
コンパイル使用メモリ 377,208 KB
実行使用メモリ 11,648 KB
最終ジャッジ日時 2024-11-08 02:45:15
合計ジャッジ時間 21,838 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 TLE -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: variable does not need to be mutable
   --> src/main.rs:8:13
    |
8   |           let mut s = {
    |               ----^
    |               |
    |               help: remove this `mut`
...
107 | /     input! {
108 | |         n: usize,
109 | |         m: usize,
110 | |         p: usize1,
...   |
113 | |         edges: [(usize1, usize1, Cost); m],
114 | |     }
    | |_____- in this macro invocation
    |
    = note: `#[warn(unused_mut)]` on by default
    = note: this warning originates in the macro `input` (in Nightly builds, run with -Z macro-backtrace for more info)

ソースコード

diff #

// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let mut s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

type Cost = u64;

#[derive(Clone)]
pub struct Edge {
    to: usize,
    cost: Cost,
}
type Graph = Vec<Vec<Edge>>;

use std::cmp::Ordering;

#[derive(PartialEq, Eq, PartialOrd)]
struct State {
    at: usize,
    cost: Cost,
}

impl Ord for State {
    fn cmp(&self, other: &Self) -> Ordering {
        other.cost.cmp(&self.cost)
    }
}

use std::collections::BinaryHeap;

const INF : Cost = std::u64::MAX;

pub fn dijkstra(s: usize, g: &Graph) -> Vec<Cost> {
    let mut minc = vec![INF; g.len()];
    minc[s] = 0;

    let mut pq = BinaryHeap::new();
    pq.push(State { at: s, cost: 0 });

    while !pq.is_empty() {
        let cur = pq.pop().unwrap();

        if minc[cur.at] < cur.cost {
            continue;
        }

        for e in g[cur.at].iter() {
            let cost = cur.cost + e.cost;
            if minc[e.to] <= cost {
                continue;
            }
            
            minc[e.to] = cost;
            pq.push(State { at: e.to, cost });
        }
    }

    minc
}

fn main() {
    input! {
        n: usize,
        m: usize,
        p: usize1,
        q: usize1,
        t: Cost,
        edges: [(usize1, usize1, Cost); m],
    }

    let mut g = vec![vec![]; n];
    for (a, b, c) in edges {
        g[a].push(Edge { to: b, cost: c });
        g[b].push(Edge { to: a, cost: c });
    }

    let minc1 = dijkstra(0, &g);
    let mincp = dijkstra(p, &g);
    let mincq = dijkstra(q, &g);

    let mut ans : i64 = -1;

    let cost = minc1[p] + mincp[q] + mincq[0];
    if cost <= t {
        ans = std::cmp::max(ans, cost as i64);
    }

    let cost = minc1[q] + mincq[p] + mincp[0];
    if cost <= t {
        ans = std::cmp::max(ans, cost as i64);
    }

    for i in 0..n {
        for j in 0..n {
            let cost = minc1[i] + std::cmp::max(mincp[i] + mincp[j], mincq[i] + mincq[j]) + minc1[j];
            if cost > t {
                continue;
            }

            let score = minc1[i] + minc1[j] + t - cost;
            ans = std::cmp::max(ans, score as i64);
        }
    }

    println!("{}", ans);
}
0