标签:vector iss ini tween stream 就会 plm str because
题目网址:http://poj.org/problem?id=3259
题目:
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 52198 | Accepted: 19426 |
Description
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ‘s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
Input
Output
Sample Input
2 3 3 1 1 2 2 1 3 4 2 3 1 3 1 3 3 2 1 1 2 3 2 3 4 3 1 8
Sample Output
NO YES
Hint
1 #include <cstdio> 2 #include <iostream> 3 #include <vector> 4 #include <algorithm> 5 using namespace std; 6 const int inf = 111111111; 7 struct node{ 8 int v,u,w; 9 }; 10 vector<node>v; 11 int n,m,w; 12 int dist[505]; 13 node x; 14 bool relax(int j){//松弛操作 15 if(dist[v[j].u]>dist[v[j].v]+v[j].w){ 16 dist[v[j].u]=dist[v[j].v]+v[j].w; 17 return true; 18 } 19 return false; 20 } 21 bool bellman_ford(){ 22 for (int i=1; i<=n; i++) { 23 dist[i]=inf; 24 } 25 for (int i=0; i<n-1; i++) { 26 int flag=0; 27 for (int j=0; j<v.size(); j++) { 28 if(relax(j)) flag=1; 29 } 30 if(!flag) return false; 31 } 32 for (int j=0; j<v.size(); j++) {//核心 33 if(relax(j)) return true; 34 } 35 return false; 36 } 37 int main(){ 38 int t; 39 cin>>t; 40 while (t--) { 41 int ok=0; 42 v.clear(); 43 cin>>n>>m>>w; 44 for (int i=0; i<m; i++) { 45 cin>>x.v>>x.u>>x.w; 46 v.push_back(x); 47 swap(x.v, x.u); 48 v.push_back(x); 49 } 50 for (int i=0; i<w; i++) { 51 cin>>x.v>>x.u>>x.w; 52 x.w=0-x.w; 53 v.push_back(x); 54 } 55 if (bellman_ford()) printf("YES\n"); 56 else printf("NO\n"); 57 } 58 return 0; 59 }
POJ 3259 Wormholes(Bellman-Ford)
标签:vector iss ini tween stream 就会 plm str because
原文地址:http://www.cnblogs.com/uniles/p/7224903.html