zoukankan      html  css  js  c++  java
  • Poj3259--Wormholes(Spfa 判负环)

    Wormholes
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 36836   Accepted: 13495

    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

    Line 1: A single integer, F. F farm descriptions follow.
    Line 1 of each farm: Three space-separated integers respectively: N, M, and W
    Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) 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 (S, E, T) 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.

    Source

    RE: 农场分为几块, 有的地方存在虫洞, (有负权值(边权值为时间)的边)。 问能不能回到过去。  Spfa 判 负环。
     1 #include <queue>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <iostream>
     5 using namespace std;
     6 const int INF = 0x3f3f3f3f; 
     7 int dis[550], vis[550], used[550];   //used[]数组判负环; 
     8 int n, m, t;
     9 struct Edge
    10 {
    11     int from, to, w, next;
    12 } edge[5050];
    13 int head[550], cnt;
    14 void Add(int a, int b, int w)
    15 {
    16     Edge E = {a, b, w, head[a]};
    17     edge[cnt] = E;
    18     head[a] = cnt++;
    19 }
    20 bool Spfa(int src)
    21 {
    22      queue<int> q;
    23     for(int i = 1; i <= n; i++)
    24         dis[i] = INF; 
    25     dis[src] = 0; vis[src] = 1;
    26     q.push(src);    
    27     while(!q.empty())
    28     {
    29     //    printf("1
    ");
    30         int u = q.front();
    31         q.pop(); 
    32         vis[u] = 0;
    33         for(int i = head[u]; i != -1; i = edge[i].next)
    34         {
    35             int v = edge[i].to;
    36             if(dis[v] > dis[u] + edge[i].w)
    37             {
    38                 dis[v] = dis[u] + edge[i].w;
    39                 if(!vis[v])
    40                 {
    41                     vis[v] = 1;
    42                     q.push(v);
    43                     used[v]++;
    44                 }
    45             }
    46             if(used[v] > n)     //存在负环;
    47                 return true;
    48         }
    49     }
    50     return false;
    51 }
    52 int main()
    53 {
    54     int nt;
    55     scanf("%d", &nt);
    56     while(nt--)
    57     {
    58         scanf("%d %d %d", &n, &m, &t);
    59         int u, v, w; cnt = 0;
    60         memset(vis, 0, sizeof(vis));
    61         memset(used, 0 , sizeof(used));
    62         memset(head, -1, sizeof(head));
    63         for(int i = 0; i < m; i++)
    64         {
    65             scanf("%d %d %d", &u, &v, &w);
    66             Add(u, v, w);
    67             Add(v, u, w);
    68         }
    69         for(int i = 1; i <= t; i++)
    70         {
    71             scanf("%d %d %d", &u, &v, &w);
    72             Add(u, v, -w);
    73         } 
    74         if(Spfa(1))
    75             printf("YES
    ");
    76         else
    77             printf("NO
    "); 
    78     }
    79     return 0;
    80 }
  • 相关阅读:
    IO复习
    递归
    转换流
    编码与解码
    打印流(printStream)
    Properties
    【转】将Visual Studio武装到底
    【转】VS2008中的自定义格式化代码
    C++开发工具的常用插件
    抽烟的注意事项
  • 原文地址:https://www.cnblogs.com/soTired/p/4746368.html
Copyright © 2011-2022 走看看