poj 3259(bellman最短路径)
| 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..N, M (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 of each farm: Three space-separated integers respectively: N, M, and 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.
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.
Output
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 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最短路径)的更多相关文章
- poj 3259 bellman最短路推断有无负权回路
		Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36717 Accepted: 13438 Descr ... 
- POJ 3259 Wormholes(最短路径,求负环)
		POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ... 
- ACM:  POJ 3259 Wormholes - SPFA负环判定
		POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ... 
- 最短路(Bellman_Ford) POJ 3259 Wormholes
		题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ... 
- poj - 3259 Wormholes (bellman-ford算法求最短路)
		http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ... 
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
		Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ... 
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
		Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ... 
- poj 3259 Wormholes(最短路 Bellman)
		题目:http://poj.org/problem?id=3259 题意:一个famer有一些农场,这些农场里面有一些田地,田地里面有一些虫洞,田地和田地之间有路,虫洞有这样的性质: 时间倒流.问你这 ... 
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
		Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ... 
随机推荐
- 原型链(__proto__)
			前面详细的解释了new的几个步骤,其中随意带过了一下原型链的概念,如果细读那篇文章,基本对原型也能有所理解. 原型有两个关键属性,一个是 __proto__ 一个是 prototype ,了解了这两个 ... 
- WM_SYSCOMMAND消息命令整理 good
			注意:1. 使用WM_SYSCOMMAND时,鼠标的一些消息可能会受到影响,比如不能响应MouseUp事件,可以在窗口中捕获WM_SYSCOMMAND消息,并判断消息的CommandType来判断消息 ... 
- Qt中提高sqlite的读写速度(使用事务一次性写入100万条数据)
			SQLite数据库本质上来讲就是一个磁盘上的文件,所以一切的数据库操作其实都会转化为对文件的操作,而频繁的文件操作将会是一个很好时的过程,会极大地影响数据库存取的速度.例如:向数据库中插入100万条数 ... 
- 阿里2016实习offer五面经验与总结(转)
			前言 目前楼主已经拿到阿里实习offer,一共经历了5次面试,其中4轮技术面,1轮HR面试.在这里分享一下自己的面试经验和学习总结.写这篇面经主要是希望能够帮助更多的小伙伴.我本科毕业于中南大学信管专 ... 
- Ubuntu 32下Android NDK+NEON的配置过程及简单使用举例
			1. 利用VMware在Windows7 64位下安装Ubuntu13.10 32位虚拟机: 2. 从 https://developer.android.com/tools/sdk/ndk/in ... 
- python实现了字符串的按位异或和php中的strpad函数
			近期在写自己主动化測试,因为开发加密中用到strpad和字符串的按位异或,而python中没有这种函数和功能,所以必须自己写一套,要不自己主动化測试无法进行,所以就用python实现了一下,因为在写字 ... 
- Python数据结构-序列
			shopList=['apple','orange','pen'] print(shopList) print(]) print(]) print(:])) print(])) 运行结果: ['app ... 
- Java中字符串中子串的查找共有四种方法(indexof())
			Java中字符串中子串的查找共有四种方法(indexof()) Java中字符串中子串的查找共有四种方法,如下:1.int indexOf(String str) :返回第一次出现的指定子字符串在此字 ... 
- JVM必备指南(转)
			本文由 ImportNew - xiafei 翻译自 anturis.欢迎加入翻译小组.转载请见文末要求. 简介 Java虚拟机(JVM)是Java应用的运行环境,从一般意义上来讲,JVM是通过规范来 ... 
- 该View转换成Bitmap方法
			方法一: /** * 该View绘制到Bitmap上 * @param view 须要绘制的View * @param width 该View的宽度 * @param height 该View的高度 ... 
