Bellman_ford POJ 3259 Wormholes
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 41728 | Accepted: 15325 |
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.
就是当你过去的时候时间会倒退Ts。我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前的自己。
简化下,就是看图中有没有负权环。
#include <cstdio>
#define maxn 502
#define INF 0x3fffffff ///边
typedef struct Edge{
int u, v; ///起点, 终点
int weight; ///权值
}Edge; Edge edge[]; ///双向边,保存边的权值
int dist[maxn]; ///节点到原点的最小距离
int edgenum; ///边数
///插入边
void insert(int u, int v, int w)
{
edge[edgenum].u = u;
edge[edgenum].v = v;
edge[edgenum++].weight = w;
}
bool Bellman_Ford(int source, int nodenum) ///原点和结点个数
{
for(int i=; i < nodenum; ++i)
dist[i] = INF;
dist[source] = ;
for(int i=; i < nodenum; ++i){
for(int j=; j < edgenum; ++j)
{
if(dist[edge[j].v] > dist[edge[j].u] + edge[j].weight)///松弛计算
dist[edge[j].v] = dist[edge[j].u] + edge[j].weight;
}
}
bool flag = false;
/// 判断是否有负权环
for(int i=; i < edgenum; ++i){
if(dist[edge[i].v] > dist[edge[i].u] + edge[i].weight)
{
flag = true; ///有负权环
break;
}
}
return flag;
}
int main()
{
int T;
scanf("%d", &T);
while(T--)
{
int n, m, c;
scanf("%d%d%d", &n, &m, &c);
edgenum = ;
for(int i=; i<m; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
insert(u, v, w);
insert(v, u, w);
}
for(int i=; i<c; i++)
{
int u, v, w;
scanf("%d%d%d", &u, &v, &w);
insert(u, v, -w);
}
if(Bellman_Ford(, n))
printf("YES\n");
else
printf("NO\n");
}
return ;
}
Bellman_ford POJ 3259 Wormholes的更多相关文章
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- poj 3259 Wormholes
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
随机推荐
- 知识库总结mysql常用cmd命令
打开命令目录 打开D盘mysql目录 d: cd D:\Ampps\mysql\bin 常用操作 将mysql目录下bin目录中的mysql.exe放到C:\WINDOWS下,可以执行以下命令 连接: ...
- A Script Pro nginx URL重写规则无法播放MP4解决方法
I am using nginx and I have already add the line location /file/ { rewrite ^/-]+)/([-]+)/([^/]*)/([- ...
- python【第八篇】socket网络编程
内容大纲 1.socke基础 两个程序通过一个双向的通信连接实现数据的交换,这个连接的一端称为一个socket. 建 立网络通信连接至少要一对端口号(socket).socket本质是编程接口(API ...
- 由lib引发的血案(opencv找不函数问题)
在使用opencv中的函数时,连续两次遇到函数找不到的问题,第一次查时按照他人说的包含进一个头文件后,果真还真解决了:然而第二次在调用cvInpaint函数时包含进对应头文件,编译通过但运行不成功还是 ...
- iOS 取得CGimage字节数据的方法
通过我在网上搜索和总结,目前看来,我发现两种比较方便的方式. 1. CGImage -> CGDataProvider -> CFData -> xx * CGDataProvide ...
- C语言连接数据库
举个例子,连接SQL: // 打开数据库 strDBClass.Format(_T("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s;Jet ...
- BZOJ 1057 棋盘制作
Description 国际象棋是世界上最古老的博弈游戏之一,和中国的围棋.象棋以及日本的将棋同享盛名.据说国际象棋起源于易经的思想,棋盘是一个8*8大小的黑白相间的方阵,对应八八六十四卦,黑白对应阴 ...
- hdu 5074 Hatsune Miku
http://acm.hdu.edu.cn/showproblem.php?pid=5074 题意:给你一个的矩阵score[i][j],然后给你一个数列,数列中有一些是-1,代表这个数可以换成1~m ...
- iOS 颜色选择器 仿ps 调色板
前几天写东西,需要到调色板,自己网上搜了一下,好多都写得很麻烦,自己手敲了一个,使用很简单,飞虎不多说,上图,上代码,上使用教程,希望大家喜欢,(基于xcode7.0版本) 最后更新于15/12/14 ...
- SSH2中实例化不了Action的一个原因
<!-- 流程管理 --> <action name="flow_*" class="workFlowAction" method=" ...