#include <iostream> #include <cstdio> #include <cstring> #include <cmath> using namespace std; const int maxn=1e3+9,inf=1e6+9; struct { int to,next,w; }e[maxn*20*2]; int head[maxn],lon; int n,ml,md; void edgeini() { memset(head,-1,sizeof(head)); lon=0; } void edgemake(int from,int to,int w) { e[++lon].to=to; e[lon].w=w; e[lon].next=head[from]; head[from]=lon; } int que[1111111],dist[maxn],cnt[maxn]; bool text[maxn],flag[maxn]; int spfa(int ff) { memset(dist,50,sizeof(dist)); memset(text,0,sizeof(text)); memset(cnt,0,sizeof(cnt)); int front=1,end=0; que[++end]=ff; dist[ff]=0; text[ff]=1; while(front<=end) { // cout<<front<<endl; int t=que[front++]; flag[t]=1; // if(dist[t]>inf||dist[t]<-inf) continue; if(cnt[t]>n) { // cout<<t<<endl; return -1; } text[t]=0; for(int k=head[t];k!=-1;k=e[k].next) { int u=e[k].to; if(dist[u]>dist[t]+e[k].w) { dist[u]=dist[t]+e[k].w; if(!text[u]) { text[u]=1; que[++end]=u; cnt[u]++; } } } } return fabs(dist[n]); } int main() { // while(scanf("%d %d %d",&n,&ml,&md)!=EOF) scanf("%d %d %d",&n,&ml,&md); { edgeini(); for(int i=1,from,to,w;i<=ml;i++) { scanf("%d %d %d",&from,&to,&w); if(from>to) swap(from,to); edgemake(from,to,w); edgemake(to,from,0); } for(int i=1,from,to,w;i<=md;i++) { scanf("%d %d %d",&from,&to,&w); if(from>to) swap(from,to); edgemake(to,from,-w); edgemake(to,from,0); // edgemake(from,to,-w); } int ans=0; memset(flag,0,sizeof(flag)); for(int i=1;i<=n;i++) if(!flag[i]) if(spfa(i)==-1) ans=-1; if(ans==-1) printf("-1\n"); else { ans=spfa(1); if(ans>=inf) printf("-2\n"); else printf("%d\n",ans); } } return 0; }
作者:yrleep 发表于2013-9-7 23:34:34 原文链接
阅读:169 评论:0 查看评论