QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#611194 | #7991. 最小环 | DengDuck | WA | 267ms | 68868kb | C++20 | 1.8kb | 2024-10-04 19:50:40 | 2024-10-04 19:50:41 |
Judging History
answer
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define Edge pair<int,LL>
#define pLL pair<LL,int>
#define To first
#define W second
using namespace std;
const int N=3e5+5;
const LL Inf=1e18;
const int B=1e6;
set<Edge>E[N],R[N];
LL D[N],Ans=Inf;
int n,m,Vis[N],TOT;
inline void Add(int x,int y,LL w){E[x].insert({y,w}),R[y].insert({x,w});}
inline void Del(int x,int y)
{
E[x].erase(E[x].lower_bound({y,0}));
R[y].erase(R[y].lower_bound({x,0}));
}
inline void Dij(int x)
{
priority_queue<pLL,vector<pLL>,greater<pLL> >Q;
Q.push({D[x]=0,x});
vector<int>V;V.pb(x);
while(!Q.empty())
{
int x=Q.top().W;Q.pop();
if(Vis[x])continue;Vis[x]=1;
for(Edge i:E[x])
{
TOT++;
if(D[x]+i.W<D[i.To]&&D[x]+i.W<Ans)
Q.push({D[i.To]=D[x]+i.W,i.To}),V.pb(i.To),TOT+=10;
}
}
for(Edge i:R[x])Ans=min(Ans,D[i.To]+i.W);
for(int i:V)D[i]=Inf,Vis[i]=0;
}
queue<int>Q;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1,x,y,w;i<=m;i++)
{
scanf("%d%d%d",&x,&y,&w);
if(x==y)Ans=min(Ans,w*1ll);
else Add(x,y,w);
}
for(int i=1;i<=n;i++)Q.push(i);
while(!Q.empty())
{
int x=Q.front();Q.pop();
if(E[x].empty())
{
set<Edge>V=R[x];
for(Edge i:V)Q.push(i.To),Del(i.To,x);
}
if(R[x].empty())
{
set<Edge>V=E[x];
for(Edge i:V)Q.push(i.To),Del(x,i.To);
}
if(E[x].size()==1&&R[x].size()==1)
{
Edge A=*R[x].begin(),B=*E[x].begin();
Del(A.To,x),Del(x,B.To);
if(A.To==B.To)
{
Ans=min(Ans,A.W+B.W);
Q.push(A.To);
continue;
}
Add(A.To,B.To,A.W+B.W);
Q.push(A.To),Q.push(B.To);
}
}
vector<int>V;
for(int i=1;i<=n;i++)
{
if(!E[i].empty())V.pb(i),D[i]=Inf;
}
random_shuffle(V.begin(),V.end());
for(int i:V)
{
Dij(i);
if(TOT>B)break;
}
if(Ans==Inf)puts("-1");
else printf("%lld\n",Ans);
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 34856kb
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: 35272kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 3ms
memory: 33684kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 167ms
memory: 67080kb
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: 246ms
memory: 65936kb
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: 267ms
memory: 68868kb
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: 223ms
memory: 62928kb
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: -100
Wrong Answer
time: 210ms
memory: 62424kb
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:
36691635080
result:
wrong answer 1st lines differ - expected: '36329947627', found: '36691635080'