zoukankan      html  css  js  c++  java
  • F

    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.. MW+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.
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <string>
    #include <set>
    #include <queue>
    #include <map>
    #include <sstream>
    #include <cstdio>
    #include <cstring>
    #include <numeric>
    #include <cmath>
    #include <iomanip>
    #include <deque>
    #include <bitset>
    //#include <unordered_set>
    //#include <unordered_map>
    //#include <bits/stdc++.h>
    //#include <xfunctional>
    #define ll              long long
    #define PII             pair<int, int>
    #define rep(i,a,b)        for(ll  i=a;i<b;i++)
    #define dec(i,a,b)        for(ll  i=a;i>=b;i--)
    #define pb              push_back
    #define mp              make_pair
    using namespace std;
    int dir[5][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 } ,{ 0,0 } };
    const long long INF = 0x7f7f7f7f7f7f7f7f;
    const int inf = 0x3f3f3f3f;
    const double pi = 3.14159265358979;
    const int mod = 1e9 + 7;
    const int N = 2500 * 2 + 200 + 10;
    //if(x<0 || x>=r || y<0 || y>=c)
    
    inline ll read()
    {
        ll x = 0; bool f = true; char c = getchar();
        while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
        while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
        return f ? x : -x;
    }
    
    int n, m, w;
    struct edge
    {
        int u, v;
        int t;
    };
    
    edge e[N];
    
    bool bf()
    {
        vector<int> d(510, 10000);
        d[1] = 0;
    
        for (int i = 1; i < n; i++)
        {
            int fg = 1;
            for (int j = 0; j < 2 * m + w; j++)
            {
                int u = e[j].u;
                int v = e[j].v;
                int t = e[j].t;
    
                if (d[v] > d[u] + t)
                {
                    d[v] = d[u] + t;
                    fg = 0;
                }
            }
            if (fg)
                return false;
        }
        for(int i=0;i<2 * m + w; i++)
        {
            if (d[e[i].v] > d[e[i].u] + e[i].t)
                return true;
        }
        return false;
    }
    
    int main()
    {
        int T;
        cin >> T;
        while (T--)
        {
            cin >> n >> m >> w;
            int u, v, t;
            int tmp = 0;
            for (int i = 1; i <= m; i++)
            {
                cin >> u >> v >> t;
                e[tmp].u = u;
                e[tmp].v = v;
                e[tmp++].t = t;
    
                e[tmp].u = v;
                e[tmp].v = u;
                e[tmp++].t = t;
            }
            for (int i = 1; i <= w; i++)
            {
                cin >> u >> v >> t;
                e[tmp].u = u;
                e[tmp].v = v;        
                e[tmp++].t = -t;
            }
            if (bf())
                cout << "YES" << endl;
            else
                cout << "NO" << endl;
        }
        return 0;
    }
  • 相关阅读:
    Hibernate(2)——Hibernate的实现原理总结和对其模仿的demo
    Hibernate(1)——数据访问层的架构模式
    JVM学习(4)——全面总结Java的GC算法和回收机制
    JVM学习(3)——总结Java内存模型
    JVM学习(2)——技术文章里常说的堆,栈,堆栈到底是什么,从os的角度总结
    JVM学习(1)——通过实例总结Java虚拟机的运行机制
    减小内存的占用问题——享元模式和单例模式的对比分析
    继承、组合和接口用法——策略模式复习总结
    软件工程的引入:Scrum开发框架总结
    软件工程里的UML序列图的概念和总结
  • 原文地址:https://www.cnblogs.com/dealer/p/12694442.html
Copyright © 2011-2022 走看看