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!!的更多相关文章

  1. UVA11090 Going in Cycle!!(二分判负环)

    UVA11090 Going in Cycle!! 二分答案,用spfa判负环. 注意格式:图不一定连通. 复杂度$O(nmlog(maxw-minw))$ #include<iostream& ...

  2. UVA11090 Going in Cycle!! [spfa负环]

    https://vjudge.net/problem/UVA-11090 平均权值最小的回路 为后面的做个铺垫 二分最小值,每条边权减去他,有负环说明有的回路平均权值小于他 spfa求负环的时候可以先 ...

  3. UVA11090 Going in Cycle!! (二分+SPFA推断有无负权)

    Problem G: Going in Cycle!! Input: standard input Output: standard output You are given a weighted d ...

  4. UVA-11090 Going in Cycle!! (平均值最大回路)

    题目大意:一个n个点,m条无向边的图,求出平均权值最小的回路. 题目分析:二分枚举平均值mid,只需判断是否存在平均值小于mid的回路,即判断是否有sum(wi)<mid*k (1≤i≤k),只 ...

  5. UVA11090 Going in Cycle (二分+判负环)

    二分法+spfa判负环.如果存在一个环sum(wi)<k*x,i=0,1,2...,k,那么每条边减去x以后会形成负环.因此可用spfa来判负环. 一般spfa判负环dfs最快,用stack次之 ...

  6. UVA11090 Going in Cycle!! 【SPFA】

    题意:求一个无向图的边权平均值最小的环 思路:假设环中Σwi/t<ans 那变形一下就是Σwi<ans*t → Σ(wi-ans)< 0 这样就可以二分答案做了 #include & ...

  7. 1.1.1最短路(Floyd、Dijstra、BellmanFord)

    转载自hr_whisper大佬的博客 [ 一.Dijkstra 比较详细的迪杰斯特拉算法讲解传送门 Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkstra常常作为其他算 ...

  8. 最短路算法详解(Dijkstra/SPFA/Floyd)

    新的整理版本版的地址见我新博客 http://www.hrwhisper.me/?p=1952 一.Dijkstra Dijkstra单源最短路算法,即计算从起点出发到每个点的最短路.所以Dijkst ...

  9. 使用JSONObject.fromObject的时候出现“There is a cycle in the hierarchy”异常 的解决办法

    在使用JSONObject.fromObject的时候,出现“There is a cycle in the hierarchy”异常.   意思是出现了死循环,也就是Model之间有循环包含关系: ...

随机推荐

  1. Java设计模式--单列设计模式

    设计模式:解决某一类问题行知最有效的方法.java有23种设计模式 单列设计模式: 解决一个类在内存中只存在一个对象 思路:(要保证对象的唯一性) 1.为了避免其它程序建立该对象,先禁止替他类创建改对 ...

  2. C#当中的多线程_任务并行库(中)

    发现自己有点懒了!也可能是越往后越难了,看书理解起来有点费劲,所以这两天就每天更新一点学习笔记吧. 4.5 将APM模式转化为任务 书上提供的三种方式 方式一: class Program       ...

  3. Ubuntu Server下建立VPN服务器 pptp 模式的方法

    对于想要在外部访问内部的网络,除了在防火墙上开启相应服务器所对应的端口,最好的方法应该是建立VPN-Server,使得用户可以在外网任何一台计算机上拨入到内网中进行操作,而且VPN可以记录详细的日志, ...

  4. MyBatis的学习总结四:实现关联表查询【参考】

    一.一对一的表关联查询(edi_test_task  和  edi_task_detail) 例子:一条任务明细对一条任务记录 对应的sql的映射xml文件如下: <?xml version=& ...

  5. Python:对象

    #!/usr/bin/python3 #对象实例 class Person: num=200 def __init__(self,name,sex): self.name=name self.sex= ...

  6. php验证码封装

    /** * 生成验证码 * @param int $type 1: 纯数字,2:纯字母,3:数字与字母混合  * @param int $length * @return string */funct ...

  7. 图像处理简单实例[OpenCV 笔记1]

    几个入门的简单程序,和对应的CMakeList, 虽然简单重新测一下写一下也是好的. CMake教程传送门 图像显示 ShowImage.cxx #include <opencv2/opencv ...

  8. php5.4安装ecshopphp5.4问题及解决

    includes/cls_template.php line422 将 $tag_sel = array_shift(explode(" ", $tag)); 这句话拆开为两句. $tag_exp = ...

  9. python 元组问题解决

    a = (1,2,{'k1':'b2'}) a[2]['k1'] = 5 print(a) (1, 2, {'k1': 5}) 为什么元素'b2' = 5 应该是元素'k1' = 5 求解 a[2][ ...

  10. namenode启动参数

    namenode启动参数:-Xmx153600m -Xms153600m -Xmn4096m -verbose:gc -Xloggc:$LOG_DIR/namenode.gc.log -XX:Erro ...