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.

  就是求是否存在负环,这样的话一定能够时间倒退。。。

代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue> using namespace std; const int INF=10e8;
const int MaxN=; struct Edge
{
int v,cost; Edge(int _v,int _cost):v(_v),cost(_cost) {}
}; vector <Edge> E[MaxN];
bool vis[MaxN];
int couNode[MaxN]; bool SPFA(int lowcost[],int n,int start)
{
queue <int> que;
int u,v,c;
int len; for(int i=;i<=n;++i)
{
lowcost[i]=INF;
vis[i]=;
couNode[i]=;
}
vis[start]=;
couNode[start]=;
lowcost[start]=; que.push(start); while(!que.empty())
{
u=que.front();
que.pop(); vis[u]=;
len=E[u].size(); for(int i=;i<len;++i)
{
v=E[u][i].v;
c=E[u][i].cost; if(lowcost[u]+c<lowcost[v])
{
lowcost[v]=lowcost[u]+c; if(!vis[v])
{
vis[v]=;
++couNode[v];
que.push(v); if(couNode[v]>=n)
return ;
}
}
}
} return ;
} inline void addEdge(int u,int v,int c)
{
E[u].push_back(Edge(v,c));
} int ans[MaxN]; int main()
{
int T;
int N,M,W;
int a,b,c; scanf("%d",&T); while(T--)
{
scanf("%d %d %d",&N,&M,&W); for(int i=;i<=N;++i)
E[i].clear(); for(int i=;i<=M;++i)
{
scanf("%d %d %d",&a,&b,&c); addEdge(a,b,c);
addEdge(b,a,c);
} for(int i=;i<=W;++i)
{
scanf("%d %d %d",&a,&b,&c); addEdge(a,b,-c);
} if(SPFA(ans,N,))
printf("NO\n");
else
printf("YES\n");
} return ;
}

(简单) POJ 3259 Wormholes,SPFA判断负环。的更多相关文章

  1. POJ 3259 Wormholes ( SPFA判断负环 && 思维 )

    题意 : 给出 N 个点,以及 M 条双向路,每一条路的权值代表你在这条路上到达终点需要那么时间,接下来给出 W 个虫洞,虫洞给出的形式为 A B C 代表能将你从 A 送到 B 点,并且回到 C 个 ...

  2. POJ 3259 Wormholes(SPFA判负环)

    题目链接:http://poj.org/problem?id=3259 题目大意是给你n个点,m条双向边,w条负权单向边.问你是否有负环(虫洞). 这个就是spfa判负环的模版题,中间的cnt数组就是 ...

  3. POJ3259 Wormholes(SPFA判断负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  4. Wormholes POJ 3259(SPFA判负环)

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

  5. POJ 3259 Wormholes 最短路+负环

    原题链接:http://poj.org/problem?id=3259 题意 有个很厉害的农民,它可以穿越虫洞去他的农场,当然他也可以通过道路,虫洞都是单向的,道路都是双向的,道路会花时间,虫洞会倒退 ...

  6. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  7. POJ 3259 Wormholes( bellmanFord判负环)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36425   Accepted: 13320 Descr ...

  8. POJ 3259 Wormholes【最短路/SPFA判断负环模板】

    农夫约翰在探索他的许多农场,发现了一些惊人的虫洞.虫洞是很奇特的,因为它是一个单向通道,可让你进入虫洞的前达到目的地!他的N(1≤N≤500)个农场被编号为1..N,之间有M(1≤M≤2500)条路径 ...

  9. [poj3259]Wormholes(spfa判负环)

    题意:有向图判负环. 解题关键:spfa算法+hash判负圈. spfa判断负环:若一个点入队次数大于节点数,则存在负环.  两点间如果有最短路,那么每个结点最多经过一次,这条路不超过$n-1$条边. ...

  10. Wormholes POJ - 3259 spfa判断负环

    //判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...

随机推荐

  1. Server的Transfer和Response的Redirect

    在实现页面跳转的时候,有些人喜欢用Response.Redirect,而有些人则喜欢用Server.Transfer.大部分时间似乎这两种方法都可以实现相同的功能,那究竟有区别吗? 查了些文档,发现两 ...

  2. UVA 10277 Boastin' Red Socks

    #include <iostream> #include<cstdio> #include<cstring> using namespace std; unsign ...

  3. weak和assign区别

    weak比assign多了一个功能,当对象消失后自动把指针变成nil haofanazenmeban[4002:406590] controller:<SecondViewController: ...

  4. eclipse背景颜色修改插件color theme

    对于长时间编码的程序猿来说,这个插件可以说是福音了. 安装方式有两种: 1.可以直接在eclipse的eclipse marketplace 那里搜索安装 2.可以在Help->Install ...

  5. Objective-C相关Category的收集

    Objective-C相关Category的收集 Categories是给你得不到源码的classes增加功能的一种方法.这个页面收集一些相关的Category,并且持续更新,你可以订阅关注.作者是F ...

  6. margin:0 auto

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  7. HDU1162-Eddy's picture(最小生成树)

    Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...

  8. 转:WebDriver(Selenium2) 处理可能存在的JS弹出框

    在自动化测试过程中,有些情况下我们会遇到一些潜在的Javascript弹出框.(即某些条件下才会出现,不是固定出现),然后如果当这种弹出框出现,我们没有加以处理,WebDriver将无法进行下一步的操 ...

  9. PAT1014

    Suppose a bank has N windows open for service. 一个银行有N个服务的窗口 There is a yellow line in front of the w ...

  10. EM阅读资料

    1,从最大似然到EM算法浅解 2,(EM算法)The EM Algorithm 3,数据挖掘十大算法----EM算法(最大期望算法) (番外)最大后验估计(MAP)