POJ 3259 Wormholes(SPFA)
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)的更多相关文章
- Poj 3259 Wormholes(spfa判负环)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 42366 Accepted: 15560 传送门 Descr ...
- POJ 3259 Wormholes(SPFA+邻接表)
( ̄▽ ̄)" #include<iostream> #include<cstdio> #include<queue> #include<vector ...
- POJ 3259 Wormholes(Bellman-Ford)
http://poj.org/problem?id=3259 题意:有一些普通的洞和虫洞,每个洞都有经过的时间,虫洞的时间是负的,也就是时光倒流,问是否能回到出发时的时间. 思路: 贝尔曼-福特算法判 ...
- POJ 3259 Wormholes(最短路径,求负环)
POJ 3259 Wormholes(最短路径,求负环) Description While exploring his many farms, Farmer John has discovered ...
- POJ 3259 Wormholes (Bellman_ford算法)
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- poj 3259 Wormholes【spfa判断负环】
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 36729 Accepted: 13444 Descr ...
- POJ 3259 Wormholes(最短路&spfa正权回路)题解
题意:给你m条路花费时间(双向正权路径),w个虫洞返回时间(单向负权路径),问你他能不能走一圈回到原点之后,时间倒流. 思路:题意有点难看懂,我们建完边之后找一下是否存在负权回路,存在则能,反之不能. ...
- 解题报告:poj 3259 Wormholes(入门spfa判断负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
随机推荐
- 用canvas 绘制的饼状统计图、柱状统计图、折线统计图
canvas 绘制的饼状统计图 canvas 绘制的柱状统计图 canvas 绘制的折线统计图
- jquery 处理字符串 【转】
1,去掉空格 var txt=$.trim($("txt1").val()); 2,转为数字 txtNum=Number($.trim(txt)) + 1; var thi ...
- 跨域名设置cookie或获取cookie
可以使用jquery里面的ajax中的jsonp的方式来访问就可以了.代码如下: $.ajax({ url: 'your url', data: {'xx' : 'xx', 'xx2' : 'xx2' ...
- 文件上传下载struts2
上传方式1: // 保存上传的文件 public boolean saveFile(File file, String fileName) throws IOException { File newF ...
- Cisco IOS Basic CLI Configuration : Switch Port Command
Cisco IOS Basic CLI Configuration : Switch Port Command 1. Basic Switch>en Switch#conf t Enter c ...
- 一个订单相关的存储过程(MySQL)
BEGIN DECLARE currentDate VARCHAR(15) ;/*当前日期,有可能包含时分秒 */ DECLARE maxNo INT DEFAULT 0 ; /* 离现在最近的满足条 ...
- IIS支持PHP
1. 解压php-5.2.6.zip到D:\php5,找到php.ini-dist改名为php.ini并将它放到C:\WINDOWS目录下. 2. 将D:\ php5目录下的libmcrypt.dll ...
- MonoBehaviour
所有的Unity脚本都继承自MonoBehaviour这个类,它没有Main函数入口,采用了事件触发的模式,根据不同的事件响应不同的函数. void Start(): void Update():每一 ...
- LintCode-Fast Power
Calculate the an % b where a, b and n are all 32bit integers. Example For 231 % 3 = 2 For 1001000 % ...
- 对cnblogs.com用户体验的评价
一.对于cnblogs.com的用户体验我们先对以下问题进行回答: 1.你是什么样的用户, 有什么样的心理, 对cnblogs 的期望值是什么? 我们是正在学习软件工程课程的在校计算机专业大学生,在博 ...