题目大意:一个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. Mysql binlog 安全删除(转载)

    简介: 如果你的 Mysql 搭建了主从同步 , 或者数据库开启了 log-bin 日志 , 那么随着时间的推移 , 你的数据库 data 目录下会产生大量的日志文件 shell > ll /u ...

  2. rf调参小结

    转自http://www.cnblogs.com/pinard/p/6160412.html 1. scikit-learn随机森林类库概述 在scikit-learn中,RF的分类类是RandomF ...

  3. python全栈开发从入门到放弃之socket并发编程多线程GIL

    一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...

  4. 关于cgi、FastCGI、php-fpm、php-cgi(复制)

    首先,CGI是干嘛的?CGI是为了保证web server传递过来的数据是标准格式的,方便CGI程序的编写者. web server(比如说nginx)只是内容的分发者.比如,如果请求/index.h ...

  5. Optimal Marks SPOJ - OPTM (按位枚举-最小割)

    题意:给一张无向图,每个点有其点权,边(i,j)的cost是\(val_i\ XOR \ val_j\).现在只给出K个点的权值,求如何安排其余的点,使总花费最小. 分析:题目保证权值不超过32位整型 ...

  6. selenium+python学习总结

    学习了一个月的selenium+python,终于学有所成,下面以一个简单的项目来总结学习所得. 1.         项目结构 在项目结构中,大家要注意到:每一个源文件夹中都要有一个__init__ ...

  7. TOSCA自动化测试工具--new Project

    1.在默认页面点击Project 进入new project页面 2.第一步出来的页面上点击new 3. 第2步弹出的页面上选择路径,project 名 3.OK之后就创建好了

  8. Tomcat源码

    1.Connector   Container:Engine,Host,Context,Wrapper(责任链的设计模式)   Valve: Tomcat 中一个最容易发现的设计模式就是责任链模式,这 ...

  9. netty9---使用编码解码器

    客户端: package com.client; import java.net.InetSocketAddress; import java.util.Scanner; import java.ut ...

  10. 在vi中打开多个文件,复制一个文件中多行到另一个文件中

    :set number 查看行号1.vi a.txt b.txt或者vi *.txt 2.文件间切换 :n切换到下一个文件,:wn保存再切换 :N到上一个文件,:wN保存再切换 :.=看当前行 3.比 ...