POJ--3529--Wormholes
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 36889 | Accepted: 13520 |
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
题意:就是给N组測试数据,给你N个点M条无向边,再给你W个虫洞(能够使时间倒流)。问你能不能看到刚出发时的自己。(就是推断存不存在负环)。
思路:套上SPFA公式然后推断负环就能够了。
#include<stdio.h>//好坑,wa了半天没过去,竟然是由于大写和小写问题。整个人都不好了。
#include<string.h>
#include<queue>
#define INF 0x3f3f3f3f
#define M 3000*2
#define N 550
using namespace std;
int n,m,w,dis[N],vis[N],used[N],head[N],edgenum;
struct node{
int from,to,cost,next;
}edge[M];
void init(){
edgenum=0;
memset(head,-1,sizeof(head));
}
void add(int u,int v,int cost){
node E={u,v,cost,head[u]};
edge[edgenum]=E;
head[u]=edgenum++;
}
void spfa(){
queue<int>q;
memset(vis,0,sizeof(vis));
memset(dis,INF,sizeof(dis));
memset(used,0,sizeof(used));
dis[1]=0;
vis[1]=1;
q.push(1);
used[1]++;
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=0;
for(int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].cost){
dis[v]=dis[u]+edge[i].cost;
if(!vis[v]){
vis[v]=1;
used[v]++;
if(used[v]>n){
printf("YES\n");//存在负环就意味着时光能够倒流。
return ;
}
q.push(v);
}
}
}
}
printf("NO\n");
}
int main(){
int T;
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&n,&m,&w);
init();
while(m--){
int a,b,cost;
scanf("%d%d%d",&a,&b,&cost);//加入无向边。
add(a,b,cost);
add(b,a,cost);
}
while(w--){
int a,b,cost;//加入单向虫洞。 scanf("%d%d%d",&a,&b,&cost);
cost=-cost;
add(a,b,cost);
}
spfa();
}
return 0;
}
ac代码:
POJ--3529--Wormholes的更多相关文章
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
- poj - 3259 Wormholes (bellman-ford算法求最短路)
http://poj.org/problem?id=3259 农夫john发现了一些虫洞,虫洞是一种在你到达虫洞之前把你送回目的地的一种方式,FJ的每个农场,由n块土地(编号为1-n),M 条路,和W ...
- 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
题目连接 http://poj.org/problem?id=3259 Wormholes Description While exploring his many farms, Farmer Joh ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- [ACM] POJ 3259 Wormholes (bellman-ford最短路径,推断是否存在负权回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 29971 Accepted: 10844 Descr ...
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
链接: http://poj.org/problem?id=3259 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22010#probl ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
随机推荐
- zk选举过程
1. 服务器启动时期的Leader选举 若进行Leader选举,则至少需要两台机器,这里选取3台机器组成的服务器集群为例.在集群初始化阶段,当有一台服务器Server1启动时,其单独无法进行和完成Le ...
- 【java】java中替换中括号[ ]操作
String aa ="[1,2,3]"; aa = aa.replaceAll("[\\[\\]]",""); 结果为 1,2,3
- java数据库编程——读写LOB、可滚动和可更新的结果集、元数据
java 数据库编程 1. 读写LOB 除了数字.字符串和日期之外,许多数据库还可以存储大对象,例如图片或其它数据.在SQL中,二进制大对象称为BLOB,字符型大对象称为CLOB. 要读取LOB,需要 ...
- linux之misc及使用misc创建字符设备
1:linux字符设备及udev 1.1字符设备 字符设备就是:一个一个字节来进行访问的,不能对字符设备进行随机读写.简单字符设备创建实例如下: #include <linux/module.h ...
- 教你用webgl快速创建一个小世界
收录待用,修改转载已取得腾讯云授权 作者:TAT.vorshen Webgl的魅力在于可以创造一个自己的3D世界,但相比较canvas2D来说,除了物体的移动旋转变换完全依赖矩阵增加了复杂度,就连生成 ...
- 【ACM】最小公倍数
http://acm.hdu.edu.cn/game/entry/problem/show.php?chapterid=2§ionid=1&problemid=1 #inclu ...
- CentOS下配置HTTPS訪问主机并绑定訪问port号
系统环境: Linux: CentOS release 6.5 (Final) LAMP 步骤 1.lamp开启ssl # yum install openssl mod_ssl -y # 安装ope ...
- Discuz常见小问题2-如何在新建的页面上只显示一部分板块
切换到论坛-版块管理,记住要只显示的板块的gid(比如我的是36) 为某个主导航设置一个单独的php页面(名字自己取) 如果这个页面内容跟首页forum.php完全一样,只是第三行增加了一句话 ...
- virtualbox使用相关问题
官方下载地址: http://www.oracle.com/technetwork/server-storage/virtualbox/downloads/index.html VirtualBox主 ...
- mysql,给每一条数据的某一个字段生成不同的随机数
UPDATE t_article ta-- 利用LEFT JOIN的方式进行关联修改 LEFT JOIN(-- 先通过查询的方式给每一条数据生成对应的10-500之间随机数 SELECT articl ...