Wormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 30169   Accepted: 10914

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.

Source

field=source&key=USACO+2006+December+Gold" style="text-decoration:none">USACO 2006 December Gold

AC代码:

#include<iostream>
using namespace std;
struct Point{
int s,e,t;
}a[10000];
int se;
int n,m,w;
int bell_man(int start){
int dis[10000];
for(int i=1;i<=n;i++)
dis[i]=999999;
dis[start]=0; for(int i=1;i<n;i++)
for(int j=0;j<se;j++)
dis[a[j].e] = dis[a[j].e] > dis[a[j].s] + a[j].t ? dis[a[j].s] + a[j].t : dis[a[j].e]; for(int i=0;i<se;i++){
if(dis[a[i].e] > dis[a[i].s] + a[i].t)
return 1;
}
return 0;
}
int main(){
int T; cin>>T;
while(T--){
se=0;
cin>>n>>m>>w;
for(int i=0;i<m;i++){
int s,e,t;
cin>>s>>e>>t;
a[se].s=s; a[se].e=e; a[se++].t=t;
a[se].s=e; a[se].e=s; a[se++].t=t;
}
for(int i=0;i<w;i++){
int s,e,t;
cin>>s>>e>>t;
a[se].s=s; a[se].e=e; a[se++].t=-t;
}
//int k;
//for(k=1;k<=n;k++){ //事实上正确的起点应该要历遍全部点。可是这种超时了
//这个题目仅仅要1点就能够了。算是题目的一个非常大漏洞吧,数据太水了
if(bell_man(1)){
cout<<"YES"<<endl;
//break;
}
//}
//if(k>n)
else
cout<<"NO"<<endl;
}
return 0;
}

poj 3259(bellman最短路径)的更多相关文章

  1. poj 3259 bellman最短路推断有无负权回路

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36717   Accepted: 13438 Descr ...

  2. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  3. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  4. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  5. poj - 3259 Wormholes (bellman-ford算法求最短路)

    http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...

  6. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  7. POJ 3259——Wormholes——————【最短路、SPFA、判负环】

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

  8. poj 3259 Wormholes(最短路 Bellman)

    题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ...

  9. [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 29971   Accepted: 10844 Descr ...

随机推荐

  1. HDU 4921 Map

    题意: 给n个节点  他们形成了最多10条链  每条最多1000的长度  每一个节点有个val  你能够选择任何位置截断链  断点前的全部节点被你获得  通过题中计算公式得出你的val  问  通过随 ...

  2. 14.3.5.1 An InnoDB Deadlock Example

    14.3.5 Deadlocks in InnoDB 14.3.5.1 An InnoDB Deadlock Example 14.3.5.2 Deadlock Detection and Rollb ...

  3. Android Application Fundamentals——Android应用程序基础知识

    Application Fundamentals--应用程序基础知识 Key classes--关键类 Activity Service BroadcastReceiver ContentProvid ...

  4. OCP读书笔记(11) - 使用闪回技术II

    闪回归档 1. 什么是闪回数据归档? 闪回归档是用来保存一个或多个表的历史数据的新数据库对象,以及该数据的存储保留和清除策略.归档只是保存数据库中一个或多个表的所有事务处理的变化的一个或多个表空间,数 ...

  5. [Android面试题-7] 写出一个Java的Singleton类(即单例类)

    1.首先明确单例的概念和特点: a>单例类只能有一个实例 b>单例类必须自己创建一个自己的唯一实例 c>单例类必须为其他所有对象提供这个实例 2.单例具有几种模式,最简单的两种分别是 ...

  6. A股市场暴跌背后的三大元凶?

    周一两市低开低走,盘中空方连续打压股指,大盘一路下行,沪指2000点关口告急,收于1963.24点,跌幅超过了5%.行业板块全线溃败.银行.证券领衔大幅杀跌,板块跌幅一度超过5%:继上周五中国石油A股 ...

  7. Selenium 验证picklist是可被正确选中且是有序的(动态数组赋值)

    原代码: <select id="edit-submitted-im-interesting-in" class="form-select required&quo ...

  8. 该项目的建设maven片:4.协调和依赖,spring依赖注入demo

    源码下载 协调 <groupId>com.demo.animal</groupId> <artifactId>animal-core</artifactId& ...

  9. Java 使用AES/CBC/PKCS7Padding 加解密字符串

    介于java 不支持PKCS7Padding,只支持PKCS5Padding 但是PKCS7Padding 和 PKCS5Padding 没有什么区别要实现在java端用PKCS7Padding填充, ...

  10. ActiveMQ源码架构解析第一节(转)

    工作四年已久,也快到了而立之年,本人也酷爱技术,总是想找一些途径来提升自己,想着温故而知新所以就写起了博客,然而写博客这个想法也是酝酿了很久,近期也看到了有很多人在问关于ActiveMQ的相关问题,有 ...