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!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
随机推荐
- WordPress插件开发实例教程 - 版权插件
说明:本教程仅限学习,高手请绕道 开发程序:WordPress 3.9-RC1 使用主题:Twenty Fourteen 在开始之前,需要注意三件事情 I.给插件取一个个性化的名字,越个性化越好,以防 ...
- 一个xib钟多个Cell
在实际开发中,有时候需要在一个xib钟拖几个cell,注意连线过程的object选择,不然出现没反应的现象. 例如: 在下图中,我在一个xib钟创建了五个cell,他们分别是:收货地址.订单信息.支付 ...
- IOS网络第二天 - 01-基本的HTTP请求
***************** #import "HMViewController.h" #import "MBProgressHUD+MJ.h" @int ...
- Final-阶段站立会议1
组名:天天向上 组长:王森 组员:张政.张金生.林莉.胡丽娜 代码地址:HTTPS:https://git.coding.net/jx8zjs/llk.git SSH:git@git.coding.n ...
- bash 中的变量
bash 中的变量 Linux command line 笔记 变量无需声明,自动创建 var=abc #变量a赋值为字符串abc var="hello world" #字符串里有 ...
- xampp使用phpunit
1.将xampp/php的pear文件夹里面的phpunit文件夹复制到htdocs目录下 2.复制xampp/php的phpunit.bat到需要测试的目录 3.使用cmd命令切换至phpunit. ...
- C++程序设计(三)
1. 运算符重载 目的:对抽象数据类型也能够直接使用C++提供的运算符.使得程序更简洁,代码更容易理解. 运算符重载的实质是函数重载 返回值类型 operator 运算符(形参表) { -- } 运算 ...
- alibaba的FastJson(高性能JSON开发包),fastjson 使用demo
这是关于FastJson的一个使用Demo,在Java环境下验证的 class User{ private int id; private String name; public int getId( ...
- 弹出框以及提示插件lghdialog.js的使用
以下使用方法 swfupload的使用
- ios - loadView与viewDidload
从打印结果可以看出,先调用loadView再调用viewDidload 代码: // // ViewController.m // test // // Created by 裴波波 on 16/4/ ...