zoukankan      html  css  js  c++  java
  • bzoj1715 [Usaco2006 Dec]Wormholes 虫洞

    1715: [Usaco2006 Dec]Wormholes 虫洞

    Time Limit: 5 Sec  Memory Limit: 64 MB
    Submit: 909  Solved: 478
    [Submit][Status][Discuss]

    Description

    John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前)。John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞。其中1<=N<=500,1<=M<=2500,1<=W<=200。 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗。 John将向你提供F(1<=F<=5)个农场的地图。没有小路会耗费你超过10000秒的时间,当然也没有虫洞回帮你回到超过10000秒以前。

    Input

    * Line 1: 一个整数 F, 表示农场个数。

    * Line 1 of each farm: 三个整数 N, M, W。

    * Lines 2..M+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条用时T秒的小路。

    * Lines M+2..M+W+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条可以使John到达T秒前的虫洞。

    Output

    * Lines 1..F: 如果John能在这个农场实现他的目标,输出"YES",否则输出"NO"。

    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

     

    Source

    分析:想要回到最开始,肯定有一个虫洞能够每走一次相对于走之前的时间减少,也就是说原图出现负环那么spfa判断一下就好.
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #include <queue>
    
    using namespace std;
    
    const int maxn = 1510, maxm = 10010,inf = 0x7ffffff;
    
    int f, n, m, w, head[maxn], ww[maxm], to[maxm], nextt[maxm], tot = 1, rudu[maxn], vis[maxn],d[maxn];
    
    void add(int x, int y, int z)
    {
        ww[tot] = z;
        to[tot] = y;
        nextt[tot] = head[x];
        head[x] = tot++;
    }
    
    queue <int> q;
    
    bool spfa()
    {
        memset(vis, 0, sizeof(vis));
        memset(rudu, 0, sizeof(rudu));
        for (int i = 1; i <= n; i++)
            d[i] = inf;
        d[1] = 0;
        q.push(1);
        vis[1] = 1;
        while (!q.empty())
        {
            int u = q.front();
            q.pop();
            vis[u] = 0;
            for (int i = head[u]; i; i = nextt[i])
            {
                int v = to[i];
                if (d[v] > d[u] + ww[i])
                {
                    d[v] = d[u] + ww[i];
                    ++rudu[v];
                    if (rudu[v] > n)
                        return true;
                    if (!vis[v])
                    {
                        vis[v] = 1;
                        q.push(v);
                    }
                }
            }
        }
        return false;
    }
    
    int main()
    {
        scanf("%d", &f);
        while (f--)
        {
            memset(head, 0, sizeof(head));
            tot = 1;
            scanf("%d%d%d", &n, &m, &w);
            for (int i = 1; i <= m; i++)
            {
                int u, v, z;
                scanf("%d%d%d", &u, &v, &z); 
                add(u, v, z); 
                add(v, u, z); 
            }
            for (int i = 1; i <= w; i++)
            {
                int u, v, z;
                scanf("%d%d%d", &u, &v, &z);
                add(u, v, -z);
            }
            if (spfa())
                printf("YES
    ");
            else
                printf("NO
    ");
        }
    
        return 0;
    }
  • 相关阅读:
    Java泛型
    Hibernate JPA实体继承的映射(一) 概述
    Hibernate JPA实体继承的映射(二) @MappedSuperclass
    rownum使用说明
    Javascript 中使用Json的四种途径
    JavaScript中使用JSON,即JS操作JSON总结
    Refresh Tokens: When to Use Them and How They Interact with JWTs
    Nginx 安装与启动
    JSON与js对象序列化
    js对象小结
  • 原文地址:https://www.cnblogs.com/zbtrs/p/7554459.html
Copyright © 2011-2022 走看看