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. CICS&&XA

    CICS (Customer Information Control System) 是IBM 公司的强大主机交易服务器.集成平台,在全球C.C++.COBOL等交易中间件市场上占有绝大多数客户.CI ...

  2. Window Phone 8开发问题反思

    项目开发有段时间了,进入了阶段测试.然而在测试过程中bug连连不断,在抱怨产品需求的坑爹.不合理之外,我也一直在反思为什么会出现这么多Bug. 首先,由于项目开发的两个人都是新手,在刚刚认识MVVM架 ...

  3. FPGA统计摄像头输出-基于MD9T112

    FPGA HDL源程序 FPGA统计摄像头的输出像素,窗口尺寸等等 //---------------------------------------------------------------- ...

  4. 转 XMLHttpRequest().readyState的五种状态详解

    转 http://javathinker.blog.ccidnet.com/blog-htm-itemid-1262479-do-showone-type-blog-uid-36384.html 在& ...

  5. 基础学习总结(七)--子线程及Handler

    使用子线程获取网络图片1.采用httpUrlConnection直连方式获取图片2.采用子线程方式获取 <LinearLayout xmlns:android="http://sche ...

  6. Javascript(JS)中的大括号{}和中括号[]详解

    一.{ } 大括号,表示定义一个对象,大部分情况下要有成对的属性和值,或是函数. 如:var LangShen = {"Name":"Langshen",&qu ...

  7. 关于查看Android系统源码【Written By KillerLegend】

    可能你会想下载Android系统源码,但是我不知道你会看多少系统的源码,如果你对源码只是偶尔看一次的话,推荐你在线看Android的系统源码,下面提供几种查看android系统源码的方法. 1:打开这 ...

  8. python 数字、字符串、列表常用函数

    一.数字的标准类型: cmp():比较两个数的大小:返回值(-1,0,1). str():数字转化成字符串. type():返回数字类型. 转换工厂函数: int(obj,base=10) long( ...

  9. Log Parser 2.2

    Log Parser 2.2 是一个功能强大的通用工具,它可对基于文本的数据(如日志文件.XML 文件和 CSV 文件)以及 Windows 操作系统上的重要数据源(如事件日志.注册表.文件系统和 A ...

  10. PAT 字符串-02 删除字符串中的子串

    /* 2 *PAT 字符串-02 删除字符串中的子串 3 *2015-08-09 4 作者:flx413 5 */ #include<stdio.h> #include<string ...