UVA-11090 Going in Cycle!! (平均值最大回路)
题目大意:一个n个点,m条无向边的图,求出平均权值最小的回路。
题目分析:二分枚举平均值mid,只需判断是否存在平均值小于mid的回路,即判断是否有sum(wi)<mid*k (1≤i≤k),只需判断是否有sum(wi-mid)<0,只需将边权值减去mid后,判断是否存在负环。
代码如下:
# include<iostream>
# include<cstdio>
# include<queue>
# include<vector>
# include<cstring>
# include<algorithm>
using namespace std; const double inf=1e30;
struct Edge
{
int to,nxt;
double w;
};
Edge e[10000];
double dist[55];
int vis[55],inq[55],head[55],n,cnt; void add(int u,int v,double w)
{
e[cnt].to=v;
e[cnt].w=w;
e[cnt].nxt=head[u];
head[u]=cnt++;
} bool judge(double M)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(inq,0,sizeof(inq));
for(int i=0;i<n;++i){
dist[i]=0;
vis[i]=inq[i]=1;
q.push(i);
}
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=0;
for(int i=head[u];i!=-1;i=e[i].nxt){
if(dist[e[i].to]>dist[u]+e[i].w-M){
dist[e[i].to]=dist[u]+e[i].w-M;
if(!inq[e[i].to]){
q.push(e[i].to);
inq[e[i].to]=1;
if(++vis[e[i].to]>n)
return true;
}
}
}
}
return false;
} int main()
{
int T,m,a,b,c,cas=0;
scanf("%d",&T);
while(T--)
{
cnt=0;
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
int maxn=0;
while(m--)
{
scanf("%d%d%d",&a,&b,&c);
add(--a,--b,c+0.0);
maxn=max(maxn,c);
}
printf("Case #%d: ",++cas);
if(!judge(maxn+1.0)){
printf("No cycle found.\n");
continue;
}
double l=0.0,r=(double)maxn;
while(r-l>1e-3){
double m=l+(r-l)/2;
if(judge(m)) r=m;
else l=m;
}
printf("%.2lf\n",l);
}
return 0;
}
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!!(二分+差分约束系统)
Problem UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...
- 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!! 环平均权值(bellman-ford,spfa,二分)
题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...
- 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!!(二分答案+判负环)
在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...
- UVA 11090 Going in Cycle!!
要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...
- 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> # ...
随机推荐
- sql server 复制表从一个数据库到另一个数据库
sql server 复制表从一个数据库到另一个数据库 /*不同服务器数据库之间的数据操作*/ --创建链接服务器 exec sp_addlinkedserver 'ITSV ', ' ', 'SQL ...
- Restful概念
文章节选自: http://www.ruanyifeng.com/blog/2011/09/restful https://www.zhihu.com/question/28557115/answer ...
- mongo启动
mongo启动 删除data目录里的mongo.lock bin 目录里执行 net start MongoDB
- 特性(property)/静态方法(staticmethod)/类方法(classmethod)/__str__的用法
property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 1 import math 2 class Circle: 3 def __init__(self,radius): #圆的 ...
- Tomcat的工作模式和运行模式
(1)工作模式 Tomcat作为servlet容器,有三种工作模式: 1.独立的servlet容器,servlet容器是web服务器的一部分: 2.进程内的servlet容器,servlet容器是作为 ...
- FactoryBean
总结自:https://www.cnblogs.com/davidwang456/p/3688250.html Spring中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即xxxF ...
- GSM900TCP/UDP连接
TCP发送:AT+CIPSTART="TCP","122.0.114.244",1001返回:OK CONNECT OK 发送: AT+CIPSEND > ...
- spring MVC Action里面怎么设置UTF-8编码集
/* 编码转换,确保写数据库的时候不会出现乱码 */ public class CodingConvert{ public CodingConvert(){ // } public String to ...
- lua5.3中luaL_setfunc设置upvalue的用法示例
缘起 luaL_setfuncs 这个函数可以注册c函数到lua,另外还可以设置闭包函数使用的变量upvalue. 我没有用过,在 云风的skynet 才第一次见过,于是写个例子实际使用以下. 函数原 ...
- SaltStack本地管理无master模式-第八篇
Salt本地管理应用场景 1.在边缘节点服务器非常少没有Salt-master 2.零售店,电影院等弱网络环境没有Salt-master 3.快速部署单个服务没有Salt-master 实现 一,安装 ...