洛谷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} = ...
随机推荐
- MySQL如何使用索引
初始化测试数据 创建一个测试用的表 create table dept(id int primary key auto_increment , deptName varchar(32) not nul ...
- aaa配置(第十三组)
拓扑 网络情况 A PING B A PING C PC-B PING PC-C 2.R1的配置 a.console线 R1(config)#username admin1 password Admi ...
- 【Spark调优】内存模型与参数调优
[Spark内存模型] Spark在一个executor中的内存分为3块:storage内存.execution内存.other内存. 1. storage内存:存储broadcast,cache,p ...
- 【翻译】JavaScript中5个值得被广泛使用的数组方法
原文地址:http://colintoh.com/blog/5-array-methods-that-you-should-use-today?utm_source=javascriptweekly& ...
- Go语言下的线程模型
阅读Go并发编程对go语言线程模型的笔记,解释的非常到,好记性不如烂笔头,忘记的时候回来翻一番,在此做下笔记. Go语言的线程实现模型,又3个必知的核心元素,他们支撑起了这个线程实现模型的主要框架: ...
- Azure认知服务之Face API上手体验
Azure认知服务:Face API Face API是Azure认知服务之一,Face API有两个主要功能: 人脸检测 Face API可在图像中以高精度人脸位置检测多达64个人脸.图像可以通过文 ...
- sql server 日志文件结构及误操作数据找回
一. 概述 在sql server 里有数据文件.mdf和日志文件.ldf,日志文件是sqlserver数据库的另一个重要组成部分,日志文件记录了所有事务以及每个事务对数据库所做的修改.为了提高数据库 ...
- idea启动多个tomcat失败
Intellij idea中,为在本地调试两个系统之间的调用,配置两个本地tomcat server,设置不同的端口号,如8081和8082,Deploy中加入两个系统各自的Artifact xxx: ...
- Hadoop学习笔记(一):安装与配置
1. 查看VM的网络配置 2. 打开虚拟机,配置网络: a). vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 注意:这里的192.168.9 ...
- Web Service 部分内容简述(2)
1. CORBA是什么?用途是什么? CORBA 标准是公共对象请求代理结构(Common Object Request Broker Architecture),由对象管理组织 (Object M ...