题目大意:
农民约翰在农场散步的时候发现农场有大量的虫洞,这些虫洞是非常特别的因为它们都是单向通道,为了方便现在把约翰的农田划分成N快区域,M条道路,W的虫洞。
约翰是时空旅行的粉丝,他希望这样做,在一个区域开始,经过一些道路和虫洞然后回到他原来所在的位置,这样也许他就能见到他自己了。
穿越虫洞会回到以前。。。。。(穿越者约翰)
/////////////////////////////////////////////////////////////
很明显这是一个很扯淡的故事,不过为了出一道带负权值的题目也是难为了出题人,迪杰斯特拉算法不能处理带有负权值的问题,佛洛依德倒是可以,不过复杂度很明显太高,所以spfa是不错的选择,只需要判断出发点时间是否变小.
  1. #include<algorithm>
  2. #include<queue>
  3. #include<stdio.h>
  4. #include<string.h>
  5. #include<vector>
  6. #include<math.h>
  7. using namespace std;
  8.  
  9. const int maxn = ;
  10. const int oo = 0xfffffff;
  11.  
  12. struct node
  13. {
  14.     int y, time;
  15.     node(int y, int t):y(y), time(t){}
  16. };
  17. vector<node> G[maxn];
  18. int v[maxn];
  19.  
  20. int spfa(int s)
  21. {
  22.     queue<int> Q;
  23.     Q.push(s);
  24.  
  25.     while(Q.size())
  26.     {
  27.         s = Q.front();Q.pop();
  28.         int len = G[s].size();
  29.  
  30.         for(int i=; i<len; i++)
  31.         {
  32.             node q = G[s][i];
  33.  
  34.             if(v[s]+q.time < v[q.y])
  35.             {
  36.                 v[q.y] = v[s] + q.time;
  37.                 Q.push(q.y);
  38.             }
  39.         }
  40.  
  41.         if(v[] < )
  42.             return ;
  43.     }
  44.  
  45.     return ;
  46. }
  47.  
  48. int main()
  49. {
  50.     int T;
  51.  
  52.     scanf("%d", &T);
  53.  
  54.     while(T--)
  55.     {
  56.         int N, M, W, i, a, b, c;
  57.  
  58.         scanf("%d%d%d", &N, &M, &W);
  59.  
  60.         for(i=; i<=N; i++)
  61.         {
  62.             v[i] = oo;
  63.             G[i].clear();
  64.         }
  65.         v[] = ;
  66.  
  67.         for(i=; i<M; i++)
  68.         {
  69.             scanf("%d%d%d", &a, &b, &c);
  70.             G[a].push_back(node(b, c));
  71.             G[b].push_back(node(a, c));
  72.         }
  73.  
  74.         for(i=; i<W; i++)
  75.         {
  76.             scanf("%d%d%d", &a, &b, &c);
  77.             G[a].push_back(node(b, -c));
  78.         }
  79.  
  80.         int ans = spfa();
  81.  
  82.         if(ans == )
  83.             printf("YES\n");
  84.         else
  85.             printf("NO\n");
  86.     }
  87.  
  88.     return ;

}

F - Wormholes的更多相关文章

  1. 【算法系列学习】SPFA邻接表最短路 [kuangbin带你飞]专题四 最短路练习 F - Wormholes

    https://vjudge.net/contest/66569#problem/F 题意:判断图中是否存在负权回路 首先,介绍图的邻接表存储方式 数据结构:图的存储结构之邻接表 邻接表建图,类似于头 ...

  2. Mysql_以案例为基准之查询

    查询数据操作

  3. POJ 3259 Wormholes (判负环)

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

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

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

  5. Wormholes

    Description While exploring his many farms, Farmer John has discovered a number of amazing wormholes ...

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

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

  7. Wormholes(Bellman-ford)

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

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

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

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

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

随机推荐

  1. Android 官方新手指导教程

    一.开始 1.建立第一个应用程序 依赖关系和先决条件 Android SDK ADT Plugin 20.0.0 或更高 (如果你使用eclipse的话) 欢迎来到Android应用程序开发! 这一节 ...

  2. There is no ID/IDREF binding for IDREF

    http://blog.csdn.net/greensurfer/article/details/7596219

  3. .net RAW(16)与GUID互相转换

    .net 1.raw转guidnew guid(byte[] id);2.guid转rawGuid result;string ids = BitConverter.ToString(result.T ...

  4. CoreBluetooth

    Core Bluetooth的基本常识 每个蓝牙4.0设备都是通过服务(Service)和特征(Characteristic)来展示自己的 一个设备必然包含一个或多个服务,每个服务下面又包含若干个特征 ...

  5. C#winform程序自定义鼠标样式

    public void SetCursor(Bitmap cursor, Point hotPoint) { int hotX = hotPoint.X; int hotY = hotPoint.Y; ...

  6. 【BZOJ2809】【splay启发式合并】dispatching

    Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依据自己的工作获取报偿.在这个帮派里,有一名忍者被称之为 Master.除了 Master以外,每名忍者都有且仅有一个上级. ...

  7. javascript——归并方法

    <script type="text/javascript"> //ECMAScript5 还新增了2个归并数组的方法:reduce()和reduceRight(). ...

  8. Python自动化运维之26、Web框架本质、MVC与MTV

    一.Web框架本质 众所周知,对于所有的Web应用,本质上其实就是一个socket服务端,用户的浏览器其实就是一个socket客户端. #!/usr/bin/env python #coding:ut ...

  9. python有序字典实现代码

    class MyDict(dict): #有序字典实现 def __init__(self): self.li = [] super(MyDict,self).__init__() def __set ...

  10. Python Tutorial 学习(三)--An Informal Introduction to Python

    3.1. 将Python用作计算器 3.1.1. Numbers 数 作为一个计算器,python支持简单的操作, '+','-','*','/'地球人都知道的加减乘除. ()可以用来改变优先级,同数 ...