#结论#洛谷 3199 [HNOI2009]最小圈
题目
求有向图最小平均权值回路。
\(n\leq 3*10^3,m\leq 10^4\)
分析
设 \(f_k(x)\) 表示从点 \(x\) 出发恰好走 \(k\) 条边的最短路,
那么答案就是 \(\min_{x=1}^n\max_{k=0}^{n-1}\frac{f_n(x)-f_k(x)}{n-k}\)
所以直接 \(O(nm)\) 就可以了,证明见_rqy dalao的博客
0/1分数规划的方法理论应该会T这里就不写了,不过只要任意点都可以作为起点那么初始值都为0就可以了
代码
#include <cstdio>
#include <cctype>
#include <cstring>
#define rr register
using namespace std;
const int N=3411; typedef double db;
int as[N],n,m; db f[N][N],ans=1e15;
struct node{int y; db w; int next;}e[N*3];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
inline void Max(db &x,db y){x=x>y?x:y;}
inline void Min(db &x,db y){x=x<y?x:y;}
signed main(){
n=iut(); m=iut();
for (rr int i=1;i<=m;++i){
rr int x=iut(),y=iut();
rr db w; scanf("%lf",&w);
e[i]=(node){y,w,as[x]},as[x]=i;
}
for (rr int i=1;i<=n;++i){
for (rr int x=1;x<=n;++x) f[i][x]=1e15;
for (rr int x=1;x<=n;++x)
if (f[i-1][x]!=1e15)
for (rr int j=as[x];j;j=e[j].next)
Min(f[i][e[j].y],f[i-1][x]+e[j].w);
}
for (rr int i=1;i<=n;++i){
rr db now=-1e15;
for (rr int j=0;j<n;++j)
Max(now,(f[n][i]-f[j][i])/(n-j));
Min(ans,now);
}
return !printf("%.8lf",ans);
}
0/1分数规划代码
#include <cstdio>
#include <cctype>
#include <cstring>
#define rr register
using namespace std;
const int N=3011; int as[N],n,m; bool v[N]; double dis[N];
struct node{int y; double w; int next;}e[N*5];
inline signed iut(){
rr int ans=0; rr char c=getchar();
while (!isdigit(c)) c=getchar();
while (isdigit(c)) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return ans;
}
inline bool dfs(int x,double mid){
v[x]=1;
for (rr int i=as[x];i;i=e[i].next)
if (dis[e[i].y]>dis[x]+e[i].w-mid){
if (v[e[i].y]) return 0;
dis[e[i].y]=dis[x]+e[i].w-mid;
if (!dfs(e[i].y,mid)) return 0;
}
v[x]=0;
return 1;
}
signed main(){
n=iut(); m=iut();
for (rr int i=1;i<=m;++i){
rr int x=iut(),y=iut();
rr double w; scanf("%lf",&w);
e[i]=(node){y,w,as[x]},as[x]=i;
}
for (rr int i=1;i<=n;++i)
e[i+m]=(node){i,0,as[n+1]},as[n+1]=m+i;
rr double l=-5000,r=5000;
while (l+1e-9<r){
rr double mid=(l+r)/2;
for (rr int i=1;i<=n+1;++i) dis[i]=v[i]=0;
if (dfs(n+1,mid)) l=mid;
else r=mid;
}
return !printf("%.8lf",l);
}
#结论#洛谷 3199 [HNOI2009]最小圈的更多相关文章
- 洛谷 P3199 [HNOI2009]最小圈
P3199 [HNOI2009]最小圈 题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点 ...
- 洛谷P3199 [HNOI2009]最小圈(01分数规划)
题意 题目链接 Sol 暴力01分数规划可过 标算应该是这个 #include<bits/stdc++.h> #define Pair pair<int, double> #d ...
- 洛谷4951 地震 bzoj1816扑克牌 洛谷3199最小圈 / 01分数规划
洛谷4951 地震 #include<iostream> #include<cstdio> #include<algorithm> #define go(i,a,b ...
- bzoj 1486: [HNOI2009]最小圈 dfs求负环
1486: [HNOI2009]最小圈 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1022 Solved: 487[Submit][Status] ...
- BZOJ 1486: [HNOI2009]最小圈( 二分答案 + dfs判负圈 )
二分答案m, 然后全部边权减掉m, 假如存在负圈, 那么说明有平均值更小的圈存在. 负圈用dfs判断. ------------------------------------------------ ...
- BZOJ_1486_[HNOI2009]最小圈_01分数规划
BZOJ_1486_[HNOI2009]最小圈_01分数规划 Description Input Output Sample Input 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 ...
- [HNOI2009]最小圈 (二分答案+负环)
题面:[HNOI2009]最小圈 题目描述: 考虑带权的有向图\(G=(V,E)\)以及\(w:E\rightarrow R\),每条边\(e=(i,j)(i\neq j,i\in V,j\in V) ...
- bzoj千题计划227:bzoj1486: [HNOI2009]最小圈
http://www.lydsy.com/JudgeOnline/problem.php?id=1486 二分答案 dfs版spfa判负环 #include<queue> #include ...
- 【BZOJ1486】[HNOI2009]最小圈 分数规划
[BZOJ1486][HNOI2009]最小圈 Description Input Output Sample Input 4 5 1 2 5 2 3 5 3 1 5 2 4 3 4 1 3 Samp ...
- BZOJ1486 HNOI2009 最小圈 【01分数规划】
BZOJ1486 HNOI2009 最小圈 Description 应该算是01分数规划的裸板题了吧..但是第一次写还是遇到了一些困难,vis数组不清零之类的 假设一个答案成立,那么一定可以找到一个环 ...
随机推荐
- ProtoBuf 基本使用
一.是什么 Protocol Buffers,是Google公司开发的一种数据描述语言,是一种平台无关.语言无关.可扩展且类似于XML能够将结构化数据序列化,可用于数据存储.通信协议等方面. 二.为什 ...
- 05-Redis系列之-主从复制配置和优化,fork和aof两大阻塞
主从复制 原理 一台主服务器配多台从服务器,主服务器宕机后,从服务器挑选一台顶上去. 从服务器同步主服务器的数据,这个同步是单向的,并且从服务器不能设置值,否则会造成数据的混乱 功能 0.故障处理:s ...
- 【Azure 应用服务】Azure App Service能否使用Storage Account File Share
问题描述 Azure App Service能否使用Storage Account File Share? 问题回答 如果部署的App Service为Linux环境,可以直接使用Mount stor ...
- debian手册摘要
apt-get source 包名 # 获取源码dpkg --info deb包名 # 查看包信息apt-cache show 包名 # 包信息(含有Depends.Suggests.Section. ...
- 机器学习可解释性--shapvalue
A Unified Approach to Interpreting Model Predictions trusting a prediction or trusting a model 如果⼀个机 ...
- 五: Mysql权限管理
# 权限管理 关于MySQL的权限简单的理解就是MySQL允许你做你权力以内的事情,不可以越界.比如只允许你执行SELECT操 作, 那么你就不能执行UPDATE操作.只允许你从某台机器上连接MySQ ...
- 万字博文让我们携手一起走进bs4的世界【python Beautifulsoup】bs4入门 find()与find_all()
目录 Beautiful Soup BeautifulSoup类的基本元素 1.Tag的name 2.Tag的attrs(属性) 3.Tag的NavigableString 二.遍历文档树 下行遍历 ...
- 安卓app设置背景音乐循环播放另有强制不能调节音量软件无法退出(仅供个人学习)
步进式调节:(直接调到那个音量): setStreamVolume (int streamType, int index, int flags) int streamType 需要调整的音量类型 ( ...
- 掌握pandas cut函数,一键实现数据分类
pandas中的cut函数可将一维数据按照给定的区间进行分组,并为每个值分配对应的标签.其主要功能是将连续的数值数据转化为离散的分组数据,方便进行分析和统计. 1. 数据准备 下面的示例中使用的数据采 ...
- AT_abc342_d 题解
UD 2024/2/24 22:36 感谢 Lixiang_is_potato 指出一处笔误. 本文同步发表于洛谷. 赛时挂了,但是赛后 3min AC,我是飞舞. 题意 给你一个长度为 \(N\) ...