QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#791623 | #7991. 最小环 | yhddd | TL | 126ms | 80640kb | C++14 | 2.2kb | 2024-11-28 19:54:05 | 2024-11-28 19:54:10 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define mod 998244353ll
#define pii pair<int,int>
#define fi first
#define se second
#define mems(x,y) memset(x,y,sizeof(x))
#define pb push_back
#define db double
using namespace std;
const int maxn=300010;
const int inf=1e18;
inline int read(){
int x=0,f=1;
char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch-48);ch=getchar();}
return x*f;
}
bool Mbe;
int n,m,ans=inf;
set<pii> e[maxn],g[maxn];
queue<int> q;
int dis[maxn];
bool vis[maxn];
priority_queue<pii> qq;
vector<pii> fr[maxn],bk[maxn];
vector<int> p;
void work(){
n=read();m=read();
for(int i=1;i<=m;i++){
int u=read(),v=read(),w=read();
e[u].insert({v,w}),g[v].insert({u,w});
if(u==v)ans=min(ans,w);
}
for(int u=1;u<=n;u++)if(!e[u].size()||!g[u].size())q.push(u);
while(!q.empty()){
int u=q.front();q.pop();
if(e[u].size()){
for(auto[v,w]:e[u]){
g[v].erase({u,w});
if(!e[v].size()||!g[v].size())q.push(v);
}
e[u].clear();
}
else{
for(auto[v,w]:g[u]){
e[v].erase({u,w});
if(!e[v].size()||!g[v].size())q.push(v);
}
g[u].clear();
}
}
for(int u=1;u<=n;u++)if(e[u].size()==1&&g[u].size()==1){
auto[v,w1]=*e[u].begin();
auto[w,w2]=*g[u].begin();
g[v].erase({u,w1}),e[w].erase({u,w2});
g[v].insert({w,w1+w2}),e[w].insert({v,w1+w2});
}
for(int u=1;u<=n;u++)if(e[u].size()&&g[u].size()){
p.pb(u);
for(auto[v,w]:e[u])bk[u].pb({v,w});
for(auto[v,w]:g[u])fr[u].pb({v,w});
}
for(int s:p){
for(int i:p)dis[i]=inf,vis[i]=0;dis[s]=0;qq.push({0,s});
while(!qq.empty()){
int u=qq.top().se;qq.pop();
if(vis[u])continue;vis[u]=1;
for(auto[v,w]:bk[u]){
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w,qq.push({-dis[v],v});
}
}
}
for(auto[v,w]:fr[s])ans=min(ans,dis[v]+w);
}
if(ans==inf)puts("-1");
else printf("%lld\n",ans);
}
// \
444
bool Med;
int T;
signed main(){
// freopen(".in","r",stdin);
// freopen(".out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);cout.tie(0);
// cerr<<(&Mbe-&Med)/1048576.0<<" MB\n";
T=1;
while(T--)work();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 48564kb
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: 46376kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 10ms
memory: 48632kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 126ms
memory: 80640kb
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 ...