标签:
题目链接:
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 32768/32768 K (Java/Others)
//#include <bits/stdc++.h> #include <iostream> #include <queue> #include <cmath> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define Riep(n) for(int i=1;i<=n;i++) #define Riop(n) for(int i=0;i<n;i++) #define Rjep(n) for(int j=1;j<=n;j++) #define Rjop(n) for(int j=0;j<n;j++) #define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; const LL mod=1e9+7; const double PI=acos(-1.0); const int inf=0x3f3f3f3f; const int N=2e4+5; int cnt,n,x,y,vis[N],head[N],num[N]; int dis[N]; struct Edge { int to,next,val; }edge[4*N]; void add_edge(int s,int e,int val) { edge[cnt].to=e; edge[cnt].next=head[s]; edge[cnt].val=val; head[s]=cnt++; } queue<int>qu; void spfa() { while(!qu.empty())qu.pop(); mst(vis,0); mst(dis,inf); mst(num,0); qu.push(1); dis[1]=0; vis[1]=1; while(!qu.empty()) { int fr=qu.front(); qu.pop(); num[fr]++; if(num[fr]>n) { printf("-1\n"); return ; } for(int i=head[fr];i!=-1;i=edge[i].next) { int x=edge[i].to; if(dis[x]>dis[fr]+edge[i].val) { dis[x]=dis[fr]+edge[i].val; if(!vis[x]) { qu.push(x); vis[x]=1; } } } vis[fr]=0; } if(dis[n]==inf)printf("-2\n"); else printf("%d\n",dis[n]); } int main() { int t; scanf("%d",&t); while(t--) { cnt=0; mst(head,-1); int u,v,w; scanf("%d%d%d",&n,&x,&y); Riep(x) { scanf("%d%d%d",&u,&v,&w); add_edge(u,v,w); add_edge(v,u,0); } Riep(y) { scanf("%d%d%d",&u,&v,&w); add_edge(v,u,-w); } spfa(); } return 0; }
hdu-3592 World Exhibition(差分约束)
标签:
原文地址:http://www.cnblogs.com/zhangchengc919/p/5477099.html