(最短路 spfa)Wormholes -- poj -- 3259
http://poj.org/problem?id=3259
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 37356 | Accepted: 13734 |
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
代码:
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
using namespace std;
const int INF = (<<)-;
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define N 5500 int n, m, w, dist[N], G[N][N], vis[N];
int u; struct node
{
int v, t, next;
} a[N]; int Head[N], cnt; void Init()
{
cnt = ;
memset(Head, -, sizeof(Head));
memset(vis, , sizeof(vis));
for(int i=; i<=n; i++)
{
dist[i] = INF;
for(int j=; j<=i; j++)
G[i][j] = G[j][i] = INF;
}
} void Add(int u, int v, int t)
{
a[cnt].v = v;
a[cnt].t = t;
a[cnt].next = Head[u];
Head[u] = cnt++;
} int spfa()
{
queue<int>Q;
Q.push();
dist[] = ;
vis[] = ; while(Q.size())
{
int u = Q.front(); Q.pop(); vis[u] = ; for(int i=Head[u]; i!=-; i=a[i].next)
{
int v = a[i].v;
int t = a[i].t;
if(dist[u] + t < dist[v])
{
dist[v] = dist[u] + t;
if(vis[v] == )
{
Q.push(v);
vis[v] = ;
}
}
} if(dist[] < ) ///当 dist[1] 为负数的时候说明它又回到了原点
return ;
}
return ;
} int main()
{
int t;
scanf("%d", &t);
while(t--)
{
int i, u, v, x; scanf("%d%d%d", &n, &m, &w); Init();
for(i=; i<=m; i++)
{
scanf("%d%d%d", &u, &v, &x);
Add(u, v, x);
Add(v, u, x);
} for(i=; i<=w; i++)
{
scanf("%d%d%d", &u, &v, &x);
Add(u, v, -x);
} int ans = spfa(); if(ans)
printf("YES\n");
else
printf("NO\n");
}
return ;
}
(最短路 spfa)Wormholes -- poj -- 3259的更多相关文章
- Wormholes POJ 3259(SPFA判负环)
Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...
- Wormholes POJ - 3259 spfa判断负环
//判断负环 dist初始化为正无穷 //正环 负无穷 #include<iostream> #include<cstring> #include<queue> # ...
- ShortestPath:Wormholes(POJ 3259)
田里的虫洞 题目大意:就是这个农夫的田里有一些虫洞,田有很多个点,点与点之间会存在路,走过路需要时间,并且这些点存在虫洞,可以使农夫的时间退回到时间之前,问你农夫是否真的能回到时间之前? 读完题:这一 ...
- Wormholes - poj 3259 (Bellman-Ford算法)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 34934 Accepted: 12752 Description W ...
- kuangbin专题专题四 Wormholes POJ - 3259
题目链接:https://vjudge.net/problem/POJ-3259 思路:求有无负环,起点随意选就可以,因为目的只是找出有没有负环,有了负环就可以让时间一直回退,那么一定能回到当初,这里 ...
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit St ...
- ACM: POJ 3259 Wormholes - SPFA负环判定
POJ 3259 Wormholes Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu ...
- POJ 3259 Wormholes(最短路,判断有没有负环回路)
Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 24249 Accepted: 8652 Descri ...
- 最短路(Bellman_Ford) POJ 3259 Wormholes
题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...
随机推荐
- Haskell语言学习笔记(50)Extra
Extra 安装 extra 模块. $ cabal install extra Installed extra-1.6 Prelude> :m +Extra Prelude Extra> ...
- windows 2008下远程连接 redhat linux 9桌面
如何使用windows远程控制Linux桌面? 1.查看本机是否有安装vnc(redhat linux 9默认有安装vnc) rpm -q vnc vnc-server 如果显示结果为: packag ...
- Windows 2008开启远程桌面连接
具体请看下面的截图. 最重要的就是要打开远程允许远程桌面的默认端口 3389 的入站规则,我第一次弄,这一端口没打开,折腾了很久!!! 第一.首先打开“服务器管理器”—“配置”—“高级安全Window ...
- android示例:一个简单的登陆程序
最近写了个简单的登陆程序,有几点收获: 1.懂得如何在LinearLayout中嵌套LinearLayout,完善布局的行列: 2.用android:layout_weight控制控件的比重: 3.用 ...
- 通过阿里OSS文件服务返回的URL获取文件流下载
我们都知道将文件上传到阿里的OSS文件服务上后,可以通过generatePresignedUrl(bucketName, key, expiration)方法获取该文件的防问路径,但是当我们知道该文件 ...
- GridView,datalist添加序号列
GridView添加序号列:这个是经常需要的一个功能 <asp:TemplateField HeaderText="序号"> <ItemTemplate> ...
- socket接口详解
1. socket概述 socket是在应用层和传输层之间的一个抽象层,它把TCP/IP层复杂的操作抽象为几个简单的接口供应用层调用已实现进程在网络中通信. socket起源于UNIX,在Unix一切 ...
- 使用Linux之安装jdk 7
工具/原料 jdk7源码安装压缩包 方法/步骤 卸载OpenJDK rpm -qa | grep java rpm -e --nodeps java-1.6.0-openjdk-1.6.0 ...
- 接触mybatis使用
1.mybatis mybatis是一个自定义sql.存储过程和高级映射的持久层框架,是Apache下的顶级项目. mybatis可以让程序员将主要精力放在sql上,通过mybatis提供的映射方式. ...
- springmvc使用数组接收页面商品列表批量删除传过来的参数,并完成批量删除的操作。
1.1 需求 在商品列表页面选中多个商品,然后删除. 1.2 需求分析 此功能要求商品列表页面中的每个商品前有一个checkbox,选中多个商品后点击删除按钮把商品id传给controller,根据商 ...