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 思路:求有无负环,起点随意选就可以,因为目的只是找出有没有负环,有了负环就可以让时间一直回退,那么一定能回到当初,这里 ...
随机推荐
- [POI2014]Criminals
题目大意: 给你一个长度为$n(n\le10^6)$的颜色序列,其中每个颜色互不相同.两个人$A$和$B$分别从某个点出发从左往右.从右往左任意地选择颜色,然后在中间的某一点相遇.分别给出两人所选择的 ...
- jsp笔记3(内置对象)
jsp脚本中的9个内置对象: 1.application:javax.servlet.ServletContext的实例对象,该实例对象代表jsp所属的web应用本身,可用于在jsp页面或Servle ...
- 集合框架(Collection和Collections的区别)
1.Collection: java.util.Collection 是一个集合接口. 它提供了对集合对象进行基本操作的通用接口方法.Collection接口在Java 类库中有很多具体的实现.Col ...
- JAVA常见算法题(七)
package com.xiaowu.demo; /** * 输入一行字符,分别统计出其中英文字母.空格.数字和其它字符的个数. * * @author WQ * */ public class De ...
- ylb: 数据库操作方法基础
ylbtech-SQL Server:SQL Server-数据库操作方法基础 数据库操作方法基础. ylb: 数据库操作方法基础 返回顶部 ----------试图操作(view)--------- ...
- ElasticSearch的Rest的访问方式查询总量
由于安装ElasticSearch插件会影响ES的性能,所以会尽量减少ES的插件安装 可以通过ElasticSearch-Sql插件 然后将生成的执行参数拷贝 { "query": ...
- python实现将文件夹内所有txt文件合并成一个文件
新建一个文件夹命名为yuliao,把所有txt文件放进去就ok啦!注意路径中‘/’,windows下路径不是这样. #coding=utf-8 import os #获取目标文件夹的路径 filed ...
- 一起学Netty(一)之 Hello Netty
一起学Netty(一)之 Hello Netty 学习了:https://blog.csdn.net/linuu/article/details/51306480
- spring自动装配(No qualifying bean )
No qualifying bean of type [com.wfj.service.cms.main.ChannelMng] found for dependency: expected at l ...
- win10 如何配置 java jdk1.8环境变量(2017.8.17 )jdk1.8.0_144
win10 如何配置 java jdk 环境变量 2017.8.17 本篇还适用于 windows server 2012. windows server 2014+ 一.安装 下载 jdk 64位 ...