[HNOI 2009]最小圈
Description
Input
Output
Sample Input
Sample Output
HINT
对于100%的数据,$n\le 3000,m\le 10000,|w_{i,j}| \le 10^7$
题解
最小化平均值($01$分数规划)。
使用二分求解。对于一个猜测的$mid$,只需判断是否存在平均值小于$mid$的回路。
如何判断?
假设存在一个包含$k$条边的回路,回路上各边权值为$w_1$ ,$w_2$ ,$...$,$w_k$ ,那么平均值小于$midv$意味着:
$$w_1 +w_2 +...+w_k <k×mid$$
即:
$$(w_1 -mid)+(w_2 -mid)+...+(w_k -mid)<0$$
换句话说,只要把边$(a,b)$的权$w(a,b)$改成$w(a,b)-mid$,再判断新图中是否有负环即可。
存在负环,那么之前的不等式满足,即存在着更小的平均值,$r=mid$;不存在,$l=mid$。
//It is made by Awson on 2017.10.9
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define sqr(x) ((x)*(x))
using namespace std;
const double eps = 1e-;
const int N = ;
const int M = ; int n, m, u, v, c;
bool vis[N+];
double dist[N+];
struct tt {
int to, next;
double cost;
}edge[M+];
int path[N+], top; void add(int u, int v, double c) {
edge[++top].to = v;
edge[top].next = path[u];
edge[top].cost = c;
path[u] = top;
}
bool dfs(int u, double dec) {
vis[u] = ;
for (int i = path[u]; i; i = edge[i].next)
if (dist[edge[i].to] > dist[u]+edge[i].cost-dec) {
if (vis[edge[i].to]) {vis[u] = ; return true;}
dist[edge[i].to] = dist[u]+(double)edge[i].cost-dec;
if (dfs(edge[i].to, dec)) {vis[u] = ; return true;}
}
vis[u] = ;
return false;
}
bool judge(double dec) {
memset(dist, , sizeof(dist));
for (int i = ; i <= n; i++)
if (dfs(i, dec)) return true;
return false;
}
void work() {
scanf("%d%d", &n, &m);
double L = , R = ;
for (int i = ; i <= m; i++) {
scanf("%d%d%d", &u, &v, &c);
add(u, v, c);
R = max(R, (double)c);
}
while (R-L >= eps) {
double mid = (L+R)/.;
if (judge(mid)) R = mid;
else L =mid;
}
printf("%.8lf\n", (L+R)/.);
}
int main() {
work();
return ;
}
[HNOI 2009]最小圈的更多相关文章
- 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判断. ------------------------------------------------ ...
- [HNOI2009]最小圈
题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点,那么一个圈的平均值为圈上k条边权的和除以k,现要求其中的最小值 输入输出格式 输入格式: 第一行2个正整数,分别为 ...
- 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) ...
- 【题解】 [HNOI2009] 最小圈 (01分数规划,二分答案,负环)
题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点,那么一个圈的平均值为圈上k条边权的和除 ...
- bzoj千题计划227:bzoj1486: [HNOI2009]最小圈
http://www.lydsy.com/JudgeOnline/problem.php?id=1486 二分答案 dfs版spfa判负环 #include<queue> #include ...
- 洛谷4951 地震 bzoj1816扑克牌 洛谷3199最小圈 / 01分数规划
洛谷4951 地震 #include<iostream> #include<cstdio> #include<algorithm> #define go(i,a,b ...
- 【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 ...
随机推荐
- winform 适配high dpi
在 mainifest文件中添加:(新建mainifest文件的时候以下内容是有的,只要取消注释就可以了) <compatibility xmlns="urn:schemas-micr ...
- c语言——第0次作业
1.你认为大学的学习生活.同学关系.师生应该是怎样?请一个个展开描写 大学生活:大学生活充满着挑战,首先当然必须先掌握自己所学的专业知识,然后就要学会独立,可以处理好人际关系,并且要有更强的自我约束能 ...
- 洛谷P2894 [USACO08FEB]酒店Hotel
P2894 [USACO08FEB]酒店Hotel https://www.luogu.org/problem/show?pid=2894 题目描述 The cows are journeying n ...
- 老板怎么办,我们网站遭到DDoS攻击又挂了?
相信现在正在阅读此文的你,一定听说过发生在上个月的史上最大的DDoS攻击. 美国东部时间2月28日,GitHub在一瞬间遭到高达1.35Tbps的带宽攻击.这次DDoS攻击几乎可以堪称是互联网有史以来 ...
- Python之旅.第三章.函数3.30
一.迭代器 1.什么是迭代?:迭代是一个重复的过程,并且每次重复都是基于上一次的结果而来2.要想了解迭代器到底是什么?必须先了解一个概念,即什么是可迭代的对象?可迭代的对象:在python中,但凡内置 ...
- Python之旅.第三章.函数3.29
一.无参装饰器 1 开放封闭原则 软件一旦上线后,就应该遵循开放封闭原则,即对修改源代码是封闭的,对功能的扩展是开放的 也就是说我们必须找到一种解决方案: 能够在不修改一个功能源代码以及调用方式的前提 ...
- C#之Socket通信
0.虽然之前在项目中也有用过Socket,但始终不是自己搭建的,所以对Server,Clinet端以及心跳,断线重连总没有很深入的理解,现在自己搭建了一遍加深一下理解. 服务端使用WPF界面,客户端使 ...
- Python内置函数(21)——tuple
英文文档: The constructor builds a tuple whose items are the same and in the same order as iterable's it ...
- Python内置函数(16)——ord
英文文档: ord(c) Given a string representing one Unicode character, return an integer representing the U ...
- python 模拟浏览器登陆coursera
import requests import random import string def randomString(length): return ''.join(random.choice(s ...