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 ...
随机推荐
- alert,confirm和prompt
1.警告消息框alertalert 方法有一个参数,即希望对用户显示的文本字符串.该字符串不是 HTML 格式.该消息框提供了一个“确定”按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说,用 ...
- FormsAuthentication详解
配置安全鉴别 鉴别是指鉴定来访用户是否合法的过程.ASP.NET Framework支持三种鉴别类型: Windows鉴别: NET Passport鉴别: Forms鉴别. 对于某一特定的应用程序, ...
- WebApi 消息拦截
最近公司要求对WebApi 实现服务端信息的监控(服务端信息拦截),由于本人之前没有做过这方便的相关项目所以在做的过程中也是困难重重,探索的过程也是非常痛苦的,好歹最终也算实现了这个功能.所以将这个分 ...
- NET Office 组件Spire
高效而稳定的企业级.NET Office 组件Spire 在项目开发中,尤其是企业的业务系统中,对文档的操作是非常多的,有时几乎给人一种错觉的是"这个系统似乎就是专门操作文档的" ...
- ajax中加上AntiForgeryToken防止CSRF攻击
经常看到在项目中ajax post数据到服务器不加防伪标记,造成CSRF攻击 在Asp.net Mvc里加入防伪标记很简单在表单中加入Html.AntiForgeryToken()即可. Html.A ...
- Tomcat简易安装指南
由于要学习activiti的使用,而activiti依赖于tomcat,所以下载了一个tomcat 7 的binary包,然后按照running.TXT中的描述来手动安装,下文主要是记录了在阅读run ...
- C#基础之Attribute
1.什么是Attribute 特性简单点理解就是为目标元素添加一些附加信息,这些附加信息我们可以在运行期间以反射的方式拿到.目标元素指的是程序集.模块.类.参数.属性等元素,附加信息指的是特性类中的成 ...
- .NET领域最为流行的IOC框架之一Autofac
一.前言 Autofac是.NET领域最为流行的IOC框架之一,微软的Orchad开源程序使用的就是Autofac,Nopcommerce开源程序也是用的Autofac. Orchad和Nopcomm ...
- 如何将Gate One嵌入我们的Web应用中
参考文档http://liftoff.github.io/GateOne/Developer/embedding.html 从https://github.com/liftoff/GateOne下载的 ...
- WCF 入门 (17)
前言 看的是入门视频,就希望培养一个学习的习惯吧. 前段时间看了微软的SurfaceBook的视频,被惊艳到了,但是我没钱买啊... 第17集 WCF中未经处理的异常 Unhandled except ...