QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#609132 | #7991. 最小环 | binbin | WA | 78ms | 15700kb | C++14 | 2.0kb | 2024-10-04 10:48:11 | 2024-10-04 10:48:11 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pli pair<ll,int>
#define fi first
#define se second
const int maxn=3e5+5;
struct Edge
{
int tot;
int head[maxn];
struct edgenode{int to,nxt;ll w;}edge[maxn*2];
inline void add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].w=z;
edge[tot].nxt=head[x];
head[x]=tot;
}
}G;
int n,m;
int from[maxn],rd[maxn],cd[maxn];
bool vis[maxn],del[maxn];
ll ans=1e18;
ll dis[maxn];
inline void dij(int st)
{
for(int i=1;i<=n;i++) vis[i]=false,dis[i]=1e15;
dis[st]=0;
priority_queue<pli,vector<pli>,greater<pli>>que;
que.push({0,st});
while(!que.empty())
{
int u=que.top().se;que.pop();
if(vis[u]) continue;
vis[u]=true;
for(int i=G.head[u];i;i=G.edge[i].nxt)
{
int v=G.edge[i].to;
if(dis[v]>dis[u]+G.edge[i].w)
{
dis[v]=dis[u]+G.edge[i].w;
que.push({dis[v],v});
}
}
}
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u==v) {ans=min(ans,(ll)w);continue;}
cd[u]++,rd[v]++;
G.add(u,v,w);from[v]=G.tot;
}
for(int i=1;i<=n;i++)
{
if(cd[i]==1&&rd[i]==1)
{
G.edge[from[i]].to=G.edge[G.head[i]].to;
G.edge[from[i]].w+=G.edge[G.head[i]].w;
from[G.edge[G.head[i]].to]=from[i];
del[i]=true;
}
}
for(int i=1;i<=n;i++)
{
if(rd[i]&&cd[i]&&!del[i])
{
dij(i);
for(int u=1;u<=n;u++) if(dis[u]^dis[0]) for(int j=G.head[u];j;j=G.edge[j].nxt)
{
int v=G.edge[j].to;
if(v==i) ans=min(ans,dis[u]+G.edge[j].w);
}
}
}
printf("%lld",ans==1e18?-1:ans);
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 9964kb
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: 1ms
memory: 5864kb
input:
1 0
output:
-1
result:
ok single line: '-1'
Test #3:
score: 0
Accepted
time: 1ms
memory: 6164kb
input:
1 1 1 1 1
output:
1
result:
ok single line: '1'
Test #4:
score: 0
Accepted
time: 48ms
memory: 14088kb
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
Wrong Answer
time: 78ms
memory: 15700kb
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:
101736764788
result:
wrong answer 1st lines differ - expected: '98674714245', found: '101736764788'