http://poj.org/problem?id=3259

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..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..M+W+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.
 

题目大意:虫洞问题,现在有n个点,m条边,代表现在可以走的通路,比如从a到b和从b到a需要花费c时间,现在在地上出现了w个虫洞,虫洞的意义就是你从a到b话费的时间是-c(时间倒流,并且虫洞是单向的),现在问你从某个点开始走,能回到从前

解题思路:其实给出了坐标,这个时候就可以构成一张图,然后将回到从前理解为是否会出现负权环,用bellman-ford就可以解出了

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 900001
struct node
{
int u,v,w;
}q[];
int dis[];
int n,m,w1,count=;
int B()
{
int flag=;
for(int i=;i<=n;i++)
dis[i]=N;
dis[]=;
for(int i=;i<=n-;i++)
{
flag=;
for(int j=;j<count;j++)
{
if(dis[q[j].v]>dis[q[j].u]+q[j].w)//这里u,v是不能颠倒的,因为
{
dis[q[j].v]=dis[q[j].u]+q[j].w;
flag=;
}
}
/* for(int j=0;j<n;j++)
printf(".%d",dis[j]);*/
if(!flag) break;
}
for(int i=;i<count;i++)
{
if(dis[q[i].v]>dis[q[i].u]+q[i].w)
return ;
}
return ;
}
int main()
{
int x,y,x1,T;
scanf("%d",&T);
while(T--)
{
count=;
scanf("%d%d%d",&n,&m,&w1);
while(m--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=x1;
q[count].u=y;
q[count].v=x;
q[count++].w=x1;
}
while(w1--)
{
scanf("%d%d%d",&x,&y,&x1);
q[count].u=x;
q[count].v=y;
q[count++].w=-x1;//他是有方向的
}
int t=B();
if(t==) printf("YES\n");
else printf("NO\n");
}
return ;
}

第二次写的:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define INF 0x7fffffff
using namespace std;
int n,m,k,tt;
struct node
{
int x,y,z;
} q[];
int dis[];
void add(int xx,int yy,int zz)
{
q[tt].x=xx;
q[tt].y=yy;
q[tt++].z=zz;
}
void BF()
{
int flag;
dis[]=;
for(int i=; i<=n; i++)
{
flag=;
for(int i=; i<tt; i++)
{
if(dis[q[i].y]>dis[q[i].x]+q[i].z)
{
dis[q[i].y]=dis[q[i].x]+q[i].z;
flag=;
}
}
if(flag==) break;
}
if(flag==) printf("YES\n");
else printf("NO\n");
}
int main()
{
int T,zz,xx,yy;
cin>>T;
while(T--)
{
cin>>n>>m>>k;
tt=;
for(int i=; i<m; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,zz);
add(yy,xx,zz);
}
for(int i=; i<k; i++)
{
cin>>xx>>yy>>zz;
add(xx,yy,-zz);
}
BF();
}
return ;
}

poj3259: Wormholes(BF模板题)的更多相关文章

  1. poj3259 Wormholes (判负环)【spfa】(模板)

    <题目链接> 题目大意: John的农场里N块地,M条路连接两块地,W个虫洞,虫洞是一条单向路,会在你离开之前把你传送到目的地,就是当你过去的时候时间会倒退Ts.我们的任务是知道会不会在从 ...

  2. POJ3259 Wormholes

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

  3. HDU 4347 - The Closest M Points - [KDTree模板题]

    本文参考: https://www.cnblogs.com/GerynOhenz/p/8727415.html kuangbin的ACM模板(新) 题目链接:http://acm.hdu.edu.cn ...

  4. [AHOI 2009] 维护序列(线段树模板题)

    1798: [Ahoi2009]Seq 维护序列seq Time Limit: 30 Sec  Memory Limit: 64 MB Description 老师交给小可可一个维护数列的任务,现在小 ...

  5. HDU 2222 AC自动机模板题

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2222 AC自动机模板题 我现在对AC自动机的理解还一般,就贴一下我参考学习的两篇博客的链接: http: ...

  6. POJ2774 & 后缀数组模板题

    题意: 求两个字符串的LCP SOL: 模板题.连一起搞一搞就好了...主要是记录一下做(sha)题(bi)过程心(cao)得(dan)体(xin)会(qing) 后缀数组概念...还算是简单的,过程 ...

  7. HDU 1251 Trie树模板题

    1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #incl ...

  8. HDU-3549 最大流模板题

    1.HDU-3549   Flow Problem 2.链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 3.总结:模板题,参考了 http://ww ...

  9. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

随机推荐

  1. mysql学习之索引

    首先,看一个例子,有一张大表,记录数超过1000,SELECT * FROM student WHERE name='xinan'; 如果没有索引,查找程序就得从头查找,很费时间,表越大越费时间.建立 ...

  2. 3.STM32F4按键扫描函数

    //按键处理函数 //返回按键值 //mode:0,不支持连续按;1,支持连续按; //0,没有任何按键按下 //1, KEY0 按下 2, KEY1 按下 3, KEY2 按下 4, WKUP 按下 ...

  3. python 解释器

    原文 Python 能让程序紧凑, 可读性增强. 用 Python 写的程序通常比同样的 C, C++ 或 Java 程序要短得多, 这是因为以下几个原因: 高级数据结构使你可以在单独的语句中也能表述 ...

  4. python爬虫重定向次数过多问题

    错误提示如下: raise TooManyRedirects('Exceeded %s redirects.' % self.max_redirects, response=resp)requests ...

  5. linux内核添加模块

    参考: http://blog.csdn.net/gaoguoxin2/article/details/50220665 动态添加模块不需要编译内核. LINUX的模块主要由6部分组成: 1.模块的加 ...

  6. [No0000138]软件开发基础知识

    1. 本文目的 本文目的在于,介绍软件开发的各种基础知识 以实现,看了之后,对于软件开发的很多领域的基础知识有所了解 如此在进行后续的真正的软件开发时,遇到各种细节知识,才会明白由来和背景知识 第 1 ...

  7. TF模型训练中注意Loss和F1的变化情况

    之前训练模型,认为网络图构建完成,Loss肯定是呈现下降的,就没有太留心,知识关注F1的变化情况,找到最优的F1训练就停止了,认为模型就ok. 但实际中发现,我们要时刻关注网络的损失变化情况,batc ...

  8. rsync定时同步文件

    rsync服务器 ip:192.168.1.198 操作系统:centos7.2 rsync客户端 ip:192.168.1.16 操作系统:centos7.2 服务器配置 1.yum -y inst ...

  9. HTTP协议之Transfer-Encoding

    HTTP协议中的Transfer-Encoding 浏览器和服务器端支持持久连接 持久连接(Persist Connection) HTTP1.0默认不是持久连接的 HTTP1.1默认是持久连接的 在 ...

  10. Flink - ResultPartition

    发送数据一般通过,collector.collect public interface Collector<T> { /** * Emits a record. * * @param re ...