UVa11090 Going in Cycle!!
UVa11090 Going in Cycle!!
链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34650
【思路】
二分+SPFA。
二分平均值mid,如果有平均值小于mid的情况我们就缩小猜测值否则增大猜测值。如何判定?如果有平均值小于mid 则有:w1-mid+w2-mid+w3-mid+…<0 即将各条边权减mid后图中存在负圈,SPFA判负圈即可。
需要注意把int修改为double
【代码】
#include<cstdio>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = +;
const double INF=1e10;
struct Edge{
int v,next;
double w;
}e[maxn*maxn];
int en,front[maxn]; int n,m; inline void AddEdge(int u,int v,double w) {
en++; e[en].v=v; e[en].w=w; e[en].next=front[u]; front[u]=en;
} bool SPFA_NC() {
int inq[maxn],cnt[maxn];
double d[maxn];
queue<int> q;
memset(cnt,,sizeof(cnt));
memset(inq,,sizeof(inq));
//添加一个超级源节点
for(int s=;s<=n;s++)
{
d[s]=; inq[s]=; q.push(s);
}
while(!q.empty()) {
int u=q.front(); q.pop(); inq[u]=;
for(int i=front[u];i>=;i=e[i].next) {
int v=e[i].v; double w=e[i].w;
if(d[v]>d[u]+w) {
d[v]=d[u]+w;
if(!inq[v]) {
inq[v]=;
q.push(v);
if(++cnt[v]>n+) return true;
}
}
}
}
return false;
} inline void init() {
en=;
memset(front,-,sizeof(front));
} bool can(double x) {
for(int i=;i<=en;i++) e[i].w-=x;
bool ans=SPFA_NC();
for(int i=;i<=en;i++) e[i].w+=x;
return ans;
} int main() {
int T;
scanf("%d",&T);
for(int kase=;kase<=T;kase++) {
init();
scanf("%d%d",&n,&m);
double L=,R=;
int u,v; double w;
for(int i=;i<m;i++) {
scanf("%d%d%lf",&u,&v,&w);
AddEdge(u,v,w);
if(w>R) R=w;
}
printf("Case #%d: ",kase);
if(!can(R+)) printf("No cycle found.\n");
else
{
while(R-L>1e-) {
double M=L+(R-L)/;
if(can(M)) R=M; //存在平均值小于mid的回路 则缩小猜测值
else L=M;
}
printf("%.2lf\n",L);
}
}
return ;
}
UVa11090 Going in Cycle!!的更多相关文章
- UVA11090 Going in Cycle!!(二分判负环)
UVA11090 Going in Cycle!! 二分答案,用spfa判负环. 注意格式:图不一定连通. 复杂度$O(nmlog(maxw-minw))$ #include<iostream& ...
- UVA11090 Going in Cycle!! [spfa负环]
https://vjudge.net/problem/UVA-11090 平均权值最小的回路 为后面的做个铺垫 二分最小值,每条边权减去他,有负环说明有的回路平均权值小于他 spfa求负环的时候可以先 ...
- UVA11090 Going in Cycle!! (二分+SPFA推断有无负权)
Problem G: Going in Cycle!! Input: standard input Output: standard output You are given a weighted d ...
- UVA-11090 Going in Cycle!! (平均值最大回路)
题目大意:一个n个点,m条无向边的图,求出平均权值最小的回路. 题目分析:二分枚举平均值mid,只需判断是否存在平均值小于mid的回路,即判断是否有sum(wi)<mid*k (1≤i≤k),只 ...
- UVA11090 Going in Cycle (二分+判负环)
二分法+spfa判负环.如果存在一个环sum(wi)<k*x,i=0,1,2...,k,那么每条边减去x以后会形成负环.因此可用spfa来判负环. 一般spfa判负环dfs最快,用stack次之 ...
- UVA11090 Going in Cycle!! 【SPFA】
题意:求一个无向图的边权平均值最小的环 思路:假设环中Σwi/t<ans 那变形一下就是Σwi<ans*t → Σ(wi-ans)< 0 这样就可以二分答案做了 #include & ...
- 1.1.1最短路(Floyd、Dijstra、BellmanFord)
转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...
- 最短路算法详解(Dijkstra/SPFA/Floyd)
新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...
- 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法
在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常. 意思是出现了死循环,也就是Model之间有循环包含关系: ...
随机推荐
- ubuntu下配置java环境变量
1.官网下载linux对应的jdk安装包tar.gz 2.filezilla上传tar.gz到对应ubuntu目录test下(见上一篇) 3.解压:tar -zcvf XXX.tar.gz 4.修改解 ...
- SQL Execute语法.
一,执行字符串: EXECUTE语句可以执行存放SQL语句的字符串变量,或直接执行SQL语句字符串. 语法:EXECUTE({@字符串变量|[N]’SQL语句字符串’}[+...n]) 例子:Decl ...
- LINUX nohup命令输入输出深浅进出
无论是否将 nohup命令的输出重定向到终端,输出都将附加到当前目录的 nohup.out 文件中.如果当前目录的 nohup.out 文件不可写,输出重定向到 $HOME/nohup.out 文件中 ...
- Performance tuning library cache lock & single-task message
My colleague suddenly encountered a problem today,a Database becomes very slow , and the a lot of se ...
- 枚举N行N列的自然数列
数据库环境:SQL SERVER 2005 现有一个需求,要枚举1-50个自然数,分10行5列展示.如图,
- C++primer(第五版)读书笔记&习题解答---CHAPTER 3
C++标准库类型包括:string,vector和迭代器,其中string是可变长的字符序列,vector存放的是某种给定类型对象的可变长序列,迭代器是string和vector的配套类型,常被用于访 ...
- MSSQL Server语句
随机从数据库中取出20条数据:select top 20 * from 表名 order by newid()
- html5 拖拽的简要介绍
1,首先,你要告诉计算机那个元素可以拖动,或者是一张图,或者是一个盒子,在标签里面加上 draggable="true" 2,然后,监听该元素被拖动的函数 ondragstart ...
- Windows phone 之Socket
服务器端: using System; using System.Collections.Generic; using System.Linq; using System.Text; using Sy ...
- Eclipse Git和sourceTree用法
Eclipse Git和sourceTree用法 Eclipse Git: 提交代码到git: 1.team->Repository->pull 若没有冲突: 2.team->com ...