#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define int long long
using namespace std;
int n,m,head[1000001],cnt,dis[1000001],times[1000001];
bool b[1000001];
struct node
{
int to,dis,next;
}a[1000001];
void add_edge(int from,int to,int dis)
{
a[++cnt].to=to;
a[cnt].dis=dis;
a[cnt].next=head[from];
head[from]=cnt;
}
bool spfa(int u)
{
b[u]=0;
times[u]++;
if(times[u]>=n)return 0;
for(int i=head[u];i;i=a[i].next)
{
int v=a[i].to;
if(dis[v]>dis[u]+a[i].dis)
{
dis[v]=dis[u]+a[i].dis;
if(!b[v])
{
b[v]=1;
if(!spfa(v))return 0;
}
}
}
return 1;
}
signed main()
{
dis[0]=0;
int x,y,z;
cin>>n>>m;
for(int i=1;i<=m;i++)
{
cin>>x>>y>>z;
if(x==1)add_edge(y,z,0),add_edge(z,y,0);
else if(x==2)
{
add_edge(z,y,-1);
if(z==y)
{
cout<<"-1";
return 0;
}
}
else if(x==3)add_edge(y,z,0);
else if(x==4)
{
add_edge(y,z,-1);
if(z==y)
{
cout<<"-1";
return 0;
}
}
else if(x==5)add_edge(z,y,0);
}
int ans=0;
for(int i=1;i<=n;i++)
add_edge(0,i,0),dis[i]=0x7fffffff;
int mini=0x7fffffff;
if(!spfa(0))
{
cout<<"-1";
return 0;
}
else
{
for(int i=1;i<=n;i++)
{
ans+=dis[i];
mini=min(mini,dis[i]);
}
}
if(mini<=0)cout<<ans+n*(abs(mini)+1);
else cout<<ans;
}
最后的输出方式是不是错误的?
(求一个hack数据)