标签:
Time Limit: 1000MS
Memory Limit: 65536K | ||
Total Submissions: 10059 | Accepted: 4835 |
Description
Input
Output
Sample Input
4 2 1
1 3 10
2 4 20
2 3 3
Sample Output
27
Hint
Source
1 /**/ 2 #include<iostream> 3 #include<cstdio> 4 #include<cmath> 5 #include<cstring> 6 #include<algorithm> 7 #include<queue> 8 using namespace std; 9 const int INF=0x3f3f3f3f; 10 const int mxn=2000; 11 struct edge{ 12 int v,dis; 13 int next; 14 }e[mxn*10]; 15 int hd[mxn],cnt; 16 int dis[mxn]; 17 int vis[mxn]; 18 int inq[mxn]; 19 int n,ml,md; 20 void add_edge(int u,int v,int w){ 21 e[++cnt].next=hd[u];e[cnt].v=v;e[cnt].dis=w;hd[u]=cnt; 22 } 23 bool SPFA(){ 24 memset(dis,0x3f,sizeof dis); 25 queue<int>q; 26 q.push(1); 27 inq[1]=1; 28 vis[1]=1; 29 dis[1]=0; 30 while(!q.empty()){ 31 int u=q.front(); 32 for(int i=hd[u];i;i=e[i].next){ 33 int v=e[i].v; 34 if(dis[u]+e[i].dis<dis[v]){ 35 dis[v]=dis[u]+e[i].dis; 36 vis[v]++; 37 if(vis[v]>n)return 0; 38 if(!inq[v]){ 39 inq[v]=1; 40 q.push(v); 41 } 42 } 43 } 44 q.pop(); 45 inq[u]=0; 46 } 47 return 1; 48 } 49 int main(){ 50 scanf("%d%d%d",&n,&ml,&md); 51 int i,j; 52 int a,b,c; 53 for(i=1;i<=ml;i++){ 54 scanf("%d%d%d",&a,&b,&c); 55 if(a>b)swap(a,b); 56 add_edge(a,b,c); 57 } 58 for(i=1;i<=md;i++){ 59 scanf("%d%d%d",&a,&b,&c); 60 if(a>b)swap(a,b); 61 add_edge(b,a,-c); 62 } 63 if(!SPFA()){ 64 printf("-1\n");return 0; 65 } 66 if(dis[n]==INF) printf("-2\n"); 67 else printf("%d\n",dis[n]); 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/SilverNebula/p/5737908.html