Submission #59443536


Source Code Expand

#![allow(unused)]

fn main() {
    let inp = readv::<usize>();
    let (n, m, q) = (inp[0], inp[1], inp[2]);

    let inf = 10i64.pow(18);
    let mut adj = vec![vec![inf; n]; n];
    for u in 0..n {
        adj[u][u] = 0;
    }
    let mut edges = vec![];
    for _ in 0..m {
        let inp = readv::<i64>();
        let u = inp[0] as usize - 1;
        let v = inp[1] as usize - 1;
        let w = inp[2];
        adj[u][v] = w;
        adj[v][u] = w;
        edges.push((u, v, w));
    }

    let mut asks = vec![];
    for _ in 0..q {
        let ask = readv::<usize>();
        if ask[0] == 1 {
            let (u, v, w) = edges[ask[1] - 1];
            adj[u][v] = inf;
            adj[v][u] = inf;
            asks.push(('r', u, v, w)); // restore
        } else {
            let u = ask[1] - 1;
            let v = ask[2] - 1;
            asks.push(('a', u, v, 0));
        }
    }

    let mut dis = floyd_warshall(&adj, inf);
    let mut ans = vec![];
    for &(cmd, u, v, w) in asks.iter().rev() {
        if cmd == 'r' {
            if w >= dis[u][v] {
                continue;
            }
            dis[u][v] = w;
            dis[v][u] = w;
            for s in 0..n {
                for t in 0..n {
                    if dis[s][u] != inf && dis[v][t] != inf {
                        dis[s][t] = dis[s][t].min(dis[s][u] + dis[u][v] + dis[v][t]);
                    }
                    if dis[s][v] != inf && dis[u][t] != inf {
                        dis[s][t] = dis[s][t].min(dis[s][v] + dis[v][u] + dis[u][t]);
                    }
                }
            }
        } else {
            if dis[u][v] == inf {
                ans.push(-1);
            } else {
                ans.push(dis[u][v]);
            }
        }
    }
    ans.reverse();
    println!("{}", join(&ans, "\n"));
}

fn floyd_warshall(adj: &Vec<Vec<i64>>, inf: i64) -> Vec<Vec<i64>> {
    // dp[k][u][v] = minimum distance from u to v using vertices 0..=k as intermediate
    // dp[-1][u][v] = adj[u][v]
    // dp[k][u][v] = min(dp[k - 1][u][v], dp[k - 1][u][k] + dp[k - 1][k][v]);
    let n = adj.len();
    let mut dp = adj.clone();
    for k in 0..n {
        for u in 0..n {
            for v in 0..n {
                if dp[u][k] != inf && dp[k][v] != inf {
                    dp[u][v] = dp[u][v].min(dp[u][k] + dp[k][v]);
                }
            }
        }
    }
    dp
}

fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

fn readv<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_ascii_whitespace()
        .map(|t| t.parse().ok().unwrap())
        .collect()
}

fn reads() -> Vec<char> {
    read::<String>().chars().collect()
}

fn mapv<T, S, F: Fn(&T) -> S>(arr: &Vec<T>, f: F) -> Vec<S> {
    arr.iter().map(f).collect()
}

fn join<T: ToString>(arr: &[T], sep: &str) -> String {
    arr.iter()
        .map(|x| x.to_string())
        .collect::<Vec<String>>()
        .join(sep)
}

Submission Info

Submission Time
Task F - Road Blocked
User amoshuangyc
Language Rust (rustc 1.70.0)
Score 550
Code Size 3144 Byte
Status AC
Exec Time 188 ms
Memory 25240 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 2
AC × 29
Set Name Test Cases
Sample sample_01.txt, sample_02.txt
All random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt, sample_01.txt, sample_02.txt
Case Name Status Exec Time Memory
random_01.txt AC 179 ms 22304 KiB
random_02.txt AC 181 ms 22292 KiB
random_03.txt AC 183 ms 22380 KiB
random_04.txt AC 187 ms 23232 KiB
random_05.txt AC 75 ms 21492 KiB
random_06.txt AC 121 ms 21744 KiB
random_07.txt AC 172 ms 22260 KiB
random_08.txt AC 183 ms 23148 KiB
random_09.txt AC 174 ms 24100 KiB
random_10.txt AC 151 ms 23892 KiB
random_11.txt AC 96 ms 23900 KiB
random_12.txt AC 75 ms 24472 KiB
random_13.txt AC 50 ms 22056 KiB
random_14.txt AC 66 ms 22708 KiB
random_15.txt AC 51 ms 22860 KiB
random_16.txt AC 47 ms 23300 KiB
random_17.txt AC 181 ms 23908 KiB
random_18.txt AC 181 ms 22308 KiB
random_19.txt AC 182 ms 23976 KiB
random_20.txt AC 179 ms 22140 KiB
random_21.txt AC 182 ms 23972 KiB
random_22.txt AC 180 ms 22356 KiB
random_23.txt AC 146 ms 22584 KiB
random_24.txt AC 139 ms 22492 KiB
random_25.txt AC 188 ms 25240 KiB
random_26.txt AC 93 ms 22556 KiB
random_27.txt AC 1 ms 2000 KiB
sample_01.txt AC 0 ms 1896 KiB
sample_02.txt AC 1 ms 2076 KiB