首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

洛谷P2868 [USACO07DEC]观光奶牛Sightseeing Cows(01分数规划)

作者头像
attack
发布2019-03-15 14:23:10
3260
发布2019-03-15 14:23:10
举报

题意

题目链接

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;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 题意
  • Sol
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档