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- ...
随机推荐
- bat脚本+diskpart 脚本实现自动划分磁盘分区
我提供的脚本只是案例展示,真实场景需要自行修改.(正好我今天看到一个规范的bat脚本,我放出来,大家一起学习下) 要求:将20G的磁盘1,分出4G为主分区,4G扩展分区(2个2G逻辑分区) 试验环境: ...
- 3星|路江涌《共演战略画布》:PPT技巧级别的创新,缺实际分析案例
作者用自己的思路综合现有各种战略思想,给出企业各阶段各要素的战略分析工具.主要是2*2矩阵和双S曲线两种工具. 从书中的插图来看,这些工具在PPT演示中效果应该会不错. 作者在书中用这些工具做的分析不 ...
- gittalk报错Error
最近通过github和jekyll搭了一个博客,申请使用了gittalk的评论. 但是博客的页面一直报Error:Not found,如下 发现是gittalk中的信息填写错了,name随便写:Hom ...
- ASP.NET Core 中断请求了解一下(翻译)
ASP.NET Core知多少系列:总体介绍及目录 本文所讲方式仅适用于托管在Kestrel Server中的应用.如果托管在IIS和IIS Express上时,ASP.NET Core Module ...
- markdown用法
Markdown 语法的目标是成为一种适用于网络的书写语言.不在 Markdown 涵盖范围之内的标签,都可以直接在文档里面用 HTML 撰写.不需要额外标注,只要直接加标签就可以了. 一.常用部分 ...
- Servlet不再是烦恼
Servlet 一.什么是Servlet? Servlet是在服务器上运行的小程序,也就是一个Java类,但比较特殊,不需要new,自动就可以运行.也有创建.垃圾回收和销毁过程.Servlet是Jav ...
- 学python走过的坑 三 不能实现的浏览器缩放功能
公司一个项目,在启动web页面时,默认应该是打开项目页面,然后浏览器启动时总是打开一个广告页面,经理让写一个脚本,让电脑每次开机自启浏览器,且加载项目页面.浏览器自启和打开项目页面轻松搞定,这时问题来 ...
- Java数据结构和算法 - 简单排序
Q: 冒泡排序? A: 1) 比较相邻的元素.如果第一个比第二个大,就交换它们两个; 2) 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.在这一点,最后的元素应该会是最大的数; 3) 针 ...
- Navicat 导出sql问题
楼主最近碰到一个问题: 使用Navicat建立数据模型的时候使用导出sql功能导出的sql脚本放在sqlserver中执行失败,表创建成功了,但是我在Navicat中写的表注释和字段注释都没有成功, ...
- [翻译] EF Core in Action 关于这本书
Entity Framework Core in Action Entityframework Core in action是 Jon P smith 所著的关于Entityframework Cor ...