zoukankan      html  css  js  c++  java
  • Wormholes

    Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u

    Submit Status

    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那点,能不能时光倒流
    题目大意:
    农民约翰在农场散步的时候发现农场有大量的虫洞,这些虫洞是非常特别的因为它们都是单向通道,为了方便现在把约翰的农田划分成N快区域,M条道路,W的虫洞。
    约翰是时空旅行的粉丝,他希望这样做,在一个区域开始,经过一些道路和虫洞然后回到他原来所在的位置,这样也许他就能见到他自己了。
    穿越虫洞会回到以前。。。。。(穿越者约翰)
    /////////////////////////////////////////////////////////////
    很明显这是一个很扯淡的故事,不过为了出一道带负权值的题目也是难为了出题人,迪杰斯特拉算法不能处理带有负权值的问题,佛洛依德倒是可以,不过复杂度很明显太高,所以spfa是不错的选择,只需要判断出发点时间是否变小.
    #include<iostream>
    #include<vector>
    #include<queue>
    
    using namespace std;
    
    #define N 550
    #define INF 0xffffff
    
    struct node
    {
        int y, w;
    }P[N];
    
    vector<node> G[N];
    int v[N];
    
    int spfa(int s)
    {
        queue<int> Q;
    
        Q.push(s);
    
        while(Q.size())
        {
            s = Q.front();
            Q.pop();
    
            int len = G[s].size();
    
            for(int i = 0; i < len; i++)
            {
                node q = G[s][i];
    
                if(v[s] + q.w < v[q.y])
                {
                    v[q.y] = v[s] + q.w;
                    Q.push(q.y);
                }
    
    
            }
            if(v[1] < 0)
                return 1;
        }
    
        return 0;
    }
    int main()
    {
        int t, n, m, w, s, e, f;
    
         cin >> f;
    
         while(f--)
         {
             for(int i = 1; i < N; i++)
             {
                 v[i] = INF;
                 G[i].clear();
             }
    
             v[1] = 0;
    
             cin >> n >> m >> w;
             node p;
    
             for(int i = 0; i < m; i++)
             {
                 cin >> s >> e >> t;
                 p.y = e, p.w = t;
                 G[s].push_back(p);
                 p.y = s;
                 G[e].push_back(p);
             }
             for(int i = 0; i < w; i++)
             {
                 cin >> s >> e >> t;
                 p.y = e, p.w = -t;
                 G[s].push_back(p);
             }
             int ans = spfa(1);
    
             if(ans)
                cout << "YES" << endl;
             else
                cout << "NO" << endl;
         }
         return 0;
    }

    SPFA 在形式上和宽度优先搜索非常类似,不同的是宽度优先搜索中一个点出了队列就不可能重新进入队列,但是SPFA中一个点可能在出队列之后再次被放入队列,也就是一个点改进过其它的点之后,过了一段时间可能本身被改进,于是再次用来改进其它的点,这样反复迭代下去。

    让未来到来 让过去过去
  • 相关阅读:
    [ScreenOS] How to change the certificate that is used for SSL (HTTPS) WebUI Management
    [ScreenOS] How to manually generate a new system self-signed certificate to replace the expired system self-signed certificate without resetting the firewall
    ELK日志分析之安装
    用ElasticSearch存储日志
    Elasticsearch5安装以及部署Head插件
    Elasticsearch+Hbase实现海量数据秒回查询
    展示消息提醒信息,2019年1月1日 09:52:54
    cvc-complex-type.2.3: Element 'dependency' cannot have character [children], because the type's cont
    layui 上传图片回显并点击放大实现
    layui table 行按钮事件,启用禁用切换
  • 原文地址:https://www.cnblogs.com/Tinamei/p/4678591.html
Copyright © 2011-2022 走看看