• POJ 3259 Wormholes(spfa判负环)


    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..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.
     
     1 #include<cstdio>
     2 #include<queue>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 
     7 const int maxn=550;
     8 const int maxm=2555;
     9 const int inf=0x3f3f3f3f;
    10 int G[maxn][maxn];
    11 bool vis[maxn];
    12 int used[maxn];
    13 int d[maxn];
    14 int cnt;
    15 int m,n1,n2;
    16 
    17 bool spfa(int s)
    18 {
    19     queue<int>q;
    20     memset(vis,0,sizeof(vis));
    21     memset(d,inf,sizeof(d));
    22     memset(used,0,sizeof(used));
    23     d[s]=0;
    24     q.push(s);
    25     vis[s]=1;
    26     used[s]++;
    27     while(!q.empty())
    28     {
    29         int u=q.front();
    30         q.pop();
    31         vis[u]=0;
    32         for(int i=1;i<=m;i++)
    33         {
    34             if(d[i]>d[u]+G[u][i])
    35             {
    36                 d[i]=d[u]+G[u][i];
    37                 if(!vis[i])
    38                 {
    39                     if(used[i]>=maxn)
    40                     return true;
    41                     q.push(i);
    42                     vis[i]=1;
    43                     used[i]++;
    44                 }
    45             }
    46         }
    47     }
    48     return false;
    49 }
    50 
    51 int main()
    52 {
    53     int t,u,v,w;
    54     scanf("%d",&t);
    55     while(t--)
    56     {
    57         memset(G,inf,sizeof(G));
    58         scanf("%d%d%d",&m,&n1,&n2);
    59         for(int i=0;i<n1;i++)
    60         {
    61             scanf("%d%d%d",&u,&v,&w);
    62             G[u][v]=G[v][u]=min(G[u][v],w);
    63         }
    64         for(int i=0;i<n2;i++)
    65         {
    66             scanf("%d%d%d",&u,&v,&w);
    67             G[u][v]=min(G[u][v],-w);
    68         }
    69         int ans=spfa(1);
    70         if(ans)
    71         printf("YES
    ");
    72         else
    73         printf("NO
    ");
    74     }
    75     return 0;
    76 }
  • 相关阅读:
    CStdioFile的Writestring无法写入中文的问题
    Warning: skipping non-radio button in group. 的处理
    Linux命令 改变文档权限及所有者
    动态链接库dll的 静态加载 与 动态加载
    switch的方便用法
    mysql 安装过程中的错误:my-template.ini could not be processed and written to XXXmy.ini.Error code-1
    HDU 3790 最短路径问题 (SPFA)
    container_of用法及实现
    ZXing工具类v1.0
    基于MapReduce的HBase开发
  • 原文地址:https://www.cnblogs.com/homura/p/4741624.html
Copyright © 2020-2023  润新知