aizu_online_judge/grl/
grl_1_b.rs1use competitive::prelude::*;
2#[doc(no_inline)]
3pub use competitive::{
4 algebra::AdditiveOperation,
5 graph::{DirectedGraphScanner, OptionSp, ShortestPathExt},
6};
7
8#[verify::aizu_online_judge("GRL_1_B")]
9pub fn grl_1_b(reader: impl Read, mut writer: impl Write) {
10 let s = read_all_unchecked(reader);
11 let mut scanner = Scanner::new(&s);
12 scan!(scanner, vs, es, r, (graph, d): @DirectedGraphScanner::<usize, i64>::new(vs, es));
13 let cost =
14 graph.bellman_ford_ss::<OptionSp<AdditiveOperation<_>>, _>(r, &|eid| Some(d[eid]), true);
15 if let Some(cost) = cost {
16 for u in graph.vertices() {
17 match cost[u] {
18 Some(d) => writeln!(writer, "{}", d).ok(),
19 None => writeln!(writer, "INF").ok(),
20 };
21 }
22 } else {
23 writeln!(writer, "NEGATIVE CYCLE").ok();
24 }
25}