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常用终端快捷键
CTRL+k:删除从光标到行尾的部分 CTRL+u:删除从光标到行首的部分 ALT+d:删除从光标到当前单词结尾的部分 CTRL+w:删除从光标到当前单词开头的部分 CTRL+a:将光标移到行首 CT ...
- ScheduleThreadPoolExecutor源码分析
ScheduleThreadPoolExecutor源码分析(一) Java中ScheduleThreadPoolExecutor主要用于执行延迟任务或者按照一定的频率执行任务.其中scheduleA ...
- 为什么用linear regression可以做classification
输出空间 错误衡量方式 能不能直接用linear regression for classification 当成一个分类器回传回去 heuristic(启发式的:试探) 错误衡量 complexit ...
- localStorage.ie6.js
!window.localStorage && function() { window.localStorage = {}; var prefix = 'data-userdata' ...
- centos 6.5 openfire安装
1.下载:http://igniterealtime.org/downloads/download-landing.jsp?file=openfire/openfire-3.9.3-1.i386.rp ...
- Asp.net服务器控件在IE10下的不兼容问题
Asp.net服务器控件在IE10下的不兼容问题 时间:2013-05-16 09:07点击: 89 次 [大 中 小] 相信很多使用IE10的童鞋们已经发现了这个问题,以下是本人在IE10标准模式下 ...
- C# C/S系统软件开发平台架构图(原创)
企业版V4.0 - 架构图 企业版V4.0 - 桥接功能.后台连接策略 桥接功能是指应用策略模式,由用户配置本地INI文件选择ADO直连(ADO-Direct)或者调用WCF服务接口访问远程服务器后台 ...
- 使用Markdown在博客里插入代码
今天尝试了一下在线使用Markdown编辑器写博客,发现想要实现下面这样的效果还真得折腾一会儿. <html> <head> <meta charset="ut ...
- PhoneGap + Dreamweaver 5.5 无法在模拟器中打开的问题(二)
转载:http://blog.csdn.net/dupang/article/details/8248335 按照网上的教程搭建Dreamweaver CS5.5+PhoneGap移动开发环境,在进行 ...
- Origin null is not allowed by Access-Control-Allow-Origin
http://www.cnblogs.com/accessking/archive/2012/05/12/2497000.html http://bbs.csdn.net/topics/3903099 ...