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> # ...
随机推荐
- C#根据日期DateTime和持续时间int找到日期
protected DateTime GetFinish(DateTime start, int duration) { return start.AddDays(duration); } prote ...
- PHPer 为什么会被 Javaer 鄙视?
最近看了知乎上的一个话题 「在工作中,为什么 Java 程序员常常瞧不起 PHP 程序员?」 个人从业多年,用过的后端语言 ASP.ASP.NET.Java.PHP.Node.js.Python,如果 ...
- POJ 3461 Oulipo(字符串匹配,KMP算法)
题意:给出几组数据,每组有字符串W和T,问你W在T中出现几次. 思路:字符串长度很大,用KMP算法. 一开始写的是:调用KMP算法查找W在T中是否匹配,若匹配,则个数+1.则接下来T的索引移动相应的距 ...
- DataRow.RowState 属性
RowState 的值取决于两个因素:已对该行执行的操作的类型,以及是否已对 DataRow 调用了 AcceptChanges. private void DemonstrateRowState() ...
- 03 - 运行OCCI测试程序遇到0xc0150002错误
通过控制面板->管理工具->Event Viewer->Windows Log->Application Logs, 可以看到可以看到是如下错误 “C:\Windows\ora ...
- struts2学习笔记(4)——数据类型转换
回过头来看昨天的那个例子. 在昨天的例子中,只转换了一个Point类,如果想转换多个Point类怎么办呢?在昨天的例子上面做一个小的修改. 首先在input.jsp页面中修改几个输入框. <s: ...
- lintcode 中等题:interleaving String 交叉字符串
题目 交叉字符串 给出三个字符串:s1.s2.s3,判断s3是否由s1和s2交叉构成. 样例 比如 s1 = "aabcc" s2 = "dbbca" - 当 ...
- 【PSR规范专题(1)】PSR-0+namespace+spl_autoload_register实现框架模型
了解命名空间 namespace是PHP5.3版本加入的新特性,用来解决在编写类库或应用程序时创建可重用的代码如类或函数时碰到的两类问题: 用户编写的代码与PHP内部的类/函数/常量或第三方类/函数/ ...
- NewPascal(也许只是对FreePascal的一种封装)
NewPascal is a breath of fresh air and long tradition! NewPascal offers a ready-to-be-used and up-to ...
- 一个java的DES加解密类转换成C#
原文:一个java的DES加解密类转换成C# 一个java的des加密解密代码如下: //package com.visionsky.util; import java.security.*; //i ...