Wormholes(SPFA+Bellman)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 36860 | Accepted: 13505 |
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.
Input
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2..M+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2..M+W+1 of each farm: Three space-separated numbers (S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
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
题解:
这道题意思是这个人的农田里有道路,还有虫洞,道路是双向的,虫洞单向,他喜欢虫洞旅行,想要从起点再回到起点时间在出发的时间之前,经过虫洞时间倒退;
做这道题其实就是判断最短路有没有负环的问题;因为只要有负环,就会无限循环,到起点自然就时间倒退了;
代码:SPFA
#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=*;
int dis[MAXN],vis[MAXN],used[MAXN],head[MAXM];
int N,W,M,en,flot;
queue<int>dl;
struct Edge{
int from,to,value,next;
};
Edge edg[MAXM];
void initial(){
memset(dis,INF,sizeof(dis));
memset(vis,,sizeof(vis));
memset(used,,sizeof(used));
memset(head,-,sizeof(head));
while(!dl.empty())dl.pop();
en=;flot=;
}
void print(){
if(flot)puts("YES");
else puts("NO");
}
void add(int u,int v,int w){
Edge E={u,v,w,head[u]};
edg[en]=E;
head[u]=en++;
}
void SPFA(int sx){
dis[sx]=;vis[sx]=;dl.push(sx);
used[sx]++;
while(!dl.empty()){
int k=dl.front();
dl.pop();
vis[k]=;
if(used[k]>N){
flot=;
break;
}
for(int i=head[k];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[k]+edg[i].value<dis[v]){
dis[v]=dis[k]+edg[i].value;
if(!vis[v]){
vis[v]=;
dl.push(v);
used[v]++;
if(used[v]>N){
flot=;return ;
}
}
}
}
}
}
void get(){
int F,a,b,c;
scanf("%d",&F);
while(F--){
initial();
scanf("%d%d%d",&N,&M,&W);
while(M--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
while(W--){
scanf("%d%d%d",&a,&b,&c);
add(a,b,-c);
}
SPFA();
print();
}
}
int main(){
get();
return ;
}
Bellman:
#include<stdio.h>
#include<string.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
const int MAXM=;
int dis[MAXN];
struct Edge{
int u,v,w;
};
Edge edg[MAXM];
int N,M,W,top;
bool Bellman(int sx){
int u,v,w;
memset(dis,INF,sizeof(dis));
dis[sx]=;
for(int i=;i<=N;i++){
for(int j=;j<top;j++){
u=edg[j].u;v=edg[j].v;w=edg[j].w;
if(dis[u]+w<dis[v])dis[v]=dis[u]+w;
}
}
for(int i=;i<top;i++){
u=edg[i].u;v=edg[i].v;w=edg[i].w;
if(dis[u]+w<dis[v])return false;
}
return true;
}
int main(){
int F;
int a,b,c;
scanf("%d",&F);
while(F--){
top=;
scanf("%d%d%d",&N,&M,&W);
while(M--){
scanf("%d%d%d",&a,&b,&c);
edg[top].u=a;edg[top].v=b;edg[top++].w=c;
edg[top].u=b;edg[top].v=a;edg[top++].w=c;
}
while(W--){
scanf("%d%d%d",&a,&b,&c);
edg[top].u=a;edg[top].v=b;edg[top++].w=-c;
}
if(Bellman())puts("NO");
else puts("YES");
}
return ;
}
Wormholes(SPFA+Bellman)的更多相关文章
- poj 3259 Wormholes spfa算法
点击打开链接 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 25582 Accepted: 9186 ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 一个人的旅行(floyd+dijskra+SPFA+Bellman)
一个人的旅行 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Subm ...
- 最短路(dijskra+SPFA+Bellman)
最短路 Time Limit : 5000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submissio ...
- ZOJ 2770 Burn the Linked Camp(spfa&&bellman)
//差分约束 >=求最长路径 <=求最短路径 结果都一样//spfa#include<stdio.h> #include<string.h> #include< ...
- POJ 1151 Wormholes spfa+反向建边+负环判断+链式前向星
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 49962 Accepted: 18421 Descr ...
- POJ3259 :Wormholes(SPFA判负环)
POJ3259 :Wormholes 时间限制:2000MS 内存限制:65536KByte 64位IO格式:%I64d & %I64u 描述 While exploring his many ...
- POJ3259 Wormholes(SPFA判断负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- POJ3259 Wormholes —— spfa求负环
题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
随机推荐
- javascript模式
http://developer.51cto.com/art/201212/372725.htm http://justjavac.com/javascript/2012/12/14/model-vi ...
- WorkFlow4.0--入门到精通系列-专题索引
原文地址:http://www.cnblogs.com/hegezhou_hot/archive/2011/06/15/2081405.html 开篇 首先.非常感谢大家的支持和厚爱,才有了这个系列, ...
- Binary Tree Level Order Traversal - leetcode - java
Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, ...
- JQuery中html()方法的注意事项
.html方法当不传参数时用来获取元素的html内容, return this[0] && this[0].nodeType === 1 ? this[0].innerHTML.rep ...
- UIButton 设置字体大小
btn.frame = CGRectMake(x, y, width, height); [btn setTitle: @"search" forState: UIControlS ...
- UVA 1617 Laptop
题意: 有n条长度为1的线段,确定他们的起点,必须是整数,使得第i条线段在[ri,di]之间.最后输出空隙的最小值 分析: 原始数据排序,排序的规则是先按照右端点排序,右端点相同的情况下,再按照左端点 ...
- 【MVC】过滤器
APS.NET MVC中(以下简称“MVC”)的每一个请求,都会分配给相应的控制器和对应的行为方法去处理,而在这些处理的前前后后如果想再加一些额外的逻辑处理.这时候就用到了过滤器. MVC支持的过滤器 ...
- String.Empty、string=”” 和null的区别
String.Empty是string类的一个静态常量: String.Empty和string=””区别不大,因为String.Empty的内部实现是: 1 2 3 4 5 6 7 8 9 10 1 ...
- Python基础:绑定和方法调用
首先,方法仅仅是类内部定义的函数,也就是说,方法是类属性而不是实例属性. 其次方法有两种被调用的方式:调用绑定的方法和调用未绑定的方法. 当存在一个实例时,方法才被认为绑定到了那个实例上,没有实例时方 ...
- js,jQuery数组常用操作小结
一.js中数组常用操作小结 (1) shift:删除原数组第一项,并返回删除元素的值:如果数组为空则返回undefined var a = [1,2,3,4,5]; var b = a.shift() ...