• POJ:3259-Wormholes(最短路判断负环)


    Wormholes

    Time Limit: 2000MS Memory Limit: 65536K
    Total Submissions: 58153 Accepted: 21747

    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. 题意就是一个图里面有很多双向边,也有单向的负权路径,问有没有负权环。
    2. 这里主要就是问了一个关于最短路中怎么判断是否存在负环,一般有负权路径的图都是用SPFA来跑,如果不存在负环一个点最多被压入队列中n次(被其余的每个点更新),那么如果大于n次说明存在负环,还是很容易想明白的,代码实现也很简单,记录一下压入队列的次数就行了。

    #include <stdio.h>
    #include <cstring>
    #include <vector>
    #include <queue>
    using namespace std;
    const int maxn = 510;
    int Time[maxn],n,m,w;
    bool vis[maxn];
    int maps[maxn][maxn];
    
    void init() {
        memset(Time,0x3f, sizeof(Time));
        memset(maps,0x3f,sizeof(maps));
        scanf("%d%d%d",&n,&m,&w);
        for(int i=0;i<m;i++) {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            if(maps[a][b] > c)
                maps[a][b] = maps[b][a] = c;
        }
        for(int i=0;i<w;i++) {
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            c = -c;
            if(maps[a][b] > c)
                maps[a][b] = c;
        }
    }
    
    bool spfa() {
        int num[maxn];
        memset(num,0, sizeof(num));
        memset(vis,0,sizeof(vis));
        num[1] = 1;
        queue <int> qu;
        qu.push(1);
        Time[1] = 0;
        while(!qu.empty()) {
            int now = qu.front(); qu.pop();
            vis[now] = false;
            for(int i=1;i<=n;i++) {
                if(maps[now][i] != 0x3f3f3f3f) {
                    if(Time[now] + maps[now][i] < Time[i]) {
                        Time[i] = maps[now][i] + Time[now];
                        if(!vis[i]) {
                            vis[i] = true;
                            qu.push(i);
                            num[i]++;
                            if(num[i] > n) {
                                return true;
                            }
                        }
                    }
                }
            }
        }
        return false;
    }
    
    int main() {
        int t;
        scanf("%d",&t);
        while(t--) {
            init();
            bool is_back = spfa();
            if(is_back) 
                printf("YES
    ");
            else 
                printf("NO
    ");
        }
        return 0;
    }
  • 相关阅读:
    CodeForces C. Songs Compression
    CodeForces B. Obtaining the String
    Linux-命令学习-df
    phplib系统开发经验总结
    阶段3-团队合作项目-网络安全传输系统sprint1-传输子系统设计第3课-加密传输优化
    阶段3-团队合作项目-网络安全传输系统sprint1-传输子系统设计第2课-OpenSSL加密系统
    阶段3-团队合作项目-网络安全传输系统sprint1-传输子系统设计第1课-系统程序框架搭建
    阶段3-团队合作项目-网络安全传输系统sprint0-产品规划与设计第1课-产品功能展示
    阶段2-新手上路项目-移动物体监控系统Sprint4-嵌入式web服务器开发第3课-CGI程序开发
    阶段2-新手上路项目-移动物体监控系统Sprint4-嵌入式web服务器开发第2课-CGI快速入门-网页控制LED
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9107149.html
Copyright © 2020-2023  润新知