码迷,mamicode.com
首页 > 编程语言 > 详细

Wormholes - poj 3259 (Bellman-Ford算法)

时间:2015-07-08 12:49:09      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

 
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 34934   Accepted: 12752

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..NM (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

Line 1: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path. 
Lines M+2..M+W+1 of each farm: Three space-separated numbers (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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

For farm 1, FJ cannot travel back in time. 
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
这题使用Bellman-Ford算法
 1 #include <iostream>
 2 using namespace std;
 3 struct farm {
 4     int S;
 5     int E;
 6     int T;
 7 } f[5500];
 8 int main() {
 9     int num;
10     int N, M, W;
11     cin >> num;
12     int F[500];
13     for (int i = 0; i < num; i++) {
14         cin >> N >> M >> W;
15         for (int j = 0; j < N; j++) {
16             F[j] = 20000;
17         }
18         F[0] = 0;
19         for (int j = 0; j < M; j++) {
20             int a, b, c;
21             cin >> a >> b >> c;
22             f[2*j].S = a;
23             f[2*j].E = b;
24             f[2*j].T = c;
25             f[2*j+1].S = b;
26             f[2*j+1].E = a;
27             f[2*j+1].T = c;
28 
29         }
30         for (int j =2* M; j < 2*M + W; j++) {
31             int a, b, c;
32             cin >> a >> b >> c;
33             f[j].S = a;
34             f[j].E = b;
35             f[j].T = 0 - c;
36         }
37         for (int j = 0; j < N-1; j++) {
38             for (int k = 0; k < 2*M + W; k++) {
39                 if (F[f[k].E] > F[f[k].S] + f[k].T) {
40                     F[f[k].E] = F[f[k].S] + f[k].T;
41                 }
42             }
43         }
44         int flag = 0;
45         for (int k = 0; k < 2*M + W; k++) {
46             if (F[f[k].E] >F[f[k].S] + f[k].T) {
47                 F[f[k].E] = F[f[k].S] + f[k].T;
48                 flag=1;
49                 break;
50             }
51         }
52         if(flag){
53             cout<<"YES"<<endl;
54         }else{
55             cout<<"NO"<<endl;
56         }
57     }
58     return 0;
59 }

 

Wormholes - poj 3259 (Bellman-Ford算法)

标签:

原文地址:http://www.cnblogs.com/sdxk/p/4626178.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!