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: 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 from S to E that also moves the traveler back T seconds.

Output

Lines 1..
F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).

Sample Input

  1. 2
  2. 3 3 1
  3. 1 2 2
  4. 1 3 4
  5. 2 3 1
  6. 3 1 3
  7. 3 2 1
  8. 1 2 3
  9. 2 3 4
  10. 3 1 8

Sample Output

  1. NO
  2. YES

Hint

For farm 1, FJ cannot travel back in time.
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.

题目大意就是:农夫约翰有F个农场,每个农场有N块地,其间有M条路(无向),W条时光隧道(有向且时间倒流即:权值为负)。问是否可能回到过去?

经典的bellman_Ford理解题,不知道的可以去百度!

  1. //Asimple
  2. #include <iostream>
  3. #include <sstream>
  4. #include <algorithm>
  5. #include <cstring>
  6. #include <cstdio>
  7. #include <vector>
  8. #include <cctype>
  9. #include <cstdlib>
  10. #include <stack>
  11. #include <cmath>
  12. #include <set>
  13. #include <map>
  14. #include <string>
  15. #include <queue>
  16. #include <limits.h>
  17. #include <time.h>
  18. #define INF 0x3f3f3f3f
  19. using namespace std;
  20. const int maxn = ;
  21. typedef long long ll;
  22. int n, m, num, T, k, x, y, len;
  23. int Map[maxn][maxn];
  24. int dis[maxn];
  25. typedef struct node {
  26. int begin;
  27. int end;
  28. int weight;
  29. node(){}
  30. node(int begin, int end, int weight) {
  31. this->begin = begin;
  32. this->end = end;
  33. this->weight = weight;
  34. }
  35. }eee;
  36. eee edg[maxn];
  37. //Bellman-Ford算法:求含负权图的单源最短路径算法
  38. //单源最短路径(从源点s到其它所有顶点v)
  39. bool bellmanFord() {
  40. memset(dis, , sizeof(dis));
  41. for(int i=; i<n; i++) {
  42. for(int j=; j<len; j++) {
  43. eee e = node(edg[j].begin, edg[j].end, edg[j].weight);
  44. if( dis[e.end] > dis[e.begin] + e.weight) {
  45. dis[e.end] = dis[e.begin] + e.weight;
  46. if( i == n- ) return true;
  47. }
  48. }
  49. }
  50. return false;
  51. }
  52.  
  53. void input() {
  54. cin >> T ;
  55. while( T -- ) {
  56. cin >> n >> m >> k;
  57. len = ;
  58. for(int i=; i<m; i++) {
  59. cin >> x >> y >> num;
  60. edg[len].begin = x;
  61. edg[len].end = y;
  62. edg[len].weight = num;
  63. len ++;
  64. edg[len].begin = y;
  65. edg[len].end = x;
  66. edg[len].weight = num;
  67. len ++;
  68. }
  69. for(int i=; i<k; i++) {
  70. cin >> x >> y >> num ;
  71. edg[len].begin = x;
  72. edg[len].end = y;
  73. edg[len].weight = -num;
  74. len ++;
  75. }
  76. if( bellmanFord() ) cout << "YES" << endl;
  77. else cout << "NO" << endl;
  78. }
  79. }
  80.  
  81. int main(){
  82. input();
  83. return ;
  84. }

2017-5-26  修改:

自己写了一个邻接矩阵的SPFA解法

坑点:可能会出现重复的路径,这个时候需要取小值。

  1. #include <iostream>
  2. #include <cstring>
  3. #include <queue>
  4. using namespace std;
  5. const int maxn = +;
  6. const int INF = ( << );
  7. int n, m, x, y, num, T, k;
  8. int Map[maxn][maxn], dis[maxn], c[maxn];
  9.  
  10. void init(){
  11. for(int i=; i<=n; i++) {
  12. dis[i] = INF;
  13. c[i] = ;
  14. for(int j=; j<=n; j++) {
  15. Map[i][j] = INF;
  16. }
  17. }
  18. }
  19.  
  20. bool spfa(){
  21. bool vis[maxn];
  22. queue<int> q;
  23. memset(vis, false, sizeof(vis));
  24. q.push();
  25. vis[] = true;
  26. c[] = ;
  27. dis[] = ;
  28. while( !q.empty() ) {
  29. x = q.front();q.pop();
  30. vis[x] = false;
  31. for(int i=; i<=n; i++) {
  32. if( dis[i]>dis[x]+Map[x][i] ) {
  33. dis[i] = dis[x]+Map[x][i];
  34. if( !vis[i] ) {
  35. vis[i] = true;
  36. c[i] ++;
  37. if( c[i]>=n ) return true;
  38. q.push(i);
  39. }
  40. }
  41. }
  42. }
  43. return false;
  44. }
  45.  
  46. int main(){
  47. cin >> T;
  48. while( T -- ) {
  49. cin >> n >> m >> k;
  50. init();
  51. while( m -- ) {
  52. cin >> x >> y >> num;
  53. Map[x][y] = min(Map[x][y], num);
  54. Map[y][x] = Map[x][y];
  55. }
  56. while( k -- ) {
  57. cin >> x >> y >> num;
  58. Map[x][y] = min(Map[x][y], -num);
  59. }
  60. if( spfa() ) cout << "YES" << endl;
  61. else cout << "NO" << endl;
  62. }
  63. return ;
  64. }

Wormholes的更多相关文章

  1. [题解]USACO 1.3 Wormholes

    Wormholes Farmer John's hobby of conducting high-energy physics experiments on weekends has backfire ...

  2. POJ 3259 Wormholes (判负环)

    Wormholes Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 46123 Accepted: 17033 Descripti ...

  3. ACM: POJ 3259 Wormholes - SPFA负环判定

     POJ 3259 Wormholes Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu   ...

  4. poj 3259 Wormholes 判断负权值回路

    Wormholes Time Limit: 2000 MS Memory Limit: 65536 KB 64-bit integer IO format: %I64d , %I64u   Java ...

  5. Wormholes(Bellman-ford)

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 33008   Accepted: 12011 Descr ...

  6. poj3259 bellman——ford Wormholes解绝负权问题

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35103   Accepted: 12805 Descr ...

  7. 最短路(Bellman_Ford) POJ 3259 Wormholes

    题目传送门 /* 题意:一张有双方向连通和单方向连通的图,单方向的是负权值,问是否能回到过去(权值和为负) Bellman_Ford:循环n-1次松弛操作,再判断是否存在负权回路(因为如果有会一直减下 ...

  8. Wormholes 分类: POJ 2015-07-14 20:21 21人阅读 评论(0) 收藏

    Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 35235   Accepted: 12861 Descr ...

  9. POJ 3259 Wormholes (Bellman_ford算法)

    题目链接:http://poj.org/problem?id=3259 Wormholes Time Limit: 2000MS   Memory Limit: 65536K Total Submis ...

随机推荐

  1. 配置DNS实验一例

    1安装bind软件 2查看当前DNS服务 3修改配置文件 4测试

  2. php完全销毁session

    <?php session_start(); session_unset(); session_destroy(); ?> session_unset()释放当前在内存中已经创建的所有$_ ...

  3. 3G網絡容量和業務承載的壓力大大增加!

    在移動通信話音業務繼續保持發展的同時,對IP和高速數據業務的支持已經成為移動通信系統演進的方向.移動數據業務是推動目前移動通信技術發展的主要動力,TD-LTE作為準4G技術,以提高數據速率和頻譜利用率 ...

  4. 【转】手把手教你把Vim改装成一个IDE编程环境(图文)

    手把手教你把Vim改装成一个IDE编程环境(图文) By: 吴垠 Date: 2007-09-07 Version: 0.5 Email: lazy.fox.wu#gmail.com Homepage ...

  5. Cookie案例-显示商品浏览历史纪录

    package cn.itcast.cookie; import java.io.IOException; import java.io.PrintWriter; import java.util.D ...

  6. 禁止COOKIE后对SESSION的影响

    一.理解SESSION机制 简单来说:每一个SESSION都有一个唯一的session_id , 默认情况下,session_id存储在客户端(默认COOKIE['PHPSESSID']), 在使用S ...

  7. 如何使用 vimdiff 来 git diff /svn diff

    #git 如何实现vimdiffgit config --global diff.tool vimdiff git config --global difftool.prompt false git ...

  8. ES6 module export options 模块导出、导入语法

    http://stackoverflow.com/questions/25494365/es6-module-export-options A year and some later, here is ...

  9. Rectangle Area || LeetCode

    把交叉点的坐标求出来即可. #define max(a,b) ( (a)>(b)?(a):(b) ) #define min(a,b) ( (a)<(b)?(a):(b) ) int co ...

  10. 《Linux内核分析》第七周 读书笔记

    <深入理解计算机系统>CHAPTER7阅读梳理 [学习时间:3hours] [学习内容:链接需要的代码&数据:链接机制:链接生成的目标文件] 一.链接概述 1.链接 定义:链接是将 ...