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> # ...
随机推荐
- 垃圾回收 GC
垃圾回收器的回收的对象: 垃圾回收只回收托管堆中的内存 什么样的对象才会被回收? 没有变量引用的对象.没有变量引用的对象,表示可以被回收了(null. 什么时间回收? 不确定,当程序需要新内存 ...
- PAT-乙级-1047. 编程团体赛(20)
1047. 编程团体赛(20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 编程团体赛的规则为:每个参赛队由若 ...
- c# 4.0新特性一览
原文:http://www.cnblogs.com/palo/archive/2009/03/01/1400949.html 终于静下心来仔细听了一遍Anders Hejlsberg(Visual S ...
- WCF 笔记 (2) - 传输泛型 List 对象
WCF 笔记 (2) - 传输泛型 List 对象 本帖介绍怎么在 WCF 中,在 Server-side 和 Client-side 之间,传递默认无法传输的 List<T>.List& ...
- SSH Tunneling Explained
转载: http://chamibuddhika.wordpress.com/2012/03/21/ssh-tunnelling-explained/ March 21, 2012 by Buddhi ...
- Silverlight弹出层(转载)
ChildWindow为Silverlight中的弹出子窗口 可以在项目新建子窗口文件: 相互传值: //父窗体向子窗体传值,需要在ChildWindow中构造函数进行传值ChildWindowTes ...
- Linux解压 tar命令
tar [-cxtzjvfpPN] 文件与目录 ....参数:-c :建立一个压缩文件的参数指令(create 的意思):-x :解开一个压缩文件的参数指令!-t :查看 tarfile 里面的文件! ...
- C++客户端程序(socket)
// MyClient.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include "stdio.h"#include ...
- lintcode 中等题:permutations 全排列
题目 全排列 给定一个数字列表,返回其所有可能的排列. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一个列表[1,2,3],其全排列为: [ [1,2,3], [1,3,2], [2,1,3 ...
- lintcode :最长上升连续子序列
题目: 最长上升连续子序列 给定一个整数数组(下标从 0 到 n-1, n 表示整个数组的规模),请找出该数组中的最长上升连续子序列.(最长上升连续子序列可以定义为从右到左或从左到右的序列.) 样例 ...