洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)
题意
Sol
复习一下01分数规划
设\(a_i\)为点权,\(b_i\)为边权,我们要最大化\(\sum \frac{a_i}{b_i}\)。可以二分一个答案\(k\),我们需要检查\(\sum \frac{a_i}{b_i} \geqslant k\)是否合法,移向之后变为\(\sum_{a_i} - k\sum_{b_i} \geqslant 0\)。把\(k * b_i\)加在出发点的点权上检查一下有没有负环就行了
#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 = 4001, 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];
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] = 0;
times[S]++;
while(!q.empty()) {
int p = q.front(); q.pop(); vis[p] = 0;
for(auto &sta : v[p]) {
int to = sta.fi; double w = sta.se;
if(chmax(dis[to], dis[p] + a[p] - k * w)) {
if(!vis[to]) q.push(to), vis[to] = 1, times[to]++;
if(times[to] > N) return 1;
}
}
}
return 0;
}
bool check(double val) {
for(int i = 1; i <= N; i++)
if(SPFA(i, val)) return 1;
return 0;
}
signed main() {
N = read(); M = read();
for(int i = 1; i <= N; i++) a[i] = read();
for(int i = 1; i <= M; i++) {
int x = read(), y = read(), z = read();
v[x].push_back({y, z});
}
double l = -1e9, r = 1e9;
while(r - l > eps) {
double mid = (l + r) / 2;
if(check(mid)) l = mid;
else r = mid;
}
printf("%.2lf", l);
return 0;
}
洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)的更多相关文章
- 洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题目描述 Farmer John has decided to reward his cows for their har ...
- 洛谷P2868 [USACO07DEC]观光奶牛 Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- 洛谷 P2868 [USACO07DEC]观光奶牛Sightseeing Cows 题解
题面 这道题是一道标准的01分数规划: 但是有一些细节可以优化: 不难想到要二分一个mid然后判定图上是否存在一个环S,该环是否满足∑i=1t(Fun[vi]−mid∗Tim[ei])>0 但是 ...
- POJ3621或洛谷2868 [USACO07DEC]观光奶牛Sightseeing Cows
一道\(0/1\)分数规划+负环 POJ原题链接 洛谷原题链接 显然是\(0/1\)分数规划问题. 二分答案,设二分值为\(mid\). 然后对二分进行判断,我们建立新图,没有点权,设当前有向边为\( ...
- 洛谷 2868 [USACO07DEC]观光奶牛Sightseeing Cows
题目戳这里 一句话题意 L个点,P条有向边,求图中最大比率环(权值(Fun)与长度(Tim)的比率最大的环). Solution 巨说这是0/1分数规划. 话说 0/1分数规划 是真的难,但貌似有一些 ...
- P2868 [USACO07DEC]观光奶牛Sightseeing Cows
P2868 [USACO07DEC]观光奶牛Sightseeing Cows [](https://www.cnblogs.com/images/cnblogs_com/Tony-Double-Sky ...
- [USACO07DEC]观光奶牛Sightseeing Cows 二分答案+判断负环
题目描述 Farmer John has decided to reward his cows for their hard work by taking them on a tour of the ...
- Luogu 2868 [USACO07DEC]观光奶牛Sightseeing Cows
01分数规划复习. 这东西有一个名字叫做最优比率环. 首先这个答案具有单调性,我们考虑如何检验. 设$\frac{\sum_{i = 1}^{n}F_i}{\sum_{i = 1}^{n}T_i} = ...
随机推荐
- 微信小程序的wx-charts插件-tab选项卡
微信小程序的wx-charts插件-tab选项卡 效果: //index.js var wxCharts = require('../../utils/wxcharts-min.js'); const ...
- 程序员周末阿里面试,5分钟就被一道题秒杀:HashMap与Hashtable
你们可能会想,我这么菜的吗?5分钟都坚持不了? 本文说起来会有点尴尬,毕竟这是我曾经经历过的故事 那时候的我还真菜,每天写着 if/ for 及一些简单的业务逻辑代码,虽工作有些日子了,但技术水平还停 ...
- Linux编程 19 编辑器(vim 用法)
一.概述 在开启shell脚本编程之前,必须要知道一款文本编辑器的用法,如文本编辑的查找,剪切,粘贴,定位等, 本篇只讲vim编辑器.vim编辑器全名叫vi improved,是经过对Unix系统vi ...
- sql server 锁与事务拨云见日(上)
一.概述 讲到sql server锁管理时,感觉它是一个大话题,因为它不但重要而且涉及的知识点很多,重点在于要掌握高并发要先要掌握锁与事务,涉及的知识点多它包括各式各样的锁,锁的组合,锁的排斥,锁延伸 ...
- 【原】使用vue2+vue-router+vuex写一个cnode的脚手架
最近喜欢上了markdown的书写方式,所以博客直接写在github上来,点击查看
- 全网最详细的Centos7系统里安装Openresty(图文详解)
不多说,直接上干货! 介绍: Nginx 采用一个 master 进程管理多个 worker 进程(master-worker)模式,基本的事件处理都在 woker 中,master 负责一些全局初始 ...
- 【Python开发】Python中数据分析环境的搭建
注:无论是任何一门语言,刚开始入门的时候,语言运行环境的搭建都是一件不轻松的事情. Python的运行环境 要运行或写Python代码,就需要Python的运行环境,主要的Python有以下三类: 原 ...
- leetcode — unique-paths-ii
/** * Source : https://oj.leetcode.com/problems/unique-paths-ii/ * * * Follow up for "Unique Pa ...
- 彻底弄懂 Unicode 编码
彻底弄懂 Unicode 编码 今天,在学习 Node.js 中的 Buffer 对象时,注意到它的 alloc 和 from 方法会默认用 UTF-8 编码,在数组中每位对应 1 字节的十六进制数. ...
- C# 锁系列目录
1.lock.Monitor lock(obj){} 编译之后是如下代码 Monitor.Enter(obj); try { // } finally { Monitor.Exit(obj); } 2 ...