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- ...
随机推荐
- 【English】20190429
detect发现 [dɪˈtekt] charger充电器 [ˈtʃɑːrdʒər] unable to detect charger
- php爬取微信文章内容
php爬取微信文章内容 在做官网升级的时遇到新的需求,需要将公司公众号文章显示在官网的文章模块下.但存在的问题是:微信文章的链接会失效,并且需要对文章部分内容做修改,同时要减少微信运营人员的工作量,避 ...
- WinServer配置MySQL主从同步
为什么要配置主从同步? 如果一台数据库服务器挂了,还有一个备用 为了方便配置,我采用两台WinServer2003虚拟机: 1.前期准备工作:安装好镜像文件,VMTOOLS,MySQL5.5 我这里以 ...
- (转)学习MySQL优化原理,这一篇就够了!
原文:https://mp.weixin.qq.com/s__biz=MzI4NTA1MDEwNg==&mid=2650763421&idx=1&sn=2515421f09c1 ...
- 【Python3爬虫】自动查询天气并实现语音播报
一.写在前面 之前写过一篇用Python发送天气预报邮件的博客,但是因为要手动输入城市名称,还要打开邮箱才能知道天气情况,这也太麻烦了.于是乎,有了这一篇博客,这次我要做的就是用Python获取本机I ...
- RDIFramework.NET V3.3 WinForm版角色授权管理新增角色对操作权限项、模块起止生效日期的设置
在实际应用在我们可能会有这样的需求,某个操作权限项(按钮)或菜单在某个时间范围内可以让指定角色访问.此时通过我们的角色权限扩展设置就可以办到. 在我们框架V3.3 WinForm版全新增加了角色权限扩 ...
- 一次node-sass安装记录
node-sass的版本是3.9.3 Please restart this script from an administrative PowerShell! 在当前powershell中执行下命令 ...
- .net core EFcore model生成数据
创建数据库 (扫盲贴还劳烦大神们勿喷,谢谢) 打开数据库 输入如下代码 创建数据库 CREATE DATABASE [Blogging]; GO USE [Blogging]; GO CREATE T ...
- C#创建IIS站点及相应的应用程序池,支持IIS6.0+Windows Server 2003. 使用Builder设计模式
测试项目结构: PS:IIS6UtilsBuilder, IIS7UtilsBuilder,IISUtilsBuilder以及IISDirector为Builder设计模式实现的核心代码.Progra ...
- ACM字符串输入问题
坑死了..竟然被这个问题困扰了大半个学期,今天搜来翻去终于弄明白了一些,以后固定用这几种用法好了不然总出错QAQ实际测试例子就没放了,死记这里就够用了T-T 概念: gets()函数:用来从标准输入设 ...