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!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
随机推荐
- java 关于hashcode和equals的测试
package thinkingInJava; import java.util.HashMap; /* * 测试在向以hashcode为基础的集合(HashSet , HashMap , HashT ...
- 打开网页自动弹出QQ对话框的实现办法
Ian今天偶然进入一个公司的企业网站,然后QQ聊天窗口这里马上就弹出与那个公司客服聊天的窗口.怀着好奇的心态,Ian分析了该公司的网站源码,发现了实现网页弹出qq对话框的原理与实现方法,相信此时此刻你 ...
- C遇到的问题
1. stdout-------printf输出到stdout,并在终端打印 stderr--------perror错误输出到stderr,并在终端打印 2. usleep(1)//代表一微妙 sl ...
- 【iCore3 双核心板_ uC/OS-III】例程五:软件定时器
实验指导书及代码包下载: http://pan.baidu.com/s/1eSHenjs iCore3 购买链接: https://item.taobao.com/item.htm?id=524229 ...
- C#网络编程之--TCP协议(一)
TCP 是面向连接的传输协议 面向连接,其实就好比,A打电话给B,如果B接听了,那么A和B之间就的通话,就是面向连接的 TCP 是全双工的传输协议 全双工,这个理解起来也很简单,A打电话给B,B接听电 ...
- VS2012编译VS2010版本的过程报错解决
C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Platforms\Win32C:\Program Files\MSBuild\Microsoft C:\Pro ...
- javac编译、运行
java源码(包结构) 源码存放位置:C:/Users/liaolongjun/DeskTop/java/ package test; import test.sub.F; public class ...
- C++ 扫描文件夹下所有文件
void GetFilesFromDirectory(std::vector<std::string> &files, const char *directoryPath) { s ...
- mysql 查表失败
我们数据库迁移,我进数据库的目录都需要拷贝什么到新的数据库才可以用,我直接拷贝的库报错了[]北京- 2016/1/26 16:07:33 mysql> use payment;Database ...
- SQL调优
# 问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...