QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#273188 | #7883. Takeout Delivering | ucup-team635# | ML | 189ms | 77508kb | Rust | 5.7kb | 2023-12-02 21:55:58 | 2023-12-02 21:55:58 |
Judging History
answer
use std::collections::*;
use std::io::Write;
type Map<K, V> = BTreeMap<K, V>;
type Set<T> = BTreeSet<T>;
type Deque<T> = VecDeque<T>;
fn main() {
input! {
n: usize,
m: usize,
e: [(usize1, usize1, i64); m],
}
let mut e = e;
for e in e.iter_mut() {
*e = (e.0.min(e.1), e.0.max(e.1), e.2);
}
e.sort_by_key(|e| e.2);
let mut g = vec![vec![]; n];
for &(a, b, c) in e.iter() {
g[a].push((a, b, c));
g[b].push((a, b, c));
}
let mut dp = vec![false; n];
let mut ep = vec![false; n];
dp[0] = true;
ep[n - 1] = true;
let mut dsu = DSU::new(n);
let mut tree = vec![vec![]; n];
let inf = std::i64::MAX / 3;
let mut val = inf;
let mut ans = inf;
for (a, b, c) in e {
if (a, b) == (0, n - 1) {
ans.chmin(c);
}
if let Some((p, ch)) = dsu.unite(a, b) {
tree[a].push((b, c));
tree[b].push((a, c));
if dp[p] != dp[ch] || ep[p] != ep[ch] {
for &r in [p, ch].iter() {
if (!dp[r] && dp[r ^ p ^ ch]) || (!ep[r] && ep[r ^ p ^ ch]) {
for &(a, b, w) in g[r].iter() {
let mut p = [dsu.root(0), dsu.root(n - 1)];
let mut q = [dsu.root(a), dsu.root(b)];
p.sort();
q.sort();
if p[0] != p[1] && p == q {
val.chmin(w);
}
}
}
}
}
dp[p] |= dp[ch];
ep[p] |= ep[ch];
let mut x = std::mem::take(&mut g[ch]);
for (a, b, w) in x {
if dsu.same(a, b) {
continue;
}
g[p].push((a, b, w));
}
}
ans.chmin(c + val);
}
println!("{}", ans);
}
// ---------- begin input macro ----------
// reference: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[macro_export]
macro_rules! input {
(source = $s:expr, $($r:tt)*) => {
let mut iter = $s.split_whitespace();
input_inner!{iter, $($r)*}
};
($($r:tt)*) => {
let 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_export]
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_export]
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, bytes) => {
read_value!($iter, String).bytes().collect::<Vec<u8>>()
};
($iter:expr, usize1) => {
read_value!($iter, usize) - 1
};
($iter:expr, $t:ty) => {
$iter.next().unwrap().parse::<$t>().expect("Parse error")
};
}
// ---------- end input macro ----------
//---------- begin union_find ----------
pub struct DSU {
p: Vec<i32>,
}
impl DSU {
pub fn new(n: usize) -> DSU {
assert!(n < std::i32::MAX as usize);
DSU { p: vec![-1; n] }
}
pub fn init(&mut self) {
self.p.iter_mut().for_each(|p| *p = -1);
}
pub fn root(&self, mut x: usize) -> usize {
assert!(x < self.p.len());
while self.p[x] >= 0 {
x = self.p[x] as usize;
}
x
}
pub fn same(&self, x: usize, y: usize) -> bool {
assert!(x < self.p.len() && y < self.p.len());
self.root(x) == self.root(y)
}
pub fn unite(&mut self, x: usize, y: usize) -> Option<(usize, usize)> {
assert!(x < self.p.len() && y < self.p.len());
let mut x = self.root(x);
let mut y = self.root(y);
if x == y {
return None;
}
if self.p[x] > self.p[y] {
std::mem::swap(&mut x, &mut y);
}
self.p[x] += self.p[y];
self.p[y] = x as i32;
Some((x, y))
}
pub fn parent(&self, x: usize) -> Option<usize> {
assert!(x < self.p.len());
let p = self.p[x];
if p >= 0 {
Some(p as usize)
} else {
None
}
}
pub fn sum<F>(&self, mut x: usize, mut f: F) -> usize
where
F: FnMut(usize),
{
while let Some(p) = self.parent(x) {
f(x);
x = p;
}
x
}
pub fn size(&self, x: usize) -> usize {
assert!(x < self.p.len());
let r = self.root(x);
(-self.p[r]) as usize
}
}
//---------- end union_find ----------
// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
fn chmin(&mut self, x: Self) -> bool;
fn chmax(&mut self, x: Self) -> bool;
}
impl<T: PartialOrd> ChangeMinMax for T {
fn chmin(&mut self, x: Self) -> bool {
*self > x && {
*self = x;
true
}
}
fn chmax(&mut self, x: Self) -> bool {
*self < x && {
*self = x;
true
}
}
}
// ---------- end chmin, chmax ----------
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 2224kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: 0
Accepted
time: 159ms
memory: 71052kb
input:
300000 299999 80516 80517 597830404 110190 110191 82173886 218008 218009 954561262 250110 250111 942489774 66540 66541 156425292 34947 34948 239499776 273789 273790 453201232 84428 84429 439418398 98599 98600 326095035 55636 55637 355015760 158611 158612 684292473 43331 43332 43265001 171621 171622 ...
output:
1999991697
result:
ok 1 number(s): "1999991697"
Test #3:
score: 0
Accepted
time: 189ms
memory: 71452kb
input:
300000 299999 207226 231742 414945003 84591 210444 175968953 46327 51582 612565723 18773 141119 82562646 76139 286963 762958587 131867 224820 928900783 215240 216181 405340417 144725 290878 195350550 267216 268752 846015171 31413 255927 389339642 45219 147512 489502910 113391 215402 555706684 53359 ...
output:
1989898633
result:
ok 1 number(s): "1989898633"
Test #4:
score: 0
Accepted
time: 119ms
memory: 77508kb
input:
300000 299999 1 118488 989720257 1 181002 810258689 1 254222 172925351 1 176703 737330574 1 218306 941887568 1 105741 645573853 1 188490 794789787 1 273997 91455946 1 214929 293300204 1 127289 600097406 1 30589 330284120 1 128954 532459734 1 163729 627033607 1 24073 718818252 1 41571 755054850 1 560...
output:
725564985
result:
ok 1 number(s): "725564985"
Test #5:
score: 0
Accepted
time: 152ms
memory: 77056kb
input:
300000 299999 19784 19785 438457172 153297 239456 192671538 153297 202254 645947104 9640 9641 583410541 153297 221084 490232940 32210 32211 838809517 153297 171896 625627853 153297 216946 683286844 110843 110844 690710575 153297 176413 983037656 74908 74909 375017711 67676 67677 695121437 153297 196...
output:
1999992880
result:
ok 1 number(s): "1999992880"
Test #6:
score: 0
Accepted
time: 176ms
memory: 71328kb
input:
300000 299999 130662 244000 579178004 167657 294630 753914681 61924 61925 165841675 113560 113561 979316325 151574 218075 973289843 1224 1225 230803236 133441 133442 806726301 167309 249456 853031541 120245 120246 688705252 142688 217826 692299631 106114 106115 735998084 123268 123269 749737073 1208...
output:
1999989691
result:
ok 1 number(s): "1999989691"
Test #7:
score: 0
Accepted
time: 184ms
memory: 71804kb
input:
300000 299999 54474 108948 971770803 11077 22155 470962125 115359 230718 373029049 124472 248944 865510218 11958 23917 100575860 130349 260698 582886291 17563 35127 697131917 54427 108854 274252758 51546 103092 198620066 100126 200252 154524070 14994 29988 528701437 147502 295004 531813162 42285 845...
output:
1823402145
result:
ok 1 number(s): "1823402145"
Test #8:
score: 0
Accepted
time: 177ms
memory: 72060kb
input:
300000 299999 240182 240189 427579190 41871 41880 360542232 57131 57140 353813367 122650 122653 621913649 67332 67340 271427538 107523 107524 774306311 60374 60379 315636931 130805 130808 667109508 154531 154534 327293148 138561 138569 833879694 32543 32551 382610722 287398 287400 211744079 59693 59...
output:
1999983202
result:
ok 1 number(s): "1999983202"
Test #9:
score: 0
Accepted
time: 0ms
memory: 2032kb
input:
10 20 1 6 98 3 4 3 4 5 26 7 8 72 1 2 86 3 7 83 7 10 89 4 7 100 3 6 59 6 9 74 5 6 28 6 7 1 2 9 100 2 6 99 5 10 85 8 10 97 2 10 98 8 9 3 2 3 94 9 10 71
output:
170
result:
ok 1 number(s): "170"
Test #10:
score: 0
Accepted
time: 0ms
memory: 2020kb
input:
10 20 2 8 94 3 7 81 8 10 85 2 4 88 4 5 90 1 4 96 3 6 95 7 10 77 1 8 98 4 7 91 2 6 38 1 10 89 1 3 45 4 9 94 3 9 64 6 10 83 5 8 19 2 5 81 6 7 92 3 4 92
output:
89
result:
ok 1 number(s): "89"
Test #11:
score: 0
Accepted
time: 0ms
memory: 2068kb
input:
10 20 1 6 58 1 4 80 1 10 28 1 3 46 6 7 76 7 10 75 4 9 95 2 10 92 1 7 15 9 10 52 3 6 93 5 7 94 1 2 75 1 5 41 3 7 79 4 8 94 2 6 80 6 9 63 1 9 42 1 8 9
output:
28
result:
ok 1 number(s): "28"
Test #12:
score: 0
Accepted
time: 0ms
memory: 2096kb
input:
10 20 6 9 79 5 6 72 2 5 74 2 4 74 8 10 90 1 5 82 3 4 55 6 10 94 7 8 55 3 7 92 1 8 83 9 10 93 4 7 98 4 5 24 2 3 28 6 7 24 1 2 63 3 10 99 8 9 2 5 10 95
output:
158
result:
ok 1 number(s): "158"
Test #13:
score: 0
Accepted
time: 0ms
memory: 2184kb
input:
10 20 3 4 75 5 7 98 3 10 91 5 8 41 2 7 84 2 3 73 4 10 100 2 4 80 1 5 99 3 9 79 4 5 97 4 9 10 4 7 52 1 10 96 8 10 98 6 9 99 1 2 50 5 9 98 2 9 93 3 6 92
output:
96
result:
ok 1 number(s): "96"
Test #14:
score: 0
Accepted
time: 0ms
memory: 2180kb
input:
10 20 1 5 84 9 10 98 2 3 56 2 4 5 3 9 95 4 9 94 3 7 82 6 8 68 1 3 10 4 8 64 3 6 46 5 10 58 5 7 92 6 7 96 1 2 38 2 5 74 1 8 70 8 9 100 4 7 93 7 9 98
output:
132
result:
ok 1 number(s): "132"
Test #15:
score: 0
Accepted
time: 0ms
memory: 2112kb
input:
10 20 2 10 100 8 10 100 7 10 85 3 7 73 5 7 100 4 9 100 1 8 7 2 4 26 6 8 27 3 4 100 6 7 100 2 3 99 3 9 66 9 10 13 4 8 75 1 2 8 4 10 100 1 6 19 7 9 99 2 5 49
output:
107
result:
ok 1 number(s): "107"
Test #16:
score: 0
Accepted
time: 0ms
memory: 2076kb
input:
100 200 60 77 957 70 71 589 63 64 915 13 79 1000 8 9 757 41 42 141 21 53 1000 82 99 911 58 59 683 62 63 889 92 93 455 71 72 457 11 92 1000 9 43 1000 94 95 582 57 58 606 54 55 162 27 28 732 99 100 256 42 43 201 13 99 1000 15 73 1000 19 20 646 22 91 1000 22 50 1000 93 94 888 14 15 904 10 47 1000 68 83...
output:
1671
result:
ok 1 number(s): "1671"
Test #17:
score: 0
Accepted
time: 0ms
memory: 2076kb
input:
100 200 21 59 969 4 22 998 47 98 637 8 51 982 48 55 995 14 65 22 3 35 987 40 46 969 6 82 432 13 98 960 24 49 965 5 32 571 20 71 617 71 82 982 30 66 979 76 79 255 7 56 965 18 21 971 53 65 81 63 64 991 6 88 949 1 13 995 31 38 405 40 91 952 47 89 956 8 84 475 29 90 202 6 12 975 30 89 509 47 88 82 4 58 ...
output:
1859
result:
ok 1 number(s): "1859"
Test #18:
score: 0
Accepted
time: 0ms
memory: 2100kb
input:
100 200 19 29 909 22 86 785 1 56 28 38 85 942 1 52 89 1 78 693 4 30 975 54 99 939 1 89 461 1 59 390 1 95 576 1 92 343 1 11 876 1 85 109 29 78 958 74 81 981 10 49 799 1 39 767 41 92 641 1 69 149 1 34 984 13 70 896 32 44 949 1 20 137 1 90 85 2 70 545 4 58 910 33 60 818 89 96 906 66 100 997 1 55 85 1 9...
output:
896
result:
ok 1 number(s): "896"
Test #19:
score: 0
Accepted
time: 0ms
memory: 2344kb
input:
100 200 11 89 997 30 81 985 58 62 260 23 29 995 58 97 909 69 91 986 58 83 597 11 12 858 58 98 213 58 93 465 8 9 638 5 79 996 58 91 659 37 98 990 47 50 993 43 61 986 37 81 997 58 75 102 22 64 998 4 23 990 41 87 1000 73 95 982 30 31 722 13 43 998 17 18 659 42 52 974 32 48 939 53 90 996 69 86 943 4 5 2...
output:
1943
result:
ok 1 number(s): "1943"
Test #20:
score: 0
Accepted
time: 0ms
memory: 2064kb
input:
100 200 14 21 980 1 2 23 61 80 984 51 52 626 1 71 828 31 32 797 60 61 436 17 61 957 35 73 85 30 37 879 48 72 267 53 54 482 7 91 997 37 88 576 26 74 996 50 93 998 8 75 993 30 80 809 26 40 866 23 89 969 60 77 964 44 80 964 28 68 899 17 100 995 47 92 989 57 58 630 35 36 291 36 37 70 18 20 888 12 99 892...
output:
427
result:
ok 1 number(s): "427"
Test #21:
score: 0
Accepted
time: 0ms
memory: 2164kb
input:
100 200 11 12 980 33 66 341 60 88 997 41 83 156 11 22 995 29 75 736 44 73 996 8 68 965 49 62 935 65 81 998 5 10 229 58 74 934 55 88 998 27 90 999 10 21 409 3 74 911 11 84 1000 9 95 988 3 6 393 16 61 956 34 88 1000 34 67 966 21 43 66 12 61 837 14 56 413 12 42 998 26 52 778 6 12 830 63 78 844 6 36 893...
output:
1505
result:
ok 1 number(s): "1505"
Test #22:
score: 0
Accepted
time: 0ms
memory: 2144kb
input:
100 200 83 88 101 56 59 191 64 71 419 7 26 927 48 57 69 9 41 741 43 67 993 21 56 916 20 22 801 35 42 973 78 85 138 26 50 924 87 96 727 19 96 997 9 26 848 21 28 820 39 49 537 47 72 900 55 63 672 18 19 109 9 60 923 8 82 988 8 18 795 9 34 618 23 28 649 67 68 366 10 12 500 23 38 819 10 17 815 69 99 998 ...
output:
1402
result:
ok 1 number(s): "1402"
Test #23:
score: -100
Memory Limit Exceeded
input:
300000 1000000 279851 289752 999954614 161068 161069 686040658 98775 290773 999996999 152639 152640 726594968 83070 83071 812508473 91034 126281 999954538 86789 222983 999990457 95587 176851 999991161 33638 137265 999988809 84658 192641 999991026 153523 295952 999998616 174583 243251 999987129 12518...
output:
1998201565