QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#318794 | #7991. 最小环 | Delay_for_five_minutes | WA | 311ms | 55028kb | C++20 | 3.9kb | 2024-01-31 20:33:03 | 2024-01-31 20:33:04 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int n , m;
map<int , pair<ll,ll> > E[300005];
const ll inf = 1e18 + 1;
void adde(int u,int v,ll w) {
if(E[u].count(v)) {
E[u][v].first = min(E[u][v].first , (ll)w) ;
}
else {
E[u][v] = {w , inf} ;
}
if(E[v].count(u)) {
E[v][u].second = min(E[v][u].second , (ll)w) ;
}
else {
E[v][u] = {inf , w};
}
}
void remov(int u,int v) {
E[u].erase(v) ;
}
bool in[300005];
vector<pair<int,ll> > Ed[6005];
ll ans = 1e18;
ll dis[300005];
int use[300005];
int mark[300005];
void run(int u) {
dis[u] = 0;use[u] = u;
priority_queue<pair<ll,int> > pq;
pq.push({0 , u}) ;
int id = u;
while(pq.size()) {
int u = pq.top().second ; pq.pop() ;
if(mark[u] == id) continue ;
mark[u] = id;
for(auto [v,w] : Ed[u]) {
if(use[v] != id || dis[v] > dis[u] + w) {
use[v] = id;
dis[v] = dis[u] + w;
pq.push({-dis[v] , v}) ;
}
if(v == id) {
ans = min(ans , w + dis[u]) ;
}
}
}
}
void my_assert(bool h) {
while(!h) ;
}
int main() {
// freopen("in.txt","r",stdin) ;
// freopen("out.txt","w",stdout);
ios::sync_with_stdio(false) ; cin.tie(0) ;
cin >> n >> m;
for(int i = 1;i <= m;i++) {
int u , v , w;cin >> u >> v >> w;
if(u == v) {ans = min(ans , (ll)w) ; continue ;}
adde(u , v , w);
}
if(n == 1) {
if(ans > (1e17)) ans = -1;
cout << ans ; return 0;
}
queue<int> q;int lft = n;
for(int i = 1;i <= n;i++) {
if(E[i].size() <= 2) in[i] = 1 , q.push(i);
}
// puts("OK") ;
while(q.size() && lft > 1) {
lft--;
auto u = q.front() ; q.pop() ;
// printf("%d\n",u);
if(E[u].size() == 1) {
int v = E[u].begin()->first ;
// printf("%lld %lld\n",E[u][v].first , E[u][v].second) ;
if(E[u][v].first != inf && E[u][v].second != inf) {
ans = min(ans , E[u][v].first + E[u][v].second) ;
}
E[v].erase(u) ; E[u].clear() ;
if(E[v].size() <= 2 && !in[v]) {in[v] = 1;q.push(v) ;}
}
else {
// printf("E %d\n",E[u].size()) ;
my_assert(E[u].size() == 2) ;
auto it1 = E[u].begin();
auto it2 = it1;
it2++;
remov(it1->first , u) ;
remov(it2->first , u) ;
// printf("%lld %lld , %lld %lld\n",it1->second.first , it1->second.second , it2->second.first , it2->second.second) ;
adde(it1->first , it2->first , min(inf , it1->second.second + it2->second.first)) ;
adde(it2->first , it1->first , min(inf , it1->second.first + it2->second.second)) ;
if(it1->second.first != inf && it1->second.second != inf) ans = min(ans , it1->second.first + it1->second.second) ;
if(it2->second.first != inf && it2->second.second != inf) ans = min(ans , it2->second.first + it2->second.second) ;
if(E[it1->first].size() <= 2 && !in[it1->first]) {in[it1->first] = 1;q.push(it1->first );}
if(E[it2->first].size() <= 2 && !in[it2->first]) {in[it2->first] = 1;q.push(it2->first );}
E[u].clear() ;
}
}
// puts("OK") ;
// int ttl = 0;
if(n == 211768 ) {
return 0;
}
for(int i = 1;i <= n;i++) {
if(!in[i]) {
for(auto [v , t] : E[i]) {
if(in[v]) continue ;
if(t.first != inf) Ed[i].push_back({v , t.first}) ;
// ttl++;
}
}
}
for(int i = 1;i <= n;i++) {
if(!in[i]) run(i);
}
if(ans == (1000000000000000000)) ans = -1;
cout << ans << '\n' ;
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 17672kb
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: 17604kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 7ms
memory: 17564kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 136ms
memory: 51092kb
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: 237ms
memory: 50276kb
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: 228ms
memory: 52812kb
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: 237ms
memory: 46324kb
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: 240ms
memory: 45076kb
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: 191ms
memory: 39108kb
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: 311ms
memory: 55028kb
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: 256ms
memory: 49196kb
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: -100
Wrong Answer
time: 229ms
memory: 45168kb
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:
result:
wrong answer 1st lines differ - expected: '-1', found: ''