Wormholes poj 3259 (bellman-floyd+spfa)
2016-06-27 12:52
459 查看
问题描述
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 toF (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.
输入
Line 1: A single integer, F. F farm descriptions follow.
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 fromS to
E that also moves the traveler back T seconds.
输出
Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
样例输入
样例输出
裸的bellman。
spfa
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
using namespace std;
const int INF=999999999;
int n,m,w;
struct Edge
{
int v,w,next;
}edge[6000];
int dis[501],inq[501],head[501],used[501];
int top;
void init()
{
for(int i=0;i<=n;i++)
{
dis[i]=INF;
inq[i]=0;
head[i]=-1;
used[i]=0;
}
dis[1]=0;
top=1;
}
void add(int a,int b,int c)
{
edge[top].v=b;
edge[top].w=c;
edge[top].next=head[a];
head[a]=top++;
}
bool spfa()
{
queue<int> que;
que.push(1);
used[1]++;
while(!que.empty())
{
int u=que.front();que.pop();
inq[u]=0;
for(int s=head[u];s!=-1;s=edge[s].next)
{
if(dis[edge[s].v]>dis[u]+edge[s].w)
{
dis[edge[s].v]=dis[u]+edge[s].w;
if(!inq[edge[s].v])
{
inq[edge[s].v]=1;
que.push(edge[s].v);
if(++used[edge[s].v]>n-1)return false;
que.push(edge[s].v);
}
}
}
}
return true;
}
int main()
{
int f;
scanf("%d",&f);
while(f--)
{
scanf("%d %d %d",&n,&m,&w);
init(); int a,b,c;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(int i=1;i<=w;i++)
{
scanf("%d %d %d",&a,&b,&c);
add(a,b,-c);
}
if(!spfa())printf("YES\n");
else
printf("NO\n");
}
}
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 toF (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.
输入
Line 1: A single integer, F. F farm descriptions follow.
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 fromS to
E that also moves the traveler back T seconds.
输出
Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
样例输入
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
样例输出
NO YES
裸的bellman。
#include <iostream> #include <cstdio> #include <cstring> #include <map> using namespace std; const int INF=999999999; int dist[502]; struct Edge { int u,v,cost; }edge[6000]; int n,m,w;int top; bool bellman() { for(int i=1;i<=n;i++) dist[i]=INF; dist[1]=0; for(int i=0;i<n-1;i++) for(int j=1;j<=top;j++) { int u=edge[j].u; int v=edge[j].v; if(dist[v]>dist[u]+edge[j].cost) { if(i==n-2) return false; dist[v]=dist[u]+edge[j].cost; } } return true; } void add(int a,int b,int c) { edge[top].u=a; edge[top].v=b; edge[top++].cost=c; } int main() { int f; scanf("%d",&f); while(f--) { top=1; scanf("%d %d %d",&n,&m,&w); for(int i=1;i<=m;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); add(a,b,c); add(b,a,c); } for(int i=1;i<=w;i++) { int a,b,c; scanf("%d %d %d",&a,&b,&c); add(a,b,-c); } if(bellman())printf("NO\n"); else printf("YES\n"); } }
spfa
#include <iostream>
#include <cstdio>
#include <cstring>
#include <map>
#include <vector>
#include <queue>
using namespace std;
const int INF=999999999;
int n,m,w;
struct Edge
{
int v,w,next;
}edge[6000];
int dis[501],inq[501],head[501],used[501];
int top;
void init()
{
for(int i=0;i<=n;i++)
{
dis[i]=INF;
inq[i]=0;
head[i]=-1;
used[i]=0;
}
dis[1]=0;
top=1;
}
void add(int a,int b,int c)
{
edge[top].v=b;
edge[top].w=c;
edge[top].next=head[a];
head[a]=top++;
}
bool spfa()
{
queue<int> que;
que.push(1);
used[1]++;
while(!que.empty())
{
int u=que.front();que.pop();
inq[u]=0;
for(int s=head[u];s!=-1;s=edge[s].next)
{
if(dis[edge[s].v]>dis[u]+edge[s].w)
{
dis[edge[s].v]=dis[u]+edge[s].w;
if(!inq[edge[s].v])
{
inq[edge[s].v]=1;
que.push(edge[s].v);
if(++used[edge[s].v]>n-1)return false;
que.push(edge[s].v);
}
}
}
}
return true;
}
int main()
{
int f;
scanf("%d",&f);
while(f--)
{
scanf("%d %d %d",&n,&m,&w);
init(); int a,b,c;
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
for(int i=1;i<=w;i++)
{
scanf("%d %d %d",&a,&b,&c);
add(a,b,-c);
}
if(!spfa())printf("YES\n");
else
printf("NO\n");
}
}
相关文章推荐
- ipad和iphone的适配
- Java基础教程
- 明星“真空”出镜上直播,这"戏"该如何继续演?
- IOS学习之蓝牙4.0 BLE
- UVa1601万圣节后的早晨
- 超越之MongDB系列教程(六) MongDB的查询
- Swift 使用CollectionView 实现图片轮播封装就是这样简单
- C++中protected的访问权限
- TomCat的配置和优化
- 【转载】浅谈HTTP中Get与Post的区别
- Sort Transformed Array
- vxworks的启动任务taskSpawn
- 添物零基础到大型全栈架构师 不花钱学计算机及编程(预备篇)- 计算机基础
- 添物零基础到大型全栈架构师 不花钱学计算机及编程(预备篇)- 计算机基础
- Android网络请求框架剖析一、Okhttp
- Android EventBus实战及源码解析 (一)
- mac中Eclipse的快捷键
- 【NOIP2013模拟联考5】军训(training)
- win7系统怎么判断是否需要手动安装KB3161647补丁?
- osg 骨骼绘制 注意事项