POJ3259_Wormholes_spfa
来源:互联网 发布:cf英雄级武器淘宝网 编辑:程序博客网 时间:2024/06/09 17:13
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
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
有 N 个点,连接他们的有 M 条 双向路, 路的长度是正的, 有 W 个双向虫洞,虫洞的长度是负的。
求问是否存在一个点,从这个点出发回来之后,穿越了时空(路径的总长度是负的)。
实际上是判断负权环是否存在。用spfa判断即可。
题面有点问题,实际上 点1 一定在负权环上。
#include<cstdio>#include<iostream>#include<queue>#include<cstring>using namespace std;const int maxn = 510;int Map [maxn][maxn];//敲黑板,spfa 零件bool in [maxn];int times [maxn];int dis [maxn];//敲黑板,spfa 零件//敲黑板 spfa ,判负权环bool spfa (int x, int N){//初始化queue<int> qu;memset(in, false, sizeof(in) );memset(times, 0, sizeof(times) );memset(dis, 0x3f, sizeof(dis) );//第一个点加入dis[x] = 0; qu.push(x); in[x] = true, times[x] ++; while(qu.size() ){int t = qu.front();qu.pop();in[t] = false;for(int i= 1; i<= N; i++)if(dis[i] > dis[t]+Map[t][i]){dis[i] = dis[t] + Map[t][i];if(!in[i]){qu.push(i);in[i] = true;times[i] ++;if(times[i] >= N) return true;}} } return false;}int main (){int F;scanf("%d", &F);while(F--){int N, M, W;scanf("%d %d %d", &N, &M, &W);//初始化memset(Map, 0x3f, sizeof(Map) );for(int i= 1; i<= N; i++)Map[i][i] = 0;for(int i= 0; i< M; i++){int s, e, t;scanf("%d %d %d", &s, &e, &t);if(t < Map[s][e]) Map[s][e] = Map[e][s] = t;}for(int i= 0; i< W; i++){int s, e, t;scanf("%d %d %d", &s, &e, &t);if(-t < Map[s][e] ) Map[s][e] = -t;}int f = spfa(1, N);if(f) puts("YES");else puts("NO");}return 0;}
- POJ3259_Wormholes_spfa
- Linux块设备驱动(一)————块设备的结构及磁盘的结构
- XTU 1236 Fraction
- Anconda 中Spyder无法打开IPython控制台
- javaCV开发详解之3:收流器实现,录制流媒体服务器的rtsp/rtmp视频文件(基于javaCV-FFMPEG)
- Ubuntu16.04 下同时使用 Python2 和 Python3 及对应的 pip 的方法
- POJ3259_Wormholes_spfa
- RecyclerViewDemo
- javaCV开发详解之4:转流器实现(也可作为本地收流器、推流器,新增添加图片及文字水印,视频图像帧保存),实现rtsp/rtmp/本地文件转发到rtmp流媒体服务器(基于javaCV-FFMPEG)
- 安卓网络请求以及图片框架大集合
- CGridCtrl
- 擒贼先擒王-并查集
- lazarus安装组件后使用时出现 Cannot find xx used by
- javaCV开发详解之5:录制音频(录制麦克风)到本地文件/流媒体服务器(基于javax.sound、javaCV-FFMPEG)
- vue2自定义指令的作用