UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
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
Sample Output
Case #1: No cycle found.
Case #2: 2.50
题解:差分约束系统板子题,配上二分很容易解决,这里的spfa和一般的spfa稍有区别,原因我在UVA 11478的博客里解释过了,不再赘述,这种写法会比分别以每个点为源点跑高效不少,目前这份代码耗时50ms,但是分别以每个点为源点跑了200ms。
- #include <bits/stdc++.h>
- using namespace std;
- #define REP(i, n) for (int i = 1; i <= (n); i++)
- #define sqr(x) ((x) * (x))
- const int maxn = + ;
- const int maxm = + ;
- const int maxs = + ;
- typedef long long LL;
- typedef pair<int, int> pii;
- typedef pair<double, double> pdd;
- const LL unit = 1LL;
- const int INF = 0x3f3f3f3f;
- const LL mod = ;
- const double eps = 1e-;
- const double inf = 1e15;
- const double pi = acos(-1.0);
- struct Edge
- {
- int to, next;
- double w;
- } edge[maxm << ];
- int n, m;
- int tot, head[maxn];
- void init()
- {
- tot = ;
- memset(head, -, sizeof(head));
- }
- void AddEdge(int u, int v, double w)
- {
- edge[tot].to = v;
- edge[tot].next = head[u];
- edge[tot].w = w;
- head[u] = tot++;
- }
- double dist[maxn];
- bool vis[maxn];
- int cnt[maxn];
- bool spfa()
- {
- queue<int> que;
- for (int i = ; i < n; i++)
- {
- dist[i] = ;
- cnt[i] = ;
- vis[i] = true;
- que.push(i);
- }
- while (!que.empty())
- {
- int x = que.front();
- que.pop();
- vis[x] = false;
- for (int i = head[x]; i != -; i = edge[i].next)
- {
- int v = edge[i].to;
- if (dist[v] > dist[x] + edge[i].w)
- {
- dist[v] = dist[x] + edge[i].w;
- if (!vis[v])
- {
- que.push(v);
- vis[v] = true;
- if (++cnt[v] > n)
- {
- return true;
- }
- }
- }
- }
- }
- return false;
- }
- bool Judge(double x)
- {
- for (int i = ; i < tot; i++)
- {
- edge[i].w -= x;
- }
- bool ok = true;
- if(spfa())
- ok = false;
- for (int i = ; i < tot; i++)
- {
- edge[i].w += x;
- }
- return ok;
- }
- int iCase;
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie();
- //freopen("input.txt", "r", stdin);
- //freopen("output.txt", "w", stdout);
- int T;
- cin >> T;
- while (T--)
- {
- cin >> n >> m;
- init();
- int u, v;
- double w;
- double lim = -inf;
- for (int i = ; i < m; i++)
- {
- cin >> u >> v >> w;
- u--, v--;
- AddEdge(u, v, w);
- lim = max(lim, w);
- }
- cout << "Case #" << ++iCase << ": ";
- if (Judge(lim + ))
- {
- cout << "No cycle found." << endl;
- }
- else
- {
- double le = , ri = lim;
- while (ri - le > 1e-)
- {
- double mid = (le + ri) / ;
- if (Judge(mid))
- {
- le = mid;
- }
- else
- {
- ri = mid;
- }
- }
- cout << fixed << setprecision() << le << endl;
- }
- }
- return ;
- }
UVA - 11090 - Going in Cycle!!(二分+差分约束系统)的更多相关文章
- UVA - 11478 - Halum(二分+差分约束系统)
Problem UVA - 11478 - Halum Time Limit: 3000 mSec Problem Description You are given a directed grap ...
- UVA 11090 - Going in Cycle!!(Bellman-Ford)
UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- UVA 11090 Going in Cycle!! SPFA判断负环+二分
原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- UVA 11090 Going in Cycle!!
要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...
- UVA 11090 - Going in Cycle!! SPFA
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- UVa 11090 Going in Cycle!! (Bellman_Ford)
题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...
随机推荐
- Linux iptables用法与NAT
1.相关概念 2.iptables相关用法 3.NAT(DNAT与SNAT) 相关概念 防火墙除了软件及硬件的分类,也可对数据封包的取得方式来分类,可分为代理服务器(Proxy)及封包过滤机制(IP ...
- 普通程序员如何转向AI方向(转)
普通程序员如何转向AI方向 眼下,人工智能已经成为越来越火的一个方向.普通程序员,如何转向人工智能方向,是知乎上的一个问题.本文是我对此问题的一个回答的归档版.相比原回答有所内容增加. 一. 目的 ...
- 腾迅云CDN的使用
一开始,我是想和七牛云一样,将腾迅云的对象存储作为网盘使用,不过在折腾的时间,搞不清楚腾迅云CDN的用法,最后看文档,看博客,大概了解了 这里讲两种用法,一种是结合对象存储,作一个静态网站或下载站,但 ...
- mpvue小程序开发之 集成第三方UI框架Vant Weapp UI
集成进第三方的UI框架其实很简单 这里把vant-weapp的dist目录重命名为vant-weapp放在项目根目录的static文件夹下: 在src文件夹下,即我们写vue代码的位置,正在编写的页面 ...
- 将Python 程序打包成 .exe格式入门
PyInstaller PyInstaller 是一个十分有用的第三方库,可以用来打包 python 应用程序,打包完的程序就可以在没有安装 Python 解释器的机器上运行了. 它能够在 Windo ...
- 配置中心框架IConfCenter
本篇和大家分享的是一个简易配置中心框架IConfCenter,框架是利用空余时间写的,主要以配置文件+redis存储方式作为数据同步驱动,目前支持的配置文件格式有 .properties 和 .con ...
- .NET Core微服务之基于Steeltoe使用Zipkin实现分布式追踪
Tip: 此篇已加入.NET Core微服务基础系列文章索引 => Steeltoe目录快速导航: 1. 基于Steeltoe使用Spring Cloud Eureka 2. 基于Steelt ...
- Android中EditText显示明文与密文的两种方式
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 记录输入框显示.隐藏密码的简单布局以及实现方式. 效果图 代码分析 方式一 /**方式一:*/ private void sh ...
- Android序列化
1.序列化的目的 (1)永久的保存对象数据(将对象数据保存在文件当中,或者是磁盘中 (2)通过序列化对象在网络中传递对象 (3)通过序列化对象在进程间传递 (4)在Intent之间,基本的数据类型直接 ...
- AsyncTask原理
一.概述 Android开发中我们通常让主线程负责前台用户界面的绘制以及响应用户的操作,让工作者线程在后台执行一些比较耗时的任务.Android中的工作者线程主要有AsyncTask.IntentSe ...