题目大意:一个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!! (平均值最大回路)的更多相关文章

  1. UVA 11090 - Going in Cycle!!(Bellman-Ford)

    UVA 11090 - Going in Cycle!! option=com_onlinejudge&Itemid=8&page=show_problem&category= ...

  2. UVA - 11090 - Going in Cycle!!(二分+差分约束系统)

    Problem  UVA - 11090 - Going in Cycle!! Time Limit: 3000 mSec Problem Description You are given a we ...

  3. UVA 11090 - Going in Cycle!! SPFA

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  4. UVa 11090 Going in Cycle!!【Bellman_Ford】

    题意:给出n个点m条边的加权有向图,求平均值最小的回路 自己想的是用DFS找环(真是too young),在比较找到各个环的平均权值,可是代码实现不了,觉得又不太对 后来看书= =好巧妙的办法, 使用 ...

  5. UVA 11090 Going in Cycle!! 环平均权值(bellman-ford,spfa,二分)

    题意: 给定一个n个点m条边的带权有向图,求平均权值最小的回路的平均权值? 思路: 首先,图中得有环的存在才有解,其次再解决这个最小平均权值为多少.一般这种就是二分猜平均权值了,因为环在哪也难以找出来 ...

  6. UVA 11090 Going in Cycle!! SPFA判断负环+二分

    原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...

  7. UVA 11090 Going in Cycle!!(二分答案+判负环)

    在加权有向图中求平均权值最小的回路. 一上手没有思路,看到“回路”,第一想法就是找连通分量,可又是加权图,没什么好思路,那就转换题意:由求回路权值->判负环,求最小值->常用二分答案. 二 ...

  8. UVA 11090 Going in Cycle!!

    要求给定的图的中平均权值最小的环,注意处理自环的情况就能过了. 按照w1+w2+w3+….wn < n*ave的不等式,也就是(w1-ave) + (w2-ave) +…..(wn-ave) & ...

  9. UVa 11090 Going in Cycle!! (Bellman_Ford)

    题意:给定一个加权有向图,求平均权值最小的回路. 析:先十分答案,假设答案是 ans,那么有这么一个回路,w1+w2+w3+...+wk < k*ans,这样就是答案太大,然后移项可得,(w1- ...

  10. UVA 11090 Going in Cycle!!(Bellman-Ford推断负圈)

    题意:给定一个n个点m条边的加权有向图,求平均权值最小的回路. 思路:使用二分法求解.对于每个枚举值mid,推断每条边权值减去mid后有无负圈就可以. #include<cstdio> # ...

随机推荐

  1. python16_day26【crm 增、改、查】

    一.增加 二.修改 三.保存

  2. xe7开发的安卓程序,体积宏大--112M!

    原因没找到,但似乎可以这样解决: 解决过程:因为代码很少,所以我重新建立一个空白程序,把代码复制过去,一字不差.重新编译, 关键的时刻到了:不要连上真机,在编译完成时,系统提示是否要启动android ...

  3. Element 中表单非必填数据项 必须为数字的验证问题

    Element-ui 的el-form组建中,自带基本的验证功能,比如某些项必填的验证,直接加入rules 规则中即可,如下实例: 在页面中书写如下: <el-form-item label=& ...

  4. Castle连接多数据库配置

    ActiveRecord 的多数据库配置基本沿袭了 NHibernate 的思想,只不过在配置文件结构上作了些调整.1. 采用继承方式,归纳使用同一数据库的类型.比如 A.B.C.D.E 中 A.B连 ...

  5. ionic 打包

    比如我下载一个官方的小Demoionic start myApp tabs(我默认你已经配置好了安卓的开发环境.安装了全局的ionic.cordova)   首先,你需要给项目添加安卓平台的支持ion ...

  6. hdu6121 Build a tree

    地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=6121 题面: Build a tree Time Limit: 2000/1000 MS (J ...

  7. head中的title显示在body中

    今天遇到一个问题,就是title中的内容会显示在body中 <head> <title>324234</title> </head> 网上搜了一下是说编 ...

  8. Selenium+Python定位实例

    常见的定位方式参见:http://www.cnblogs.com/ranxf/p/7928732.html 1.ID定位(find_element_by_id) <input class=&qu ...

  9. oracle同一个库上面,不同用户相互赋予权限

    用法: 有两个用户:user1和user2,都是在库TEST上,分别有表user1.table1,user2.table2 但是用user1登录的时候,user2上表就不能用,此时就可以使用grant ...

  10. C#基础--多线程

    一.微软早期操作系统中的问题 在早期的操作系统中,应用程序都是在同一个地址空间中运行的,每个程序的数据其它程序都是可见的,并且因为早期CPU是单内核 的所以所有的执行都是线性的.这就引出两个问题: 第 ...