QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#353055 | #7991. 最小环 | rzh123 | AC ✓ | 303ms | 71680kb | C++14 | 2.8kb | 2024-03-13 20:20:18 | 2024-03-13 20:20:19 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
constexpr int N=3e5+1505;
constexpr long long LINF{0x3f3f3f3f3f3f3f3fll};
int n,m;
long long ans{LINF};
struct W{long long a,b;}; // 正走、反走的贡献
inline W operator+(W a,W b){
auto t=W{a.a+b.a,a.b+b.b};
t.a=min(t.a,LINF),t.b=min(t.b,LINF);
return t;
}
map<int,W> g[N];
bool del[N];
int id[N],idc,idp[N];
long long dis[N];
vector<pair<int,ll> > gnew[N],grev[N];
inline void adde(int u,int v,W w){
auto p=g[u].insert(make_pair(v,w));
if(!p.second){
auto it=p.first;
it->second.a=min(it->second.a,w.a),
it->second.b=min(it->second.b,w.b);
}
}
void dij(int s){
using Q=pair<long long,int>;
static bool vst[N];
for(int i{1};i<=idc;++i){
dis[idp[i]]=LINF;
vst[idp[i]]=false;
}
priority_queue<Q,vector<Q>,greater<Q>> q;
q.emplace(dis[s]=0ll,s);
while(!q.empty()){
int u{q.top().second}; q.pop();
if(vst[u]) continue; else vst[u]=true;
for(auto e:gnew[u]){
int v{e.first};long long w{e.second};
if(dis[u]+w<dis[v]) q.emplace(dis[v]=dis[u]+w,v);
}
}
}
inline void bf(){
for(int i{1};i<=idc;++i){
int u{idp[i]};
dij(u);
for(auto e:grev[u]){
int v{e.first}; ll w{e.second};
ans=min(ans,w+dis[v]);
}
}
}
void simplify(){
static bool inq[N];
queue<int> q;
for(int i{1};i<=n;++i)
if(g[i].size()<=2u) q.emplace(i);
while(!q.empty()){
int u{q.front()}; q.pop(); inq[u]=false;
if(del[u]) continue;
del[u]=true;
if(g[u].size()==1u){
auto e=*g[u].begin();
int v{e.first};W w{e.second};
ans=min(ans,w.a+w.b);
g[v].erase(u);
if(!inq[v]&&g[v].size()<=2u) q.emplace(v),inq[v]=true;
}
else if(g[u].size()==2u){
auto e1=*g[u].begin(),e2=*prev(g[u].end());
int v1{e1.first},v2{e2.first};
W w1{e1.second},w2{e2.second};
swap(w1.a,w1.b);
del[u]=true;
g[v1].erase(u),g[v2].erase(u);
ans=min(ans,w1.a+w1.b);
ans=min(ans,w2.a+w2.b);
W w12{w1+w2},w21{w2+w1};
swap(w21.a,w21.b);
adde(v1,v2,w12),adde(v2,v1,w21);
if(!inq[v1]&&g[v1].size()<=2u) q.emplace(v1);
if(!inq[v2]&&g[v2].size()<=2u) q.emplace(v2);
}
g[u].clear();
}
for(int i{1};i<=n;++i)
if(!del[i])
idp[id[i]=++idc]=i;
int ec{0};
fprintf(stderr,"idc=%d\n",idc);
for(int i{1};i<=idc;++i){
int u{idp[i]};
for(auto e:g[u]){
int v{e.first}; W w{e.second};
if(w.a<LINF) gnew[u].emplace_back(v,w.a),++ec;
if(w.b<LINF) grev[u].emplace_back(v,w.b);
}
}
fprintf(stderr,"ec=%d\n",ec);
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>m;
for(int i{1};i<=m;++i){
int u,v;ll w; cin>>u>>v>>w;
if(u==v) ans=min(ans,1ll*w);
else adde(u,v,W{w,LINF}),adde(v,u,W{LINF,w});
}
simplify();
bf();
if(ans==LINF) ans=-1ll;
cout<<ans<<'\n';
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 4ms
memory: 32060kb
input:
4 6 1 2 1 4 3 3 4 1 9 2 4 1 3 1 2 3 2 6
output:
7
result:
ok single line: '7'
Test #2:
score: 0
Accepted
time: 4ms
memory: 32080kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 4ms
memory: 31980kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 161ms
memory: 66200kb
input:
258420 258419 33061 33062 767169384 212916 212917 1741339 229881 229882 896760805 173467 173468 273055172 233189 233190 800433307 10157 10158 126766550 174605 174606 552176083 224030 224031 886617880 229102 229103 783848581 67588 67589 510826095 233648 233649 879695751 214453 214454 867104578 153140...
output:
-1
result:
ok single line: '-1'
Test #5:
score: 0
Accepted
time: 227ms
memory: 64668kb
input:
248016 248896 82688 82755 592665252 202847 203260 294408121 26821 28237 269132335 150967 152178 3125829 246069 247390 29492546 7470 7673 55843492 33975 35414 442802995 28451 28948 193736988 34484 34637 84441058 60168 60309 501991354 79579 79844 26854803 239672 239706 111702667 73548 73658 149840530 ...
output:
98674714245
result:
ok single line: '98674714245'
Test #6:
score: 0
Accepted
time: 218ms
memory: 67648kb
input:
270530 271285 80489 81855 218173724 188930 190845 783975756 29830 30626 22189315 234320 234472 70840355 198096 198272 300313423 224194 226906 105128197 115010 115834 37228105 134788 135583 18647938 257292 257358 98569041 146988 147215 69398857 248752 250002 409565478 62128 63751 839744551 121918 122...
output:
133486910467
result:
ok single line: '133486910467'
Test #7:
score: 0
Accepted
time: 229ms
memory: 61556kb
input:
222087 223141 123107 123811 2984035 216346 217464 675263 139741 141286 892140 77973 78018 453931100 38603 39546 157182459 13105 14616 775862 97035 97704 379136464 86254 88311 84193802 83968 84398 246202498 152486 160164 65619516 73213 73517 1129576 15618 16541 498613468 192241 195576 889879 21363 21...
output:
47599478278
result:
ok single line: '47599478278'
Test #8:
score: 0
Accepted
time: 245ms
memory: 60412kb
input:
212718 214066 104602 105717 148385760 163427 165307 437059346 108663 111803 784753745 15490 15784 789609 77598 80118 53908869 97776 98040 78287597 26994 27717 989577 134781 134919 531908 22362 24185 185680 114422 114890 609661 192852 192861 155477 45695 45800 35773 150695 152662 511678590 101629 102...
output:
36329947627
result:
ok single line: '36329947627'
Test #9:
score: 0
Accepted
time: 175ms
memory: 54356kb
input:
166349 167207 127268 127447 264589535 87716 91194 596943 123233 126065 170996332 16886 20295 35862710 4657 7035 31814455 95412 96577 195164337 17282 19855 200600035 18848 20733 547079078 139859 141952 197062 124361 126887 37905401 30749 32439 248082130 115409 121655 13113841 85640 88061 989595 74722...
output:
30821798636
result:
ok single line: '30821798636'
Test #10:
score: 0
Accepted
time: 303ms
memory: 70248kb
input:
289406 290248 136815 139417 24401 82238 82679 391891 117261 117722 23784755 45898 47276 91613 19042 20538 139326255 90781 91014 33771 173238 174945 166532570 64778 65593 89778641 107363 112432 3864090 260499 261031 165160 167079 167190 807727902 15135 17610 819060894 46707 48909 252893 51782 55878 3...
output:
61125659219
result:
ok single line: '61125659219'
Test #11:
score: 0
Accepted
time: 251ms
memory: 64112kb
input:
246266 246265 67999 29611 208851615 22833 19844 11567655 78556 60887 111689338 95799 91984 129780604 41384 117633 410486433 7780 17854 417509938 16799 26207 657779642 94022 203027 902990247 41361 49284 914930989 188504 211149 506036119 55526 231127 316210314 179380 117042 590986492 198142 119962 623...
output:
-1
result:
ok single line: '-1'
Test #12:
score: 0
Accepted
time: 202ms
memory: 60384kb
input:
211768 213001 149297 26223 476199539 41008 36464 884082193 55217 33464 115782442 68840 35424 855362664 143738 85588 100080439 40110 28162 373005241 129902 66381 475925374 201288 125187 14707055 137096 124072 115900270 133421 107543 256018220 6224 2375 672655723 35440 9684 72896288 59199 35246 339260...
output:
-1
result:
ok single line: '-1'
Test #13:
score: 0
Accepted
time: 132ms
memory: 56996kb
input:
189247 189247 40564 40565 126861810 103097 103098 125735427 23599 23600 699624689 118819 118820 988946397 165617 165618 98692171 77479 77480 379839193 110553 110554 938974540 115537 115538 214424513 142294 142295 245482087 72445 72446 983499445 130871 130872 557270990 91433 91434 224165097 78267 782...
output:
89784394212359
result:
ok single line: '89784394212359'
Test #14:
score: 0
Accepted
time: 265ms
memory: 65948kb
input:
260655 260655 21607 6885 878779927 39652 259514 670669357 9951 27823 587572053 42838 39782 144417668 72417 6356 619184403 185975 89666 645647130 76384 54129 395523971 61846 16086 124527845 9133 38689 207646370 147822 200308 566313558 38830 122385 93281252 7617 13394 325882927 199865 204258 378663267...
output:
458074331261
result:
ok single line: '458074331261'
Test #15:
score: 0
Accepted
time: 189ms
memory: 60764kb
input:
219732 219732 103472 140334 380068165 73933 162518 926018493 47678 47679 878421674 25405 25406 841514595 117852 171915 471111497 90799 63291 746636238 147551 54378 180584784 175618 216790 940522032 28024 28025 357423428 121290 72993 911767018 103269 144817 625471074 139641 150831 542699252 117562 66...
output:
24288743988756
result:
ok single line: '24288743988756'
Test #16:
score: 0
Accepted
time: 160ms
memory: 60236kb
input:
215086 215086 116576 116577 286771136 152525 152526 166264772 65067 65066 570398365 43139 43138 503552627 7182 7181 134876306 101929 101928 523800520 155406 155407 685194522 53401 53400 433386119 126226 126227 494524327 91449 91448 790089379 174539 189885 758546774 106776 106777 215494756 103032 103...
output:
-1
result:
ok single line: '-1'
Test #17:
score: 0
Accepted
time: 301ms
memory: 71680kb
input:
300000 301500 29189 30997 927578079 260545 260669 492392474 213735 226063 455930491 11586 20995 255773088 18461 27079 845799243 64966 67483 49185881 176419 180909 56922852 159940 160137 469740653 248115 251495 835368096 101352 103380 518722935 80650 86065 250840499 70757 76241 374593963 205938 22863...
output:
43941542770116
result:
ok single line: '43941542770116'
Test #18:
score: 0
Accepted
time: 161ms
memory: 57680kb
input:
191944 192760 1 1459 46894095 161913 162176 60297968 188684 189606 493906604 115668 116953 16132875 178539 178550 191199785 110089 111482 400171054 47522 47552 13721714 51832 52187 65768106 157987 158725 153066538 136656 137155 141796503 122387 123388 546674692 146085 146725 950321060 16363 19998 10...
output:
89279240522
result:
ok single line: '89279240522'