标签:des style blog http color os io java ar
4 2 1 1 3 10 2 4 20 2 3 3
27
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib> 10 #include <string> 11 #include <set> 12 #include <stack> 13 #define LL long long 14 #define pii pair<int,int> 15 #define INF 0x3f3f3f3f 16 using namespace std; 17 const int maxn = 1010; 18 struct arc{ 19 int to,w; 20 arc(int x = 0,int y = 0){ 21 to = x; 22 w = y; 23 } 24 }; 25 vector<arc>g[maxn]; 26 queue<int>q; 27 int d[maxn],cnt[maxn],N,ML,MD; 28 bool used[maxn]; 29 void spfa(){ 30 for(int i = 1; i <= N; i++){ 31 d[i] = INF; 32 cnt[i] = 0; 33 used[i] = false; 34 } 35 d[1] = 0; 36 used[1] = true; 37 q.push(1); 38 while(!q.empty()){ 39 int u = q.front(); 40 q.pop(); 41 used[u] = false; 42 for(int i = 0; i < g[u].size(); i++){ 43 if(d[g[u][i].to] > d[u] + g[u][i].w){ 44 d[g[u][i].to] = d[u] + g[u][i].w; 45 if(!used[g[u][i].to]){ 46 q.push(g[u][i].to); 47 used[g[u][i].to] = true; 48 cnt[g[u][i].to]++; 49 if(cnt[g[u][i].to] > N){ 50 puts("-1"); 51 return; 52 } 53 } 54 } 55 } 56 } 57 if(d[N] == INF) puts("-2"); 58 else printf("%d\n",d[N]); 59 } 60 int main(){ 61 int i,u,v,w; 62 while(~scanf("%d %d %d",&N,&ML,&MD)){ 63 for(i = 0; i <= N; i++) g[i].clear(); 64 for(i = 0; i < ML; i++){ 65 scanf("%d %d %d",&u,&v,&w); 66 g[u].push_back(arc(v,w)); 67 } 68 for(i = 0; i < MD; i++){ 69 scanf("%d %d %d",&u,&v,&w); 70 g[v].push_back(arc(u,-w)); 71 } 72 for(i = 1; i < N; i++) 73 g[i+1].push_back(arc(i,0)); 74 spfa(); 75 } 76 return 0; 77 }
标签:des style blog http color os io java ar
原文地址:http://www.cnblogs.com/crackpotisback/p/3958850.html