UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路。
一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案。
二份答案,再利用利用bellman-ford判负环。
注意:
1、double:经常为了确定每个变量的类型,漏掉了某个变量,调半天心都凉了。干脆全变double。
2、没有告诉m的数据范围,要是在比赛中肯定有人问,要是reply是“read carefully”,总不能猜吧,乖乖用vector吧= =
3、原图为有向图,但不一定强连通,所以所有点要先入队才能找到全部的连通分量(就wa在这里)
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define clr(a,m) memset(a,m,sizeof(a))
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std; const int MAXN=;
const int INF =1e8;
const double eps=1e-; struct Edge{
int u,v;
double c;
}; int inq[MAXN],cnt[MAXN];
double d[MAXN];
vector<Edge>edge;
vector<int>G[MAXN]; void init(int n)
{
edge.clear();
rep(i,,n)
G[i].clear();
} void add(int u,int v,double c)
{
edge.push_back((Edge){u,v,c});
int m=edge.size();
G[u].push_back(m-);
} double build(int m)
{
int u,v;
double c;
double up=;
rep(i,,m){
scanf("%d%d%lf",&u,&v,&c);
up=max(up,c);
add(u,v,c);
}
return up;
} bool BF(int st,int n)
{
clr(inq,);
clr(cnt,);
queue<int>q;
rep(i,,n){
if(i==st)d[i]=;
else d[i]=INF;
q.push(i);
}
while(!q.empty())
{
int u=q.front();q.pop();
inq[u]=false;
int sz=G[u].size();
rep(i,,sz-){
Edge e=edge[G[u][i]];
if(d[e.v]>d[u]+e.c){
d[e.v]=d[u]+e.c;
if(!inq[e.v]){
q.push(e.v);
inq[e.v]=true;
if(++cnt[e.v]>n)
return true;
}
}
}
}
return false;
} bool test(int n,int m,double x)
{
rep(i,,m-)
edge[i].c-=x;
bool flog=BF(,n);
rep(i,,m-)
edge[i].c+=x;
return flog;
} int main()
{
int T,n,m;
scanf("%d",&T);
for(int ans=;ans<=T;ans++)
{
scanf("%d%d",&n,&m);
init(n);
double up=build(m); printf("Case #%d: ",ans);
if(!test(n,m,up+))
printf("No cycle found.\n");
else{
double l=,r=up;
while(r-l>eps)
{
double x=l+(r-l)/;
if(test(n,m,x))
r=x;
else
l=x;
}
printf("%.2f\n",l);
}
}
return ;
}
UVA 11090 Going in Cycle!!(二分答案+判负环)的更多相关文章
- poj 2049(二分+spfa判负环)
poj 2049(二分+spfa判负环) 给你一堆字符串,若字符串x的后两个字符和y的前两个字符相连,那么x可向y连边.问字符串环的平均最小值是多少.1 ≤ n ≤ 100000,有多组数据. 首先根 ...
- poj 3621 二分+spfa判负环
http://poj.org/problem?id=3621 求一个环的{点权和}除以{边权和},使得那个环在所有环中{点权和}除以{边权和}最大. 0/1整数划分问题 令在一个环里,点权为v[i], ...
- [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- LOJ #10084. 「一本通 3.3 练习 1」最小圈(二分+SPFA判负环)
题意描述: 见原LOJ:https://loj.ac/problem/10084 题解: 假设所求的平均最小值为X,环上各个边的权值分别为A1,A2...Ak,可以得到: X=(A1+A2+A3+.. ...
- 【题解】 [HNOI2009] 最小圈 (01分数规划,二分答案,负环)
题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点,那么一个圈的平均值为圈上k条边权的和除 ...
- 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环)
layout: post title: 训练指南 UVA - 11090(最短路BellmanFord+ 二分判负环) author: "luowentaoaa" catalog: ...
- UVA - 11090 - Going in Cycle!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- UVA 11090 - Going in Cycle!!(Bellman-Ford)
UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...
- UVA11090 Going in Cycle!!(二分判负环)
UVA11090 Going in Cycle!! 二分答案,用spfa判负环. 注意格式:图不一定连通. 复杂度$O(nmlog(maxw-minw))$ #include<iostream& ...
随机推荐
- linux下php多版本的并存实现
其实最简单的方法,就是通过nginx,生成多个php使用不同的端口,这实在简单,我写了两个版本,一个是apche服务,一个是nginx服务,使用一两个不同的版本,爽!
- Window7中Eclipse运行MapReduce程序报错的问题
按照文档:http://www.micmiu.com/bigdata/hadoop/hadoop2x-eclipse-mapreduce-demo/安装配置好Eclipse后,运行WordCount程 ...
- JS利用正则配合replace替换指定字符
替换指定字符的方法有很多,在本文为大家详细介绍下,JS利用正则配合replace是如何做到的,喜欢的朋友可以参考下 定义和用法 replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一 ...
- Codeforces Round #362 (Div. 2)->A. Pineapple Incident
A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...
- spring测试父类,使用junit-4.4.jar,spring-test.jar
@ContextConfiguration(locations = "classpath:conf/applicationContext.xml") @RunWith(Spring ...
- jsp中文件下载的实现
jsp中实现文件下载的最简单的方式是在网页上做超级链接,如:<a href="music/abc.mp3">点击下载</a>.但是这样服务器上的目录资源会直 ...
- 关联式容器(associative containers)
关联式容器(associative containers) 根据数据在容器中的排列特性,容器可分为序列式(sequence)和关联式(associative)两种. 标准的STL关联式容器分为set( ...
- hdu 3758 Factorial Simplification
这题主要是质因数分解!! 求出每个因子的幂,如果有负数,则输出-1: 如果2的幂数为0,这输出0: 最后就是开始凑阶乘了…… #include<iostream> #include< ...
- stringUtils是apache下的Java jar补充包
org.apache.commons.lang.StringUtils StringUtils中一共有130多个方法,并且都是static的, 所以我们可以这样调用StringUtils.xxx().
- 国内为什么没有好的 Stack Overflow 的模仿者?
国内为什么没有好的 Stack Overflow 的模仿者? 个人觉得, 高端的程序员会直接上stackoverflow提问, 所以国内中文的stackoverflow必然面对低端程序员. 鉴于中国程 ...