UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)
题意:
给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值?
思路:
首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少。一般这种就是二分猜平均权值了,因为环在哪也难以找出来,还有可能是一条边属于多个环。对于每个猜到的平均值,如果对应环的存在,那么这个环的每条边的权减去这个平均值之后,用spfa算法就能判断其是否有环的存在即可。
假设环上各边权值为:w1+w2+...+wk。
式子:w1+w2+...+wk<k*even 相当于 (w1-even)+(w2-even)+...(wk-even)< 0。即更新完边权后应i该是有环存在的。
对于猜测的平均权值mid,如果不能找到环,则说明mid应该更大。
#include <bits/stdc++.h>
#define LL long long
#define pii pair<int,int>
#define INF 0x7f7f7f7f
using namespace std;
const int N=;
vector<int> vect[N];
struct node
{
int from,to;
double cost;
node(){};
node(int from,int to,int cost):from(from),to(to),cost(cost){};
}edge[N];
int edge_cnt;
int big, small;
void add_node(int from,int to,double cost)
{
edge[edge_cnt]=node(from, to, cost);
vect[from].push_back(edge_cnt++);
} int inq[N], cnt[N];
double dist[N];
bool spfa(int n, double q)
{
memset(inq,,sizeof(inq));
memset(cnt,,sizeof(cnt));
deque<int> que;
for(int i=; i<=n; i++) dist[i]=0.0, inq[i]=, que.push_back(i); //因为是判断负环的,所以dist初始化为0即可。
while(!que.empty())
{
int x=que.front();que.pop_front();
inq[x]=;
for(int i=; i<vect[x].size(); i++)
{
node e=edge[vect[x][i]];
if(dist[e.to]>dist[x]+e.cost-q )
{
dist[e.to]=dist[x]+e.cost-q ;
if(!inq[e.to])
{
inq[e.to]=;
que.push_back(e.to);
if(++cnt[e.to]>n)
return true;
}
}
}
}
return false;
} double cal(int n)
{
double l=small, r=big, ans=0.0;
while(r-l>1e-)
{
double mid=(l+r)/;
if( spfa(n, mid) ) r=mid; //有负环
else l=mid;
}
return l;
} int main()
{
// freopen("input.txt", "r", stdin);
int n, m, t, a, b, c, j=;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
edge_cnt=;
for(int i=; i<=n; i++) vect[i].clear();
memset(edge,,sizeof(edge));
big=;
small=INF; for(int i=; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
add_node(a,b,c);
small=min(small,c);
big=max(big, c);
}
if( !spfa(n, big+) ) printf("Case #%d: No cycle found.\n", ++j);
else printf("Case #%d: %.2f\n", ++j, cal(n));
}
return ;
}
AC代码
UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)的更多相关文章
- UVA 11090 Going in Cycle!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- 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
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- 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!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- The Minimum Cycle Mean in a Digraph 《有向图中的最小平均权值回路》 Karp
文件链接 Karp在1977年的论文,讲述了一种\(O(nm)\)的算法,用来求有向强连通图中最小平均权值回路(具体问题请参照这里) 本人翻译(有删改): 首先任取一个节点 \(s\) ,定义 \(F ...
- UVa 11090 Going in Cycle!! (Bellman_Ford)
题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...
- UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)
题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...
随机推荐
- web服务器和应用服务器
通俗的讲,Web服务器传送(serves)页面使浏览器可以浏览,然而应用程序服务器提供的是客户端应用程序可以调用(call)的方法(methods).确切一点,你可以说:Web服务器专门处理HTTP请 ...
- Codeforces Round #130 (Div. 2) A. Dubstep
题目链接: http://codeforces.com/problemset/problem/208/A A. Dubstep time limit per test:2 secondsmemory ...
- pos机套现是怎么回事
POS机是商家为了促进消费,向银行申请的刷卡机它的主要功能是转账就是通过客户的刷卡,把相对的金额转入商户的帐户银行会根据笔数或金额向商户收取手续费非法套现就是客户并未和商户产生贸易往来,单纯通过pos ...
- 0327定时执行--存储过程--dbms_job--dbms_scheduler.create_job
--oracle job 定时执行 存储过程 --建一张测试表 create table Person( name ), sex ) ); / --创建测试的存储过程 create or replac ...
- PHP之XML节点追加操作讲解
问题: 最近研究了xml,我想知道,如何用php向已有的xml文档中添加新的节点,比如,xml文档内容如下,并保存为information.xml: <?xml version="1. ...
- 【log4net】配置文件
相关资料: http://www.cnblogs.com/dragon/archive/2005/03/24/124254.html 注意: //如果为了使得应用程序的配置文件(web/app.con ...
- lintcode:previous permutation上一个排列
题目 上一个排列 给定一个整数数组来表示排列,找出其上一个排列. 样例 给出排列[1,3,2,3],其上一个排列是[1,2,3,3] 给出排列[1,2,3,4],其上一个排列是[4,3,2,1] 注意 ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- dubbo与zookeeper安装手册
原文 示例提供者安装 (+) (#) 安装: wget http://code.alibabatech.com/mvn/releases/com/alibaba/dubbo-demo-provider ...
- Hibernate逍遥游记-第12章 映射值类型集合-001映射set(<element>)
1. 2. <?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate ...