QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#756645 | #7991. 最小环 | ucup-team4352 | TL | 95ms | 35992kb | C++23 | 2.4kb | 2024-11-16 21:23:28 | 2024-11-16 21:23:29 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pii pair<int,ll>
#define node pair<ll,int>
using namespace std;
const int maxn=3e5+5,maxf=3e3+5;
vector<pii>e0[maxn],e1[maxn];
vector<pii>e[maxn];
int p[maxn];
// array<array<ll,maxf>,maxf>dis;
array<ll,maxn>dis;
vector<int>pos;
ll ans=1e18;
void dij(int x){
priority_queue<node,vector<node>,greater<node>>q;
q.push({0,x});
for(auto u:pos)dis[u]=1e18;
dis[x]=0;
while(!q.empty()){
int now=q.top().second;
ll d=q.top().first;
q.pop();
if(dis[now]<d)continue;
for(auto u:e[now]){
if(dis[u.first]>u.second+d){
dis[u.first]=u.second+d;
q.push({dis[u.first],u.first});
}
if(u.first==x)ans=min(ans,u.second+d);
}
}
// ans=min(ans,dis[x]);
}
int vis[maxn],inst[maxn];
ll dist[maxn];
void dfs(int x){
if(vis[x])return;
inst[x]=1;
vis[x]=1;
for(auto u:e0[x]){
if(inst[u.first]){
ans=min(ans,dist[x]-dist[u.first]+u.second);
continue;
}
if(!vis[u.first]){
dist[u.first]=u.second+dist[x];
dfs(u.first);
}
}
inst[x]=0;
}
int deg0[maxn],deg1[maxn];
void solve(){
int n,m;
cin>>n>>m;
for(int i=1;i<=m;i++){
int u,v,w;
cin>>u>>v>>w;
if(u==v){
ans=min(ans,1ll*w);
continue;
}
e0[u].push_back({v,w});
e1[v].push_back({u,w});
deg0[u]++;
deg1[v]++;
}
queue<int>q;
for(int i=1;i<=n;i++){
// dfs(i);
if((deg0[i]==0||deg1[i]==0||deg0[i]+deg1[i]==1)&&!p[i]){
p[i]=1;
q.push(i);
}
}
while(!q.empty()){
int now=q.front();q.pop();
for(auto u:e0[now]){
int i=u.first;
deg1[i]--;
if((deg0[i]==0||deg1[i]==0||deg0[i]+deg1[i]==1)&&!p[i]){
p[i]=1;
q.push(i);
}
}
for(auto u:e1[now]){
int i=u.first;
deg0[i]--;
if((deg0[i]==0||deg1[i]==0||deg0[i]+deg1[i]==1)&&!p[i]){
p[i]=1;
q.push(i);
}
}
}
for(int i=1;i<=n;i++){
if(!p[i]){
for(auto u:e0[i]){
int flag=0,t=u.first;
ll d=u.second;
while(p[t]){
if(e0[t].empty()){flag=1;break;}
d+=e0[t][0].second;
t=e0[t][0].first;
}
if(flag)continue;
e[i].push_back({t,d});
}
pos.push_back(i);
}
}
for(int i=1;i<=n;i++){
if(!p[i]){
dij(i);
}
}
if(ans==1e18)cout<<"-1\n";
else cout<<ans<<"\n";
}
int main(){
ios::sync_with_stdio(0),cin.tie(0);
int t=1;
// cin>>t;
while(t--)solve();
return 0;
}
/*
n*3/2<=n+1500
m>=n*3/2
n<=3000
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 5688kb
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: 3564kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 1ms
memory: 3600kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 95ms
memory: 35992kb
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: -100
Time Limit Exceeded
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 ...