QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#610976 | #7991. 最小环 | DengDuck | TL | 4ms | 34952kb | C++20 | 1.7kb | 2024-10-04 18:27:53 | 2024-10-04 18:27:54 |
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;
set<Edge>E[N],R[N];
LL D[N],Ans=Inf;
int n,m,Vis[N];
inline void Add(int x,int y,int 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])
{
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);
}
}
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())
for(Edge i:R[x])Q.push(i.To),Del(i.To,x);
if(R[x].empty())
for(Edge i:E[x])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);
}
}
for(int i=1;i<=n;i++)D[i]=Inf;
for(int i=1;i<=n;i++)
{
if(E[i].empty())continue;
Dij(i);
}
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: 4ms
memory: 34160kb
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: 0ms
memory: 32128kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 0ms
memory: 34952kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: -100
Time Limit Exceeded
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...