POJ-3259 Wormholes(判负环,spfa算法)

Posted wx62a8776062513

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ-3259 Wormholes(判负环,spfa算法)相关的知识,希望对你有一定的参考价值。

题干:

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 FJs 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 Tseconds to traverse. Two fields might be connected by more than one path. 
Lines M+2.. MW+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.

题目大意:


有若干个虫洞,给出了若干普通路径和其所用时间以及虫洞的路径和其倒回的时间,现问你能否回到出发之前的时间,注意普通路径是双向的,虫洞是单向的。

解题报告:


由题目所给信息已经可以构建一张完整的图了,然后进一步理解题目的意思其实是这张图是否存在负环,因此使用Bellman_Ford或者spfa即可。

AC代码:(邻接表储存图)(266ms)

#include<cstdio>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int n,cnt;
const int MAX = 505;
const int INF = 0x3f3f3f3f;
int dis[MAX],maze[MAX][MAX],cntt[MAX],head[MAX];
bool vis[MAX];
struct Edge
int to,w,ne;
Edge()//没有此构造函数不能写 node t 这样
Edge(int to,int w,int ne):to(to),w(w),ne(ne)//可以写node(pos,cost)这样

e[200000 + 5];//数组别开小了
void add(int u,int v,int w)
e[cnt].to = v;
e[cnt].w = w;
e[cnt].ne = head[u];
head[u] = cnt++;

bool spfa(int s)
dis[s]=0; vis[s]=1; //队列初始化,s为起点
int v;
queue<int > q;
q.push(s);
while (!q.empty()) //队列非空
v=q.front(); //取队首元素
q.pop();
vis[v]=0; //释放队首结点,因为这节点可能下次用来松弛其它节点,重新入队
for(int i=head[v]; i!=-1; i=e[i].ne) //对所有顶点

if (dis[e[i].to]>dis[v]+e[i].w)
dis[e[i].to] = dis[v]+e[i].w; //修改最短路
if (vis[e[i].to]==0) //如果扩展结点i不在队列中,入队

cntt[e[i].to]++;
vis[e[i].to]=1;
q.push(e[i].to);
if(cntt[e[i].to] >=n) return true;





return false;


void init()
cnt = 0;
memset(vis,0,sizeof(vis));
memset(maze,0,sizeof(maze));
memset(e,0,sizeof(e));
memset(dis,INF,sizeof(dis));
memset(cntt,0,sizeof(cntt));
memset(head,-1,sizeof(head));

int main()

int t;
int M,W,u,v,w;
cin>>t;
while(t--)
init();
scanf("%d%d%d",&n,&M,&W);
for(int i = 1; i<=M; i++)
scanf("%d%d%d",&u,&v,&w);
add(u,v,w);add(v,u,w);

for(int i = 1; i<=W; i++)
scanf("%d%d%d",&u,&v,&w);
add(u,v,-w);

cntt[1] =1;
if(spfa(1)) printf("YES\\n");
else printf("NO\\n");


return 0 ;

AC代码2:(邻接矩阵)(需要判重边!)(1079ms)

#include<cstdio>
#include<algorithm>
#include<iostream>
POJ 3259 Wormholes(SPFA判负环)

「POJ3259」Wormholes - SPFA判负环

POJ-3259 Wormholes(判负环,spfa算法)

POJ #3259 Wormholes 判负环

POJ 3259 Wormholes spfa 判负环

POJ3259 - Wormholes - 判负环模板题