• POJ 3259 Wormholes


    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

    题目大意:农夫有F个农场,每个农场里有一些路径和虫洞,路径是双向的,虫洞是单向的,经过路径时会消耗t的时间,经过虫洞时时间会倒退x秒,求有没有可能使得农夫能在经过一些路径和虫洞之后看到之前的自己
    思路:看到之前的自己就相当于是求图中有没有负环,对于虫洞我们将之权值修改为负数,在用一次SPFA即可

     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<vector>
     5 #include<queue>
     6 using namespace std;
     7 const int INF = 0x3f3f3f3f;
     8 int n, m, w;
     9 struct node{
    10     int to, cost;
    11     node() {}
    12     node(int a, int b) :to(a), cost(b) {}
    13 };
    14 vector<node>e[505];
    15 int dis[505], vis[505], f[505];
    16 bool SPFA(int s)
    17 {
    18     for (int i = 0; i <= n; i++) {
    19         dis[i] = INF;
    20         f[i] = 0; vis[i] = 0;
    21     }
    22     dis[s] = 0; f[s]++;
    23     vis[s] = 1; queue<int>Q;
    24     Q.push(s);
    25     while (!Q.empty()) {
    26         int t = Q.front(); Q.pop(); vis[t] = 0;
    27         for (int i = 0; i < e[t].size(); i++) {
    28             int tmp = e[t][i].to;
    29             if (dis[tmp] > dis[t] + e[t][i].cost) {
    30                 dis[tmp] = dis[t] + e[t][i].cost;
    31                 if (!vis[tmp]) {
    32                     vis[tmp] = 1;
    33                     Q.push(tmp);
    34                     if (++f[tmp] >= n)return false;
    35                 }
    36             }
    37         }
    38     }
    39     return true;
    40 }
    41 int main()
    42 {
    43     ios::sync_with_stdio(false);
    44     int T;
    45     cin >> T;
    46     while (T--) {
    47         for (int i = 1; i <= n; i++)e[i].clear();
    48         cin >> n >> m >> w;
    49         for (int a, b, c, i = 1; i <= m; i++) {
    50             cin >> a >> b >> c;
    51             e[a].push_back(node(b, c));
    52             e[b].push_back(node(a, c));
    53         }
    54         for (int a, b, c, i = 1; i <= w; i++) {
    55             cin >> a >> b >> c;
    56             e[a].push_back(node(b, -c));
    57         }
    58         if (!SPFA(1))cout << "YES" << endl;
    59         else cout << "NO" << endl;
    60     }
    61     return 0;
    62 }
  • 相关阅读:
    LeetCode #1021. Remove Outermost Parentheses 删除最外层的括号
    使用Maven运行测试提示Module sdk 1.5的解决方法
    Map.Entry使用详解
    c++基础(一)
    Python_正则表达式
    使用PIL生成验证码
    OpenCV_图像平滑
    OpenCV—图像阈值
    OpenCV形状变换
    使用OpenCV读写图片
  • 原文地址:https://www.cnblogs.com/wangrunhu/p/9501007.html
Copyright © 2020-2023  润新知