Wormholes - poj 3259 (Bellman-Ford算法)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 34934 | Accepted: 12752 |
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.
#include <iostream>
using namespace std;
struct farm {
int S;
int E;
int T;
} f[];
int main() {
int num;
int N, M, W;
cin >> num;
int F[];
for (int i = ; i < num; i++) {
cin >> N >> M >> W;
for (int j = ; j < N; j++) {
F[j] = ;
}
F[] = ;
for (int j = ; j < M; j++) {
int a, b, c;
cin >> a >> b >> c;
f[*j].S = a;
f[*j].E = b;
f[*j].T = c;
f[*j+].S = b;
f[*j+].E = a;
f[*j+].T = c; }
for (int j =* M; j < *M + W; j++) {
int a, b, c;
cin >> a >> b >> c;
f[j].S = a;
f[j].E = b;
f[j].T = - c;
}
for (int j = ; j < N-; j++) {
for (int k = ; k < *M + W; k++) {
if (F[f[k].E] > F[f[k].S] + f[k].T) {
F[f[k].E] = F[f[k].S] + f[k].T;
}
}
}
int flag = ;
for (int k = ; k < *M + W; k++) {
if (F[f[k].E] >F[f[k].S] + f[k].T) {
F[f[k].E] = F[f[k].S] + f[k].T;
flag=;
break;
}
}
if(flag){
cout<<"YES"<<endl;
}else{
cout<<"NO"<<endl;
}
}
return ;
}
Wormholes - poj 3259 (Bellman-Ford算法)的更多相关文章
- Bellman—Ford算法思想
---恢复内容开始--- Bellman—Ford算法能在更普遍的情况下(存在负权边)解决单源点最短路径问题.对于给定的带权(有向或无向)图G=(V,E),其源点为s,加权函数w是边集E的映射.对图G ...
- Bellman - Ford 算法解决最短路径问题
Bellman - Ford 算法: 一:基本算法 对于单源最短路径问题,上一篇文章中介绍了 Dijkstra 算法,但是由于 Dijkstra 算法局限于解决非负权的最短路径问题,对于带负权的图就力 ...
- (最短路 spfa)Wormholes -- poj -- 3259
http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions ...
- Dijkstra算法与Bellman - Ford算法示例(源自网上大牛的博客)【图论】
题意:题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 poj2387 Description Bessie is out in the field and ...
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- poj 3259 bellman最短路推断有无负权回路
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36717 Accepted: 13438 Descr ...
- ShortestPath:Wormholes(POJ 3259)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
- poj 3259(bellman最短路径)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 30169 Accepted: 10914 Descr ...
- kuangbin专题专题四 Wormholes POJ - 3259
题目链接:https://vjudge.net/problem/POJ-3259 思路:求有无负环,起点随意选就可以,因为目的只是找出有没有负环,有了负环就可以让时间一直回退,那么一定能回到当初,这里 ...
随机推荐
- [CF617E]XOR and Favorite Number/[CQOI2018]异或序列
题目大意: 给定一个长度为$n(n\leq10^5)$的数列$A$和数$k$$(A_i,k\leq10^6)$.$m$组询问,每次询问区间$[l,r]$中有多少对$i,j(l\leq i\leq j\ ...
- C/C++框架和库 (真的很强大) 转
http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 - 1. Webbench Webbench ...
- source insight研究——快捷键篇
转:http://blog.csdn.net/ison81/article/details/3510426 关于键盘和鼠标谁更快捷之争,是一个永远被程序员争论的话题.我想大多数人都不会极端的信奉一种操 ...
- JAVA常见算法题(二十三)
package com.xiaowu.demo; /** * 给一个不多于5位的正整数,要求:①求它是几位数:②逆序打印出各位数字. * * * @author WQ * */ public clas ...
- Xml解析(Dom解析xml)
xml四种解析方式: DOM 平台无关的官方解析方式 优点:形成了树结构,直观好理解,代码更易编写 解析过程中树结构保留在内存中,方便修改 缺点:当xml文件较大时,对内存耗费比较大,容易影响解析性能 ...
- 机器学习入门之四:机器学习的方法--SVM(支持向量机)(转载)
转自 飞鸟各投林 SVM(支持向量机) 支持向量机算法是诞生于统计学习界,同时在机器学习界大放光彩的经典算法. 支持向量机算法从某种意义上来说是逻辑回归算法的强化:通过给予逻辑回归算法更严格的优化条件 ...
- BeagleBone Black板第四课:简单LED控制实验
BBB板第四课:简单LED控制实验 学习BBB板的终于目的是想像单片机一样做控制,但控制思路全然不一样(Linux下控制硬件设备实质就是对相关设备虚拟文件的读写).研究了几天头都大了还是没有进展,网上 ...
- 【Hive】Hive 安装&使用基础
2 安装 2.1 参考 2.1.1 下载 2.1.1.1 https://mirrors.tuna.tsinghua.edu.cn/apache/hive/stable-2/ 2.1.2 安装指导 2 ...
- Scala和Java二种方式实战Spark Streaming开发
一.Java方式开发 1.开发前准备:假定您以搭建好了Spark集群. 2.开发环境采用eclipse maven工程,需要添加Spark Streaming依赖. 3.Spark streaming ...
- 如何在阿里云服务器搭建FTP服务器,在本地电脑连接并操作
首先你需要有一个阿里云的ECS服务器 并且开通了公网宽带(话说也不贵,开来玩玩还是可以的,第一次买会比较便宜,第二次买1M的宽带两天是九毛多吧~) 开通了宽带之后,ECS服务器就可以上网了 如果嫌弃阿 ...