POJ - 3259—— Wormholes
Time Limit: 2000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u
 use MathJax to parse formulas

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.

在探索他的农场时,农场主约翰发现了一些令人惊叹的虫洞。虫洞是非常独特的,因为它是一条单向路径,在进入虫洞之前,它会把你带到它的目的地。每个FJ农场由N(1≤N≤500)字段方便编号1 . .N,M(1≤≤2500)路径,和W W(1≤≤200)虫洞。

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.

帮助FJ找出是否这是可能的,他将为你提供完整的映射到F(F 1≤≤5)他的农场。没有路径需要超过1万秒的时间来旅行,而且没有虫洞可以让FJ回到1万多秒的时间。

Input

输入

Line 1: A single integer, F. F farm descriptions follow.

第1行:一个单独的整数,F . F农场描述如下。

Line 1 of each farm: Three space-separated integers respectively: N, M, and W

每个农场的第1行:分别是3个空格分隔的整数:N、M和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.

行2 . .每个农场的M + 1:分别描述的三个空格分隔的数字(S,E,T):在S和E之间的双向路径,需要T秒来遍历。两个字段可能由多个路径连接。

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.

行M + 2 . .每个农场的M + W + 1:分别描述的三个空格分隔的数字(S,E,T):一个从S到E的路径,这也使旅行者返回T秒。

Output

输出

Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

行1 . .F:对于每个农场,如果FJ能达到他的目标,输出“是”,否则输出“不”(不包括引号)。

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.

对于农场1号来说,FJ不能及时返回。

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.

对于第2个农场,FJ可以在周期1 - > 2 - > 3 - > 1的时间内返回,在他离开之前1秒到达他的起始位置。他可以从这个周期的任何一个地方开始完成这个任务。

 

题意是问是否能通过虫洞回到过去;

虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。

我们把虫洞看成是一条负权路,问题就转化成求一个图中是否存在负权回路;

所以说这个题还是比较水的。

各种错误:

数组开小了!一直RE!

该网站上无返回值的函数必须用void!否则CE。

在spfa里面多加了一个dis[s]=0;WA!

代码:

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 2521
using namespace std;
int t,dis[N],x,y,z,n,m,s,tot,head[N];
bool vis[N],flag;
struct Edge
{
    int to,next,ds;
}edge[N*];
int read()
{
    ,f=;
    char ch=getchar();
    ')
    {
        ;
        ch=getchar();
    }
    ')
    {
        x=x*+ch-';
        ch=getchar();
    }
    return x*f;
}
void add(int from,int to,int dis)
{
    tot++;
    edge[tot].ds=dis;
    edge[tot].to=to;
    edge[tot].next=head[from];
    head[from]=tot;
}
void begin()
{
    memset(dis,,sizeof(dis));
    memset(vis,false,sizeof(vis));
    memset(head,,sizeof(head));
    tot=flag=;
}
void spfa(int s)
{
    vis[s]=true;
//    if(flag) return ;
    for(int i=head[s];i;i=edge[i].next)
    {
        if(dis[s]+edge[i].ds<dis[edge[i].to])
        {
            if(vis[edge[i].to]||flag)
            {
                flag=true;
                break;
            }
            dis[edge[i].to]=dis[s]+edge[i].ds;
            spfa(edge[i].to);
        }
    }
    vis[s]=false;
}
int main()
{
    t=read();
    while(t--)
    {
        n=read(),m=read(),s=read();
        begin();
        ;i<=m;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,z);
            add(y,x,z);
        }
        ;i<=s;i++)
        {
            x=read(),y=read(),z=read();
            add(x,y,-z);
        }
        ;i<=n;i++)
        {
            spfa(i);
            if(flag) break;
        }
        if(flag) printf("YES\n");
        else printf("NO\n");
    }
    ;
}

Wormholes(spfa判负环)的更多相关文章

  1. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  2. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  3. POJ3259 :Wormholes(SPFA判负环)

    POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...

  4. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  5. BZOJ 1715: [Usaco2006 Dec]Wormholes 虫洞 DFS版SPFA判负环

    Description John在他的农场中闲逛时发现了许多虫洞.虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前).John的每个农场有M条小路(无向边)连接着N ...

  6. POJ3259 Wormholes(SPFA判断负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  7. spfa判负环

    bfs版spfa void spfa(){ queue<int> q; ;i<=n;i++) dis[i]=inf; q.push();dis[]=;vis[]=; while(!q ...

  8. poj 1364 King(线性差分约束+超级源点+spfa判负环)

    King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 14791   Accepted: 5226 Description ...

  9. 2018.09.24 bzoj1486: [HNOI2009]最小圈(01分数规划+spfa判负环)

    传送门 答案只保留了6位小数WA了两次233. 这就是一个简单的01分数规划. 直接二分答案,根据图中有没有负环存在进行调整. 注意二分边界. 另外dfs版spfa判负环真心快很多. 代码: #inc ...

  10. BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划

    BZOJ 4898 [APIO2017] 商旅 | SPFA判负环 分数规划 更清真的题面链接:https://files.cnblogs.com/files/winmt/merchant%28zh_ ...

随机推荐

  1. qemu-img管理虚拟机

    qemu-img管理虚拟机 1. 查看正在运行的虚拟机 [root@idca-vm02 ~]# virsh list Id    名称                         状态 ----- ...

  2. React初识整理(一)

    一.React的特点 1.自动化的UI状态管理:自动完成数据变化与界面效果的更新. 2.虚拟DOM:创建1个虚拟的dom节点树,放在内存里(内存修改数据效率高),数据变化时先修改内存里的虚拟DOM,然 ...

  3. PHP必知必会

    MQ(消息队列) 消息队列主要用于以下场景: 1. 上传图片,用户需要迅速反馈,把上传图片的后续操作交给consumer 2. A用户对B用户发消息 3. 日志记录,APP发生的任何警告错误日志都要被 ...

  4. Spring Boot -- Idea搭建下搭建web项目

    最近公司准备使用Spring Boot框架,让小瑾先来学习一下,为了表示小瑾的办事效率,小瑾直接先学习用Idea搭建一个Spring Boot项目,哈哈哈,坐等领导夸. 废话不多说了,先来总结一下用I ...

  5. linux centeros 通过 innoback 工具备份mysql 5.7 全库并自动压缩zip上传到备份服务器的脚本,附自动清理过期备份

    innoback 安装见连接:https://blog.csdn.net/fanren224/article/details/79693863 脚本解析后续将更新 181024:更新添加定期清理备份的 ...

  6. MySQL Group Replication(组复制MGR)

    MGR基本要求: 1.InnoDB存储引擎 2.主键,每个表必须具有已定义的主键或等效的主键,其中等效项是非null唯一键 3.IPv4网络 4.网络性能 5.开启二进制日志并开启GTID模式 6.m ...

  7. golang 实现冒泡排序

    package main import ( "fmt" ) func main(){ a := [...] int{2,5,9,6,8} fmt.Println(a) num := ...

  8. sklearn 快速入门教程

    1. 获取数据 1.1 导入sklearn数据集 sklearn中包含了大量的优质的数据集,在你学习机器学习的过程中,你可以通过使用这些数据集实现出不同的模型,从而提高你的动手实践能力,同时这个过程也 ...

  9. 三、Pandas速查手册中文版

    本文翻译自文章:Pandas Cheat Sheet - Python for Data Science,同时添加了部分注解. 对于数据科学家,无论是数据分析还是数据挖掘来说,Pandas是一个非常重 ...

  10. PAT Basic 1037

    1037 在霍格沃茨找零钱 如果你是哈利·波特迷,你会知道魔法世界有它自己的货币系统 —— 就如海格告诉哈利的:“十七个银西可(Sickle)兑一个加隆(Galleon),二十九个纳特(Knut)兑一 ...