Problem  UVA - 11090 - Going in Cycle!!

Time Limit: 3000 mSec

Problem Description

You are given a weighted directed graph with n vertices and m edges. Each cycle in the graph has a weight, which equals to sum of its edges. There are so many cycles in the graph with different weights. In this problem we want to find a cycle with the minimum mean.

Input

The first line of input gives the number of cases, N. N test cases follow. Each one starts with two numbers n and m. m lines follow, each has three positive number a,b,c which means there is an edge from vertex a to b with weight of c.

Output

For each test case output one line containing Case #x: followed by a number that is the lowest mean cycle in graph with 2 digits after decimal place, if there is a cycle. Otherwise print No cycle found..
Constraints

• n ≤ 50

• a,b ≤ n

• c ≤ 10000000

Sample Input

2 2 1 1 2 1 2 2 1 2 2 2 1 3

Sample Output

Case #1: No cycle found.

Case #2: 2.50

题解:差分约束系统板子题,配上二分很容易解决,这里的spfa和一般的spfa稍有区别,原因我在UVA 11478的博客里解释过了,不再赘述,这种写法会比分别以每个点为源点跑高效不少,目前这份代码耗时50ms,但是分别以每个点为源点跑了200ms。

  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define REP(i, n) for (int i = 1; i <= (n); i++)
  6. #define sqr(x) ((x) * (x))
  7.  
  8. const int maxn = + ;
  9. const int maxm = + ;
  10. const int maxs = + ;
  11.  
  12. typedef long long LL;
  13. typedef pair<int, int> pii;
  14. typedef pair<double, double> pdd;
  15.  
  16. const LL unit = 1LL;
  17. const int INF = 0x3f3f3f3f;
  18. const LL mod = ;
  19. const double eps = 1e-;
  20. const double inf = 1e15;
  21. const double pi = acos(-1.0);
  22.  
  23. struct Edge
  24. {
  25. int to, next;
  26. double w;
  27. } edge[maxm << ];
  28.  
  29. int n, m;
  30. int tot, head[maxn];
  31.  
  32. void init()
  33. {
  34. tot = ;
  35. memset(head, -, sizeof(head));
  36. }
  37.  
  38. void AddEdge(int u, int v, double w)
  39. {
  40. edge[tot].to = v;
  41. edge[tot].next = head[u];
  42. edge[tot].w = w;
  43. head[u] = tot++;
  44. }
  45.  
  46. double dist[maxn];
  47. bool vis[maxn];
  48. int cnt[maxn];
  49.  
  50. bool spfa()
  51. {
  52. queue<int> que;
  53. for (int i = ; i < n; i++)
  54. {
  55. dist[i] = ;
  56. cnt[i] = ;
  57. vis[i] = true;
  58. que.push(i);
  59. }
  60.  
  61. while (!que.empty())
  62. {
  63. int x = que.front();
  64. que.pop();
  65. vis[x] = false;
  66. for (int i = head[x]; i != -; i = edge[i].next)
  67. {
  68. int v = edge[i].to;
  69. if (dist[v] > dist[x] + edge[i].w)
  70. {
  71. dist[v] = dist[x] + edge[i].w;
  72. if (!vis[v])
  73. {
  74. que.push(v);
  75. vis[v] = true;
  76. if (++cnt[v] > n)
  77. {
  78. return true;
  79. }
  80. }
  81. }
  82. }
  83. }
  84. return false;
  85. }
  86.  
  87. bool Judge(double x)
  88. {
  89. for (int i = ; i < tot; i++)
  90. {
  91. edge[i].w -= x;
  92. }
  93. bool ok = true;
  94. if(spfa())
  95. ok = false;
  96. for (int i = ; i < tot; i++)
  97. {
  98. edge[i].w += x;
  99. }
  100. return ok;
  101. }
  102.  
  103. int iCase;
  104.  
  105. int main()
  106. {
  107. ios::sync_with_stdio(false);
  108. cin.tie();
  109. //freopen("input.txt", "r", stdin);
  110. //freopen("output.txt", "w", stdout);
  111. int T;
  112. cin >> T;
  113. while (T--)
  114. {
  115. cin >> n >> m;
  116. init();
  117. int u, v;
  118. double w;
  119. double lim = -inf;
  120. for (int i = ; i < m; i++)
  121. {
  122. cin >> u >> v >> w;
  123. u--, v--;
  124. AddEdge(u, v, w);
  125. lim = max(lim, w);
  126. }
  127. cout << "Case #" << ++iCase << ": ";
  128. if (Judge(lim + ))
  129. {
  130. cout << "No cycle found." << endl;
  131. }
  132. else
  133. {
  134. double le = , ri = lim;
  135. while (ri - le > 1e-)
  136. {
  137. double mid = (le + ri) / ;
  138. if (Judge(mid))
  139. {
  140. le = mid;
  141. }
  142. else
  143. {
  144. ri = mid;
  145. }
  146. }
  147. cout << fixed << setprecision() << le << endl;
  148. }
  149. }
  150. return ;
  151. }

UVA - 11090 - Going in Cycle!!(二分+差分约束系统)的更多相关文章

  1. UVA - 11478 - Halum(二分+差分约束系统)

    Problem  UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...

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

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

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

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

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

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

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

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

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

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

  7. UVA 11090 Going in Cycle!!

    要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...

  8. UVA 11090 - Going in Cycle!! SPFA

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  9. UVa 11090 Going in Cycle!! (Bellman_Ford)

    题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...

随机推荐

  1. Linux iptables用法与NAT

    1.相关概念 2.iptables相关用法 3.NAT(DNAT与SNAT) 相关概念 防火墙除了软件及硬件的分类,也可对数据封包的取得方式来分类,可分为代理服务器(Proxy)及封包过滤机制(IP ...

  2. 普通程序员如何转向AI方向(转)

    普通程序员如何转向AI方向   眼下,人工智能已经成为越来越火的一个方向.普通程序员,如何转向人工智能方向,是知乎上的一个问题.本文是我对此问题的一个回答的归档版.相比原回答有所内容增加. 一. 目的 ...

  3. 腾迅云CDN的使用

    一开始,我是想和七牛云一样,将腾迅云的对象存储作为网盘使用,不过在折腾的时间,搞不清楚腾迅云CDN的用法,最后看文档,看博客,大概了解了 这里讲两种用法,一种是结合对象存储,作一个静态网站或下载站,但 ...

  4. mpvue小程序开发之 集成第三方UI框架Vant Weapp UI

    集成进第三方的UI框架其实很简单 这里把vant-weapp的dist目录重命名为vant-weapp放在项目根目录的static文件夹下: 在src文件夹下,即我们写vue代码的位置,正在编写的页面 ...

  5. 将Python 程序打包成 .exe格式入门

    PyInstaller PyInstaller 是一个十分有用的第三方库,可以用来打包 python 应用程序,打包完的程序就可以在没有安装 Python 解释器的机器上运行了. 它能够在 Windo ...

  6. 配置中心框架IConfCenter

    本篇和大家分享的是一个简易配置中心框架IConfCenter,框架是利用空余时间写的,主要以配置文件+redis存储方式作为数据同步驱动,目前支持的配置文件格式有 .properties 和 .con ...

  7. .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 =>  Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...

  8. Android中EditText显示明文与密文的两种方式

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录输入框显示.隐藏密码的简单布局以及实现方式. 效果图    代码分析 方式一 /**方式一:*/ private void sh ...

  9. Android序列化

    1.序列化的目的 (1)永久的保存对象数据(将对象数据保存在文件当中,或者是磁盘中 (2)通过序列化对象在网络中传递对象 (3)通过序列化对象在进程间传递 (4)在Intent之间,基本的数据类型直接 ...

  10. AsyncTask原理

    一.概述 Android开发中我们通常让主线程负责前台用户界面的绘制以及响应用户的操作,让工作者线程在后台执行一些比较耗时的任务.Android中的工作者线程主要有AsyncTask.IntentSe ...