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 ...
随机推荐
- 《转载》常用算法经典代码(C++版)
转自:http://blog.renren.com/blog/311453043/736944237 一.快速排序 void qsort(int x,int y) //待排序的数据存放在a[1]..a ...
- Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)
uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...
- jquery怎么在点击li标签之后添加一个在class,点击下一个li时删除上一个class?
思路:点击当前li元素后是用removeClass()删除所有兄弟元素(使用siblings()获取)的class样式,然后使用addClass()为当前li添加class. 具体演示如下: 1.HT ...
- MIPS平台目标机安装Oprofile时出现error
在Debian下安装Oprofile 重要:应该使linux内核选项支持 在 .config 文件中设置CONFIG_PROFILING=y和CONFIG_OPROFILE=y. 重新编译,烧写.使用 ...
- facebook打开动画pop
POP源代码:https://github.com/facebook/pop demo相关链接:https://github.com/jxd001/POPdemo/blob/master/README ...
- oracle 12c 中asm元数据是否有所变化
详见原文博客链接地址: oracle 12c 中asm元数据是否有所变化
- c++里的类型转化
c++里的类型转换种类 在c++里包含4种,static_cast,dynamic_cast,const_cast,reinterpret_cast. 4种类型 reinterpret_cast: 强 ...
- NET实现的DDD、CQRS与微服务架构
WeText项目:一个基于.NET实现的DDD.CQRS与微服务架构的演示案例 最近出于工作需要,了解了一下微服务架构(Microservice Architecture,MSA).我经过两周业余时间 ...
- CV和Resume的区别(转)
常常有人把CV和Resume混起来称为“简历”,其实精确而言,CV应该是“履历”,Resume才是简历.Resume概述了有关的教育准备和经历,是对经验技能的摘要:curriculum vitae则集 ...
- Web工程师的工具箱 | 酷壳 - CoolShell.cn
Web工程师的工具箱 | 酷壳 - CoolShell.cn Web工程师的工具箱 2012年12月19日 陈皓 发表评论 阅读评论 30,168 人阅读 本文出自Ivan Zuzak 的&l ...