http://poj.org/problem?id=3259

题意 : 农夫约翰农场里发现了很多虫洞,他是个超级冒险迷,想利用虫洞回到过去,看再回来的时候能不能看到没有离开之前的自己,农场里有N块地,M条路连接着两块地,W个虫洞,连接两块地的路是双向的,而虫洞是单向的,去到虫洞之后时间会倒退T秒,如果能遇到离开之前的自己就输出YES,反之就是NO。

样例解释 :

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

第一行中的2代表有两组测试数据,第一组测试数据中,3,3,1代表着有3块地,3条路,1个虫洞,下面三行代表着编号几到几的权值是几,最后一行代表的是通过虫洞去到目的地时间倒退了t秒

思路 : 这个题因为去到虫洞时间会倒退,而走农场之间相连的路又会花掉时间,明显是判断有没有负环,只要有负环,他只要一直走就能回到过去,典型的Bellman-Ford做法,本题采用的是spfa做法。

 #include <cstdio>
#include <cstring>
#include <queue>
#include<iostream>
using namespace std; const int maxn = ;
const int maxm = ;
const int oo = <<;
struct node
{
int u;
int v;
int w;
int next;
} edge[maxm];
int dis[maxn];
int cnt;
int N,M,T;
int head[maxn];
bool vis[maxn];
queue<int>qu;
int count[maxn];
void add(int u, int v, int w)
{
edge[cnt].u = u;
edge[cnt].v = v;
edge[cnt].w = w;
edge[cnt].next = head[u];
head[u] = cnt++;
} int spfa(int s)
{
memset(count,,sizeof(count));
for(int i = ; i <= N; i++)
{
dis[i] = oo;
vis[i] = false;
}
dis[s] = ;
qu.push(s);
vis[s] = true;
while(!qu.empty())
{
int u = qu.front();
qu.pop();
vis[u] = false;
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].v;
if(dis[u]+edge[i].w < dis[v])
{
dis[v] = dis[u]+edge[i].w;
if(++count[v] > N) return ;
if(!vis[v])
{
vis[v] = true;
qu.push(v);
}
}
}
}
return ;
} void init()
{
cnt = ;
memset(head, -, sizeof(head));
} int main()
{
int n ;
scanf("%d",&n);
while(n--)
{
init();
scanf("%d %d %d",&N,&M,&T);
int u, v, w;
for(int i = ; i <= M; i++)
{
scanf("%d %d %d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
}
for(int i = ; i <= T ; i++)
{
cin>>u>>v>>w;
add(u,v,(-*w));
}
int flag = spfa();
if(flag == )
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return ;
}

Bellman-Ford算法及其改进---SPFA算法可以参照下边这个博客

http://hi.baidu.com/laozhonggu/item/30a3f90d81b01fc975cd3c28

POJ 3259 Wormholes(SPFA)的更多相关文章

  1. Poj 3259 Wormholes(spfa判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...

  2. POJ 3259 Wormholes(SPFA+邻接表)

    ( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<queue> #include<vector ...

  3. POJ 3259 Wormholes(Bellman-Ford)

    http://poj.org/problem?id=3259 题意:有一些普通的洞和虫洞,每个洞都有经过的时间,虫洞的时间是负的,也就是时光倒流,问是否能回到出发时的时间. 思路: 贝尔曼-福特算法判 ...

  4. POJ 3259 Wormholes(最短路径,求负环)

    POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...

  5. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

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

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

  7. poj 3259 Wormholes【spfa判断负环】

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 36729   Accepted: 13444 Descr ...

  8. POJ 3259 Wormholes(最短路&spfa正权回路)题解

    题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...

  9. 解题报告:poj 3259 Wormholes(入门spfa判断负环)

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

随机推荐

  1. Ubuntu系统下允许Apache的mod_rewrite功能

    首先,使能apache的rewirte模块,在shell里输入下边的命令: sudo a2enmod rewrite 然后重启一下webserver使更改生效 sudo service apache2 ...

  2. linux系统目录架构

    /bin目录:可执行的二进制文件,shell命令(就是我们说的命令:cp ls ...),所有用户都有权执行. /boot目录:引导目录,整个操作系统启动所需的所有文件都在该目录下,其中最主要的就是v ...

  3. windows store app 读写图片

    using System; using System.Threading.Tasks; using System.Runtime.InteropServices.WindowsRuntime; usi ...

  4. 【转】IL编织 借助PostSharp程序集实现AOP

    ref:   C# AOP实现方法拦截器 在写程序的时候,很多方法都加了.日志信息.比如打印方法开始,方法结束,错误信息,等等. 由于辅助性功能的代码几乎是完全相同的,这样就会令同样的代码在各个函数中 ...

  5. js----方法是否加括号的问题

    在我们js编写程序的时候,我们会写很多函数然后调用它们,那么这些函数调用的时候什么时候加()什么时候不加()?记住以下几个要点. (1)函数做参数时都不要括号. function fun(e) { a ...

  6. How to check Windows 7 OS is permanently activated?[Windows 7]

    Press Windows + R, then you can enter : slmgr.vbs -xpr

  7. 在HTML中通过jQuery设置列表项符号

    在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...

  8. Linux nmon 监控工具使用

    Linux 系统下监控指标及指标查看 一.工具介绍     Linux 系统下资源监控使用nmon 工具.它可以帮助在一个屏幕上显示所有重要的性能优化信息,并动态地对其进行更新且并不会消耗大量的CPU ...

  9. orcale 修改字段属性

    有些时候,因为没能预料到一些情况的变化,需要修改字段的类型.如果是varchar型,直接增加长度是可以的,但是如果需要修改成其他类型就不能这么做了. 思路:1.增加一个临时列,把需要修改的那个字段的数 ...

  10. 【quartz】 理论知识

    属性的介绍 1.调度器属性:分别设置调度器的实例名(instanceName) 和实例 ID (instanceId).属性 org.quartz.scheduler.instanceName 可以是 ...