zoukankan      html  css  js  c++  java
  • 【最短路】poj3259-Wormholes(Bellman-Ford 最短路)

                                                                                               Wormholes
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 32109   Accepted: 11660

    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.

    Source

     
    题意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。简化下,就是看图中有没有负权环。有的话就是可以,没有的话就是不可以了。
     
           这道题的题意不好懂,主要难理解的是:
            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.  一个是单向边,而且单向边为负值。
     
     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<stdlib.h>
     4 
     5 const int VM=520;
     6 const int EM=5020;
     7 const int INF=0x3f3f3f3f;
     8 
     9 struct Edge{
    10     int u,v;
    11     int c;
    12 }edge[EM<<1];
    13 
    14 int n, m, w, cnt, dis[VM];
    15 
    16 
    17     void add(int u, int v, int c)
    18     {
    19         edge[cnt].u = u;
    20         edge[cnt].v = v;
    21         edge[cnt].c = c;
    22         cnt++;
    23     }
    24     int Bellman()
    25     {
    26         int i, j, k, flag;
    27         for(i=0; i<n; i++)
    28             dis[i] = INF;
    29         for(i=1; i<n; i++)
    30         {
    31             flag = 0;
    32             for(j=0; j<cnt; j++)
    33             {
    34                 if(dis[edge[j].v] > dis[edge[j].u] + edge[j].c)
    35                 {
    36                     dis[edge[j].v] = dis[edge[j].u] + edge[j].c;
    37                     flag = 1;
    38                 }
    39             }
    40             if(!flag) break;
    41         }
    42         for(k=0; k<cnt; k++)
    43             if(dis[edge[k].v] > dis[edge[k].u] + edge[k].c)
    44                 return 1;
    45         return 0;
    46 
    47     }
    48     int main()
    49     {
    50         int T, i;
    51         scanf("%d", &T);
    52         while(T--)
    53         {
    54             int x, y, z;
    55             cnt = 0;
    56             scanf("%d%d%d", &n, &m, &w);
    57             for(i=0; i<m; i++)
    58             {
    59                 scanf("%d%d%d", &x, &y, &z);
    60                 add(x, y, z);//加边
    61                 add(y, x, z);
    62             }
    63             for(i=0; i<w; i++)
    64             {
    65                 scanf("%d%d%d", &x, &y, &z);
    66                 add(x, y, -z);//注意这里是-z
    67             }
    68             if(Bellman())
    69                 printf("YES
    ");
    70             else printf("NO
    ");
    71         }
    72         return 0;
    73     }
     
     
      
  • 相关阅读:
    将MySQL中的数据导入到Solr
    Solr中schema.xml的Field介绍
    Window下Nginx的安装和启动
    Idea中运行shell脚本
    window10 下安装Mysql5.6
    Solr Admin管理界面使用说明
    Solr 整合中文分词器mmseg4j
    Solr安装和使用
    ActiveMQ 安装和使用
    AbstractQueuedSynchronizer源码解析
  • 原文地址:https://www.cnblogs.com/6bing/p/4138440.html
Copyright © 2011-2022 走看看