为什么过不了第六个点

P4878 [USACO05DEC] Layout G

不用回复了,已AC ```cpp #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<queue> using namespace std; const int N=100000+10; const int M=100000+10; struct e{ int to,next,cost; }edge[M]; int n,ml,md,hrp,cnt; int d[M],head[M],v[M],num[M]; queue<int> q; void add(int a,int b,int c) { cnt++; edge[cnt].to=b; edge[cnt].cost=c; edge[cnt].next=head[a]; head[a]=cnt; } int spfa(int s) { memset(d,0x3f,sizeof(d)); memset(v,0,sizeof(v)); memset(num,0,sizeof(num)); v[s]=1,d[s]=0; q.push(s); while(q.size()) { int u=q.front(); q.pop(); v[u]=0; for(int i=head[u];i!=-1;i=edge[i].next) { int to=edge[i].to; int cost=edge[i].cost; if(d[to]>d[u]+cost) { d[to]=d[u]+cost; num[to]=num[u]+1; if(num[to]>=n) { cout<<-1; exit(0); } if(v[to]==0) { v[to]=1; q.push(to); } } } } } int main() { scanf("%d%d%d",&n,&ml,&md); memset(head,-1,sizeof(head)); for(int i=1;i<=ml;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(a,b,c); } for(int i=1;i<=md;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); add(b,a,-c); } for(int i=1;i<=n;i++) add(0,i,0); spfa(0); spfa(1); if(d[n]==0x3f3f3f3f) cout<<-2; else cout<<d[n]; return 0; } ```
by 黄汝鹏 @ 2019-11-02 13:29:18


|