zoukankan      html  css  js  c++  java
  • Wormholes

     
    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 }
  • 相关阅读:
    洛谷 P1313 计算系数
    洛谷 P1088 火星人
    洛谷 P1049 装箱问题
    P5016 龙虎斗
    洛谷P1208
    求lca
    没有上司的舞会
    最短路spfa
    懒羊羊找朋友
    简单的图论问题之单源最短路dijkstra算法
  • 原文地址:https://www.cnblogs.com/sdxk/p/4626178.html
Copyright © 2011-2022 走看看