Uva 11090 在环中
题目链接:http://vjudge.net/contest/143318#problem/A
题意: 求平均权值最小的回路。
分析:
平均权值不可能超过最大边,二分查,然后,由于是平均权值,就可以转换一下。
是否存在平均权值 < mid 的回路: w1 + w2 +.. +wk < k*mid
(w1-mid) + (w2-mid) +... + (wk-mid)<0;
只要精度够。 这个式子,就是一个负环,只要改变一下各条边的权值。
#include <bits/stdc++.h>
using namespace std; const int maxn = ; struct Edge
{
int from,to;
double dist;
}; struct BellmanFord
{
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool inq[maxn];
double d[maxn];
int p[maxn];
int cnt[maxn]; void init(int n)
{
this->n = n;
for(int i = ; i < n; i++) G[i].clear();
edges.clear();
} void AddEdge(int from, int to, double dist)
{
edges.push_back((Edge)
{
from, to, dist
});
m = edges.size();
G[from].push_back(m-);
} bool negativeCycle()
{
queue<int> Q;
memset(inq, , sizeof(inq));
memset(cnt, , sizeof(cnt));
for(int i = ; i < n; i++)
{
d[i] = ;
inq[] = true;
Q.push(i);
} while(!Q.empty())
{
int u = Q.front();
Q.pop();
inq[u] = false;
for(int i = ; i < G[u].size(); i++)
{
Edge& e = edges[G[u][i]];
if(d[e.to] > d[u] + e.dist)
{
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
if(!inq[e.to])
{
Q.push(e.to);
inq[e.to] = true;
if(++cnt[e.to] > n) return true;
}
}
}
}
return false;
}
}; BellmanFord solver; bool test(double x)
{
for(int i=; i<solver.m; i++)
{
solver.edges[i].dist -= x;
} bool ret = solver.negativeCycle();
for(int i=; i<solver.m; i++)
{
solver.edges[i].dist+=x;
}
return ret;
} int main()
{
int t;
scanf("%d",&t);
for(int kase = ; kase<=t; kase++)
{
int n,m;
scanf("%d%d",&n,&m);
solver.init(n);
double ub = ;
for(int i=; i<m; i++)
{
int u,v;
double w;
scanf("%d%d%lf",&u,&v,&w);
u--;
v--;
ub = max(ub,w);
solver.AddEdge(u,v,w);
}
printf("Case #%d: ",kase);
if(!test(ub+)) printf("No cycle found.\n"); else
{
double L = ,R=ub;
while(R-L>1e-)
{
double M = L +(R-L)/; if(test(M)) R = M;
else L = M; }
printf("%.2f\n",L);
}
} return ;
}
Uva 11090 在环中的更多相关文章
- 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!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- 在环中(Going in Cycle!!, UVa 11090)
[题目描述] 给定一个 n 个点 m 条边的加权有向图,求平均权值最小的回路. [输入格式] 输入第一行为数据组数 T .每组数据第一行为图的点数 n 和边数 m (n ≤ 50).以下 m 行每行3 ...
- 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!! SPFA
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- uva 11090
I I U P C 2 0 0 6 Problem G: Going in Cycle!! Input: standard input Output: standard output You are ...
- UVa 11090 Going in Cycle!!【Bellman_Ford】
题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
随机推荐
- 20145337《Java程序设计》第三周学习总结
20145337 <Java程序设计>第三周学习总结 教材学习内容总结 类与对象 类与对象的关系:要产生对象必须先定义类,类是对象的设计图,对象是类的实例.我觉得在视频中对类与对象关系的描 ...
- IOS第八天(1:UITableViewController团购,数据转模型,xib显示数据)
******HMTg.h 模型数据 #import <Foundation/Foundation.h> @interface HMTg : NSObject @property (nona ...
- jquery send(data) 对data的处理
// Convert data if not already a string if ( s.data && s.processData && typeof s.dat ...
- String-自定义功能
<script> /* *发现js中的String对象有限,想要对字符串操作的其他功能. *比如:去除字符串两端的空格.这时只能自.定义 */ //去除字符串两端的空格 function ...
- PLSQL Developer的使用
登陆服务器 除了 sys 用户登陆之外,其他都都选Normal 新建sql 窗口 编写执行sql语句 设置字体.颜色 工具——首选项——用户界面——字体——选择 工具——首选项——用户界面——编辑器— ...
- NEC学习 ---- 模块 -文本圆角背景导航
下图是效果图: 然后, 左右两边的圆角图片和背景图片如下 (因为截图工具的原因, 可能图片不是很清晰. 这个图片有4个部分, 分别是中间的背景图, 左右圆角以及栏目分隔白线) 思路: 利用inline ...
- jQuery extend 实现代码封装
jQuery 有两种方式封装代码 $.extend 和 $.fn.extend,我们也称为封装插件 $.extend DEMO // 封装 $.extend({ say:function(option ...
- Xcode 杂七杂八
一.Exception 的捕捉 1.message send to dealloc instance a, 输出控制台(lldb)后面输入:c + enter, 找到对应的行 b, po ...
- 关于iOS去除数组中重复数据的几种方法
关于iOS去除数组中重复数据的几种方法 在工作工程中我们不必要会遇到,在数组中有重复数据的时候,如何去除重复的数据呢? 第一种:利用NSDictionary的AllKeys(AllValues)方 ...
- Mac系统上用Node做APNS
1.安装Node,下载地址:http://nodejs.org 2.更新npm,终端命令:sudo npm update npm -g 3.安装apn,终端命令:npm install apn 4.导 ...