标签:图
#include <cstdio> #include <iostream> #include <algorithm> #include <queue> #include <stack> #include <cstdlib> #include <cmath> #include <set> #include <map> #include <vector> #include <cstring> #define INF 100000000L using namespace std; int n,ml,md; int a[1005][1005]; struct node{ int x,y,w; }; int d[1005]; int main(){ while(cin >> n >> ml >> md){ vector<node> vec; for(int i = 0 ;i < ml;i++){ node a; scanf("%d%d%d",&a.x,&a.y,&a.w); vec.push_back(a); } for(int i = 0;i < md;i++){ node a; scanf("%d%d%d",&a.y,&a.x,&a.w); a.w = -a.w; vec.push_back(a); } for(int i = 1;i <= n;i++){ d[i] = INF; } d[1] = 0; int len = vec.size(); for(int k = 0;k < n;k++){ for(int i = 1;i +1 <= n;i++){ if(d[i+1] < INF) { d[i] = min(d[i],d[i+1]);//如果第i+1个已经有了最大的距离,那么第i个的最大距离就只会到i+1的最大距离 } } for(int i = 0;i < len;i++){ if(d[vec[i].x] < INF){ d[vec[i].y] = min(d[vec[i].x]+vec[i].w,d[vec[i].y]);// } } } int ans = d[n]; if(d[1] < 0){//有负环 ans = -1; } else if(ans == INF) { ans = -2; } cout << ans << endl; } return 0; }
POJ 3169 Layout bellman_ford 最短路
标签:图
原文地址:http://blog.csdn.net/qq_24667639/article/details/45724553