Wormholes
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..NM (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: A single integer, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.

Output

Lines 1..F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

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 1, FJ cannot travel back in time. 
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.
 
T个样例,先输入N,M,W;N表示点的个数,M表示正权的边数,无向的,W表示负权边数,有向,
如果存在负回路则输出yes否则no
 
自己真是够脑残,两个代码找错找了 半天,尽然都是一些手残的原因
 
1:SPFA
若一个点最短路被改进的次数达到n ,则有负权环。可以用spfa算法判断图有无负权环
 #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(判断是否存在负回路)的更多相关文章

  1. POJ 3259 Wormholes(最短路,判断有没有负环回路)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 24249   Accepted: 8652 Descri ...

  2. POJ 3259 Wormholes(bellman_ford,判断有没有负环回路)

    题意:John的农场里field块地,path条路连接两块地,hole个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts.我们的任务是知道会不会在从某块地出发后又回来,看到了离开之前 ...

  3. Spfa 求含负权边的最短路 + 判断是否存在负权回路

    在Bellman-Ford算法之后,我们总算迎来了spfa算法,其实就如同堆优化Dijkstra算法之于朴素版Dijkstra算法,spfa算法仅仅是对Bellman-Ford算法的一种优化,但是在形 ...

  4. vijos1053 用spfa判断是否存在负环

    MARK 用spfa判断是否存在负环 判断是否存在负环的方法有很多, 其中用spfa判断的方法是:如果存在一个点入栈两次,那么就存在负环. 细节想想确实是这样,按理来说是不存在入栈两次的如果边权值为正 ...

  5. poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)

    题目链接:http://poj.org/problem?id=3259 题目大意:一个图,有n个顶点,其中有m条边是双向的且权值为为正,w条边是单向的且权值为负,判断途中是否存在负环,如果有输出YES ...

  6. 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 ...

  7. 使用spfa算法判断有没有负环

    如果存在最短路径的边数大于等于点数,就有负环 给定一个n个点m条边的有向图,图中可能存在重边和自环, 边权可能为负数. 请你判断图中是否存在负权回路. 输入格式 第一行包含整数n和m. 接下来m行每行 ...

  8. ZZUOJ 1199 大小关系(拓扑排序,两种方法_判断入度和dfs回路判断)

    /* 这道题如果按照度为0的节点来判断的时候,将度为0的节点和其相连的节点(度数并减去1) 从图中去掉,如果度为0的节点的个数为0个但是图中的节点没有都去掉的 时候那么说明 出现了回路!用这种方法必须 ...

  9. bellman-ford算法(判断有没有负环)

    #include <iostream> #include <vector> #include<string> #include<cstring> usi ...

随机推荐

  1. C# 无边框窗体边框阴影效果

    通过下面代码在构造函数中调用方法 SetShadow(); 即可实现无边框窗体的阴影效果了 需要添加命名空间 using System.Runtime.InteropServices; private ...

  2. [资料]常用Windows CMD指令

    1. 查找80端口开放情况 netstat -aon|findstr “80″ 2. 用netstat查询端口占用程序的PID,显示列表的最后一列就是占用程序的PID,然后再利用tasklist找到这 ...

  3. IBatisNet基础组件

    DomSqlMapBuilder DomSqlMapBuilder,其作用是根据配置文件创建SqlMap实例.可以通过这个组件从Stream, Uri, FileInfo, or XmlDocumen ...

  4. LeetCode:Palindrome Partitioning,Palindrome Partitioning II

    LeetCode:Palindrome Partitioning 题目如下:(把一个字符串划分成几个回文子串,枚举所有可能的划分) Given a string s, partition s such ...

  5. [MetaHook] R_SparkEffect

    By hzqst void R_SparkEffect(float *pos, int count, int velocityMin, int velocityMax) { efx.R_SparkSt ...

  6. ASP.NET MVC5 + EF6 入门教程 (6) View中的Razor使用

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-ef-6-get-started-model.html 上一节:ASP.NET MVC ...

  7. idea 重写toString()模板,转成json格式

    idea toString()模板,将对象toString()为json格式. 1. 2.点击新增 public java.lang.String toString() { final java.la ...

  8. mod mono xsp

    Mod_Mono 是Apache的一个扩展模块,使得apache支持asp.net. 该模块传递asp.net的请求到一个额外的程序 mod-mono-server(该程序是在安装xsp的时候自动安装 ...

  9. 【转】十分详细的xStream解析

    转自博文:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html xStream框架 xStream可以轻易的将Java对象和xml文 ...

  10. Android Studio修改项目的包名

    android studio的修改包名,没有有Eclipse环境中那么好操作.也可能你对Eclipse的操作比较熟悉,对Andoid Studio的操作还不太熟悉.在项目的开发中,你可能遇到需要更改模 ...