题意:

  给定一个n个点m条边的加权有向图,求平均权值最小的回路

解析:

  首先肯定是想到找出环路  然后。。呵。。呵。。呵呵。。。

  显然不现实!!

  二分大法好 。。。。去猜结果 然后带入验证 。。。真是的。。很过分!

嗯! 是的!

我参考一下UVA11478的代码 。。。建立超级源的做法。。竟然50ms  网上的用遍历每个没经过的点的做法2130ms  质的飞跃 。。。。。

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <sstream>
  4. #include <cstring>
  5. #include <map>
  6. #include <set>
  7. #include <vector>
  8. #include <stack>
  9. #include <queue>
  10. #include <algorithm>
  11. #include <cmath>
  12. #define rap(i, a, n) for(int i=a; i<=n; i++)
  13. #define MOD 2018
  14. #define LL long long
  15. #define ULL unsigned long long
  16. #define Pair pair<int, int>
  17. #define mem(a, b) memset(a, b, sizeof(a))
  18. #define _ ios_base::sync_with_stdio(0),cin.tie(0)
  19. //freopen("1.txt", "r", stdin);
  20. using namespace std;
  21. const int maxn = , INF = 0x7fffffff;
  22. int head[maxn], vis[maxn], ans[maxn];
  23. double d[maxn];
  24. int cnt, n, m;
  25. struct node
  26. {
  27. int v, next;
  28. double w;
  29. }Node[maxn];
  30.  
  31. void add(int u, int v, double w)
  32. {
  33. Node[cnt].v = v;
  34. Node[cnt].w = w;
  35. Node[cnt].next = head[u];
  36. head[u] = cnt++;
  37. }
  38.  
  39. int spfa()
  40. {
  41. queue<int> Q;
  42. for(int i=; i<=n; i++)
  43. {
  44. Q.push(i);
  45. d[i] = ;
  46. vis[i] = ;
  47. }
  48. mem(ans, );
  49. while(!Q.empty())
  50. {
  51. int u = Q.front(); Q.pop();
  52. vis[u] = ;
  53. for(int i=head[u]; i!=-; i=Node[i].next)
  54. {
  55. node e = Node[i];
  56. if(d[e.v] > d[u] + e.w)
  57. {
  58. d[e.v] = d[u] + e.w;
  59. if(!vis[e.v])
  60. {
  61. Q.push(e.v);
  62. vis[e.v] = ;
  63. if(++ans[e.v] >= n) return ;
  64. }
  65. }
  66. }
  67. }
  68. return ;
  69. }
  70.  
  71. bool check(double x)
  72. {
  73. bool flag = ;
  74. for(int i=; i<cnt; i++)
  75. Node[i].w -= x;
  76.  
  77. // for(int i=1; i<=n; i++)
  78. // if(spfa(i))
  79. // flag = 1;
  80. if(spfa())
  81. flag = ;
  82. for(int i=; i<cnt; i++)
  83. Node[i].w += x;
  84. return flag;
  85. }
  86.  
  87. void init()
  88. {
  89. mem(head, -);
  90. cnt = ;
  91. }
  92.  
  93. int main()
  94. {
  95. int T, kase = ;
  96. scanf("%d", &T);
  97. while(T--)
  98. {
  99. init();
  100. int u, v;
  101. double w, x = , y = ;
  102. scanf("%d%d", &n, &m);
  103. for(int i=; i<m; i++)
  104. {
  105. scanf("%d%d%lf", &u, &v, &w);
  106. add(u, v, w);
  107. y = max(y, w);
  108. }
  109. printf("Case #%d: ",++kase);
  110. if(!check(y+)) printf("No cycle found.\n");
  111. else
  112. {
  113. while(y - x > 1e-)
  114. {
  115. double mid = x + (y-x)/(double);
  116. if(check(mid)) y = mid;
  117. else x = mid;
  118. }
  119. printf("%.2lf\n",x);
  120. }
  121. }
  122.  
  123. return ;
  124. }

Going in Cycle!! UVA - 11090(二分+判断环路 )的更多相关文章

  1. 在环中(Going in Cycle!!, UVa 11090)

    [题目描述] 给定一个 n 个点 m 条边的加权有向图,求平均权值最小的回路. [输入格式] 输入第一行为数据组数 T .每组数据第一行为图的点数 n 和边数 m (n ≤ 50).以下 m 行每行3 ...

  2. 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)

    layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...

  3. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

  4. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  5. POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置关系

    POJ_2318_TOYS&&POJ_2398_Toy Storage_二分+判断直线和点的位置 Description Calculate the number of toys th ...

  6. UVA 11090 Going in Cycle!! SPFA判断负环+二分

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)

    题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...

  8. UVA 11090 Going in Cycle!!(二分答案+判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

  9. UVa 11090 Going in Cycle!!【Bellman_Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

随机推荐

  1. Q&As:1.cocos2d-html5如何获得鼠标划过事件

    不喜欢按部就班学东西,感觉各种框架各种技术就应该是拿到手用的,这应该是导致我现在学了这么多却没一样精通的缘故吧. 发现自己喜欢在QQ群回答一些菜鸟的问题,就算自己不清楚也会乐意看代码帮助解决╮(╯_╰ ...

  2. Supervisor4.0和python2.7的crit问题,导致python进程阻塞

    1.问题原因 Supervisor高版本在守护python2.7的服务时,会crit并报错并倒至进程阻塞(python进程存在,但不在运行)的问题,一般会和字符集有关系 <type 'excep ...

  3. Unity利用Share SDK实现QQ、微信及微博第三方登录及定制内容分享(附代码)

    最近因为公司的项目需要添加一些实用性的功能,需要添加第三方登录及分享,采用的是Mob的SDK,可以先到其官网下载对应的SDK 点击这里,为了方便后期进行数据统计和分析,所以可以先添加一个应用,添加成功 ...

  4. 最短路径算法(I)

    弗洛伊德算法(Floyed-Warshall) 适用范围及时间复杂度 该算法的时间复杂度为O(N^3),适用于出现负边权的情况. 可以求取最短路径或判断路径是否连通.可用于求最小环,比较两点之间的大小 ...

  5. 亚马逊:PS4和Xbox One实在太火

    圣诞节刚刚结束,当实体零售商在抱怨坑爹的天气让自己节日生意变得冷清的同时,在线零售商们却依旧赚的盆满钵满. 亚马逊近日表示,今年节日期间的零售工作非常不错,新一代游戏机更是最大的亮点.据销售统计,在圣 ...

  6. oozie的shell-action中加入hive脚本命令启动执行shell同时操作hive,抛异常Container killed on request. Exit code is 143 Container exited with a non-zero exit code 143

    使用oozie来调度操作,用shell的action执行命令,其中shell里包含着hive -e 操作执行时,oozie窗口报 WARN ShellActionExecutor: - SERVER[ ...

  7. centos上搭建git服务--2

    在 Linux 下搭建 Git 服务器   环境: 服务器 CentOS6.6 + git(version 1.7.1)客户端 Windows10 + git(version 2.8.4.window ...

  8. npm 常用指令

    npm install <name>安装nodejs的依赖包 例如npm install express 就会默认安装express的最新版本,也可以通过在后面加版本号的方式安装指定版本, ...

  9. Scrum立会报告+燃尽图(十月十二日总第三次):视频相关工作

    此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2190 Scrum立会master:孙赛佳 一.小组介绍 组长:付佳 组员 ...

  10. 20135208 JAVA第四次实验

    课程:Java程序与设计     班级:1352 姓名:贺邦 小组成员: 20135212池彬宁 20135208贺邦 学号:20135208 成绩:             指导教师:娄嘉鹏     ...