洛谷P3199 [HNOI2009]最小圈(01分数规划)
题意
Sol
暴力01分数规划可过
标算应该是这个
#include<bits/stdc++.h>
#define Pair pair<int, double>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long
#define LL long long
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 3001, mod = 998244353, INF = 2e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M;
vector<Pair> v[MAXN];
double a[MAXN], dis[MAXN];
int vis[MAXN], times[MAXN], can[MAXN];
bool SPFA(int S, double k) {
queue<int> q; q.push(S);
for(int i = 1; i <= N; i++) vis[i] = 0, times[i] = 0, dis[i] = INF;
dis[S] = 0;
times[S]++;
while(!q.empty()) {
int p = q.front(); q.pop(); vis[p] = 0;
can[p] = 1;
for(auto &sta : v[p]) {
int to = sta.fi; double w = sta.se;
if(chmin(dis[to], dis[p] + w - k)) {
if(!vis[to]) q.push(to), vis[to] = 1, times[to]++;
if(times[to] > 50) return 1;
}
}
}
return 0;
}
bool check(double val) {
memset(can, 0, sizeof(can));
for(int i = 1; i <= N; i++)
if(!can[i] && SPFA(i, val)) return 1;
return 0;
}
signed main() {
//Fin(a);
N = read(); M = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read(); double z; scanf("%lf", &z);
v[x].push_back({y, z});
}
double l = -1e7 - 10, r = 1e7 + 10;
while(r - l > eps) {
double mid = (l + r) / 2;
if(check(mid)) r = mid;
else l = mid;
}
printf("%.8lf", l);
return 0;
}
/*
10 3
aaaabbbbab
7 7 3 9 10 6 7 6 6 1
2 6 2
1 3 1
2 9 1
*/
洛谷P3199 [HNOI2009]最小圈(01分数规划)的更多相关文章
- P3199 [HNOI2009]最小圈 01分数规划
裸题,第二个权值是自己点的个数.二分之后用spfa判负环就行了. 题目描述 考虑带权的有向图G=(V,E)G=(V,E)G=(V,E)以及w:E→Rw:E\rightarrow Rw:E→R,每条边e ...
- 洛谷 P3199 [HNOI2009]最小圈
P3199 [HNOI2009]最小圈 题目背景 如果你能提供题面或者题意简述,请直接在讨论区发帖,感谢你的贡献. 题目描述 对于一张有向图,要你求图中最小圈的平均值最小是多少,即若一个圈经过k个节点 ...
- BZOJ 1486: [HNOI2009]最小圈 [01分数规划]
裸题...平均权值最小的环.... 注意$dfs-spfa$时$dfs(cl)$...不要写成$dfs(u)$ #include <iostream> #include <cstdi ...
- 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 ...
- BZOJ 1486 最小圈(01分数规划)
好像是很normal的01分数规划题.最小比率生成环. u(c)=sigma(E)/k.转化一下就是k*u(c)=sigma(E). sigma(E-u(c))=0. 所以答案对于这个式子是有单调性的 ...
- 洛谷4951 地震 bzoj1816扑克牌 洛谷3199最小圈 / 01分数规划
洛谷4951 地震 #include<iostream> #include<cstdio> #include<algorithm> #define go(i,a,b ...
- 【洛谷 P3199】 [HNOI2009]最小圈(分数规划,Spfa)
题目链接 一开始不理解为什么不能直接用\(Tarjan\)跑出换直接求出最小值,然后想到了"简单环",恍然大悟. 二分答案,把所有边都减去\(mid\),判是否存在负环,存在就\( ...
- [HNOI2009]最小圈(分数规划+SPFA判负环)
题解:求环长比环边个数的最小值,即求min{Σw[i]/|S|},其中i∈S.这题一眼二分,然后可以把边的个数进行转化,假设存在Σw[i]/|S|<=k,则Σw[i]-k|S|<=0,即Σ ...
- 【BZOJ1486】最小圈(分数规划)
[BZOJ1486]最小圈(分数规划) 题面 BZOJ 洛谷 求图中边权和除以点数最小的环 题解 分数规划 二分答案之后将边权修改为边权减去二分值 检查有无负环即可 #include<iostr ...
随机推荐
- SpringMVC 使用 MultipartFile 实现文件上传
该代码实现了文件上传和文本字段同时传递到后台进行处理的功能. 直接贴代码,中间涉及到的实体类就不贴了,和功能没啥关系的. Controller /** * 添加活动 * * @param req * ...
- git小技巧
1 git提交时如何忽略一些文件: 在git根目录下添加,然后提交,就可以使用了,详细的语法详见 https://github.com/github/gitignore github提供了一个通用的. ...
- socket 实现单一串口共享读写操作
前提:物理串口连接到PC上,通过串口号被PC唯一识别. 此时,物理串口通过该串口号仅能被单一线程或进程实例并占用,其他线程或进程不能再通过该串口号与物理串口通信.这个暂称为串口独占性. 解决思路:核心 ...
- [2019BUAA软工助教]第0次代码作业
[2019BUAA软工助教]第0次代码作业 前言 本博客为完成结对项目所需的先导知识,题目不难,请认真对待 :) 欢迎来到软件工程 :) 注:本次实验为附加作业,不做不扣分,做了有附加分 Part 0 ...
- rest-framework之认证组件
认证组件 认证简介 作用:校验是否登录 首先定义一个类,集成BaseAuthentication,写一个方法:authenticate,在方法内部,实证过程,认证通过,返回None或者两个对象(use ...
- Plugin with id 'com.novoda.bintray-release' not found.的解决方案
import Module的时候,有时候会提示Plugin with id 'com.novoda.bintray-release' not found. 点击Open File,定位到该Module ...
- 小程序this.setData
data: { isChecked: [ { key: true },{ key: true },{ key: true} ]} 如上,如果我想动态修改isChecked里面指定某个下标的值怎么办? ...
- 痞子衡嵌入式:让你从此高效写作的轻量级标记语言(Markdown)
大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是轻量级标记语言Markdown. 1.假如你有过这样的烦恼? 想写出排版优雅简洁的文章,并且能够轻易地发表(连同文字和排版)到各大网站上 ...
- python集合类型
集合类型简介 集合也是容器,其内元素都是无序.唯一.不可变的.它常用来做成员测试.移除重复数据.数据计算(比如交集.并集.差集). 集合Set是dict的无value版.集合也使用大括号包围: > ...
- Python中的序列操作
官方手册:https://docs.python.org/3.7/library/stdtypes.html#sequence-types-list-tuple-range 序列简介 序列是指按照位置 ...