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 ...
随机推荐
- 2014第7周三初识CouchBase
今天主要还是完善需求,然后提交评审流程,尽可能不纠结一些细节问题后发现自己速度更快了,或许这才是最好的顺序,其它可能的问题就留在后续发现并解决吧.今天第一次听到并重视下couchbase.上午看到同事 ...
- 快速配置 Samba 将 Linux 目录映射为 Windows 驱动器
原文链接 samba client ubuntu redhat ubuntu gui tools 1,列出某个IP地址所提供的共享文件夹 smbclient -L 198.168.0.1 2,在s ...
- Android 绘图工具库AChartEngine
From: http://www.oschina.net/p/achartengine AChartEngine是为android应用而设计的绘图工具库.目前该库的最新稳定版本是0.7,支持绘制以下类 ...
- EF框架+Lamada表达式(联合多表lamada表达式的用法)
有俩张表对应的EF框架的类Reviews和Commodity_Review,新建一个新的类,字段是联合俩张表后自己需要展示的字段ReviewsShow IQueryable<ReviewsSho ...
- 加濾鏡效果GlowTween
/** * * new GlowTween(xxxx, 0xFFFF00); * new GlowTween(xxxx, 0x00FFFF); * GlowTween */ package com.r ...
- 傅老师课堂:Java高级应用之Struts2+Spring2+Hibernate3大集成
开篇一笑:一对情侣,非常恩爱,但男友喜欢说脏话,一天女友提出要带男友回家吃个饭,见见家长,千叮万嘱让男友别说脏话,男友在家憋了一晚上没说一句脏话,天气寒冷,到走的时候女友家长要出来送他们,男友客气的说 ...
- 【main()的参数探究】
恩...今天研究信安的课件的时候看到一段对于main(int argc,char *argv[])的编程 所以探究探究main()函数的参数 探究程序如下: #include <cstdio&g ...
- jquery选择器从认识到使用初级篇
1. .class 选择器 ---一种通过元素类别属性查找元素 调用格式: $(".class") ----其中参数表示元素的css类别名称(类选择器)<input cl ...
- 基本文件的I/O
System.IO命名空间包含允许在数据流和文件上进行同步,异步及写入的类型.文件是一些永久存储及具有特定顺序的字节组成的一个有序的,具有名称的集合.与文件有关的概念是目录路径和磁盘存储等.流提供了一 ...
- javascript——touch事件介绍与实例演示
分类: javascript2014-02-12 16:42 1742人阅读 评论(0) 收藏 举报 touch事件touchmovetouchstarttouchend 前言 诸如智能手机和平板 ...