POJ3259Wormholes(判断是否存在负回路)
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 38300 | Accepted: 14095 |
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>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;
const int INF = ;
const int MAX = + ;
struct point
{
int e,w;
};
int F,M,N,W;
vector<point> g[MAX];
int updatetimes[MAX],dist[MAX];
/*
int spfa(int v)
{
for( int i = 1; i <= N; ++i)
dist[i] = INF;
dist[v] = 0;
queue<int> que;
que.push(v);
memset(updatetimes ,0,sizeof(updatetimes));
while( !que.empty()) {
int s = que.front();
que.pop();
for( int i = 0;i < g[s].size(); ++i) {
int e = g[s][i].e;
if( dist[e] > dist[s] + g[s][i].w ) {
dist[e] = dist[s] + g[s][i].w;
que.push(e);
++updatetimes[e];
if( updatetimes[e] >= N)
return true;
}
}
}
return false;
}
*/
int spfa(int v)
{
for(int i = ; i <= N; i++)
dist[i] = INF;
dist[v] = ;
queue<int> que;
que.push(v);
memset(updatetimes,,sizeof(updatetimes));
while(que.size())
{
int s = que.front();
que.pop();
int len = g[s].size();
for(int i = ; i < g[s].size(); i++)
{
int e = g[s][i].e;
if(dist[e] > dist[s] + g[s][i].w)
{
dist[e] = dist[s] + g[s][i].w;
que.push(e);
++updatetimes[e];
if(updatetimes[e] >= N)
return true;
}
}
}
return false;
} int main()
{
scanf("%d", &F);
while(F--)
{
scanf("%d%d%d", &N,&M,&W);
for(int i = ; i < MAX; i++)
g[i].clear();
point edge;
for(int i = ; i < M; i++)
{
int s,e,w;
scanf("%d%d%d", &s,&e,&w);
edge.e = e;
edge.w = w;
g[s].push_back(edge);
edge.e = s;
g[e].push_back(edge);
}
for(int i = ; i < W; i++)
{
int s,e,w;
scanf("%d%d%d", &s,&e,&w);
edge.e = e;
edge.w = (-) * w;
g[s].push_back(edge);
}
if(spfa())
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}
return ;
}
2.Ballem-ford
Bellman-Ford:算法核心就是对每个点更新一下dist[](离原点的距离),怎么更新一个点呢,通过枚举每个边就可以了,所以每次把所有的边枚举一遍,专业术语 叫做<松弛操作>就可以确定一个点的dist,除了原点一共需要N-1个点,所以套个循环
至于判断是否存在负环呢,就在更新完所有dist,然后在枚举一下每个边,看看是否通过在增加一个边能让dist再减少,如果可以的话那就是存在负回路,因为在前面我们已经更新到最短的路径了。
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
const int INF = ;
struct Edge
{
int s,e,w;
};
vector<Edge> edge;
int dist[ + ];
int N,W,M; bool Ballem_ford(int v)
{
for(int i = ; i <= N; i++)
{
dist[i] = INF;
}
dist[v] = ;
int len = edge.size();
for(int i = ; i < N; i++)
{
for(int j = ; j < len; j++)
{
int s = edge[j].s;
int e = edge[j].e;
if(dist[e] > dist[s] + edge[j].w) //把j写成了i,真是无语
dist[e] = dist[s] + edge[j].w;
}
}
for(int i = ; i < len; i++)
{
int s = edge[i].s;
int e = edge[i].e;
if(dist[e] > dist[s] + edge[i].w)
return true;
}
return false;
}
int main()
{
int F;
scanf("%d", &F);
while(F--)
{
scanf("%d%d%d",&N,&M,&W);
edge.clear();
Edge point,temp;
for(int i = ; i < M; i++)
{
scanf("%d%d%d",&point.s,&point.e,&point.w);
edge.push_back(point);
temp.s = point.e;
temp.e = point.s;
temp.w = point.w;
edge.push_back(temp);
}
for(int i = ; i < W; i++)
{
scanf("%d%d%d", &point.s,&point.e,&point.w);
point.w = (-) * point.w;
edge.push_back(point);
}
if(Ballem_ford() == true)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
POJ3259Wormholes(判断是否存在负回路)的更多相关文章
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)
题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...
- Spfa 求含负权边的最短路 + 判断是否存在负权回路
在Bellman-Ford算法之后,我们总算迎来了spfa算法,其实就如同堆优化Dijkstra算法之于朴素版Dijkstra算法,spfa算法仅仅是对Bellman-Ford算法的一种优化,但是在形 ...
- vijos1053 用spfa判断是否存在负环
MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...
- poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)
题目链接:http://poj.org/problem?id=3259 题目大意:一个图,有n个顶点,其中有m条边是双向的且权值为为正,w条边是单向的且权值为负,判断途中是否存在负环,如果有输出YES ...
- POJ No 3259 Wormholes Bellman-Ford 判断是否存在负图
题目:http://poj.org/problem?id=3259 题意:主要就是构造图, 然后判断,是否存在负图,可以回到原点 /* 2 3 3 1 //N, M, W 1 2 2 1 3 4 2 ...
- 使用spfa算法判断有没有负环
如果存在最短路径的边数大于等于点数,就有负环 给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你判断图中是否存在负权回路. 输入格式 第一行包含整数n和m. 接下来m行每行 ...
- ZZUOJ 1199 大小关系(拓扑排序,两种方法_判断入度和dfs回路判断)
/* 这道题如果按照度为0的节点来判断的时候,将度为0的节点和其相连的节点(度数并减去1) 从图中去掉,如果度为0的节点的个数为0个但是图中的节点没有都去掉的 时候那么说明 出现了回路!用这种方法必须 ...
- bellman-ford算法(判断有没有负环)
#include <iostream> #include <vector> #include<string> #include<cstring> usi ...
随机推荐
- javascript中的array对象属性及方法
Array 对象 Array 对象用于在单个的变量中存储多个值. 创建 Array 对象的语法: new Array(); new Array(size); new Array(element0, e ...
- 谁可以说出HashMap和HashSet的相同点和不同点。
谁可以说出HashMap和HashSet的相同点和不同点. 2011-11-15 20:46ruoshui_t | 浏览 20310 次 Perl 2011-11-15 21:17 #知道行家专业创 ...
- 09Spring_AOP介绍和java本身的动态代理以及cglib代理
Aspect Oriented Programming 面向切面编程 1. 业界 AOP 实际上 OOP (面向对象编程 ) 延伸 ---- OOP编程语言. AOP设计思想,下面给出一张AOP的设 ...
- 键盘事件keydown、keypress、keyup随笔整理总结(摘抄)
原文1:http://www.cnblogs.com/silence516/archive/2013/01/25/2876611.html 原文2:http://www.cnblogs.com/leo ...
- R 分类进行数值处理
主要Mark一下R程序中,分类进行数值计算的情况. 1.aggregate函数 有数据框case,列名分别a,b,c,d,e,f (1)根据一列对另一列求和:根据a,对d求和 sum1 <- a ...
- Dungeon Game ——动态规划
The demons had captured the princess (P) and imprisoned her in the bottom-right corner of a dungeon. ...
- C语言 后缀自增的优先级详解
// ++ 后缀自增与取地址& ,提领 * (指针里的操作符)的优先级比较 #include<stdio.h> #include<stdlib.h> #include& ...
- WebGIS空间数据请求访问机制
通用的WebGIS空间数据请求访问机制如下图所示: 图1 WebGIS空间数据请求访问机制
- 使用Spring.net中对Ado.net的抽象封装来访问数据库
使用Spring.net中对Ado.net的抽象封装来访问数据库 Spring.NET是一个应用程序框架,其目的是协助开发人员创建企业级的.NET应用程序.它提供了很多方面的功能,比如依赖注入 ...
- Android 动画之TranslateAnimation应用详解
TranslateAnimation比较常用,比如QQ,网易新闻菜单条的动画,就可以用TranslateAnimation实现, 通过TranslateAnimation(float fromXDel ...