天天看點

【BZOJ5367】【LOJ2308】【APIO2017】商旅(SPFA判負環,01分數規劃,Floyd)

Description

PDF

Solution

我們對于每一個點對 (u,v) ( u , v ) ,求出從 u u 走到vv的最短路 dist(u,v) d i s t ( u , v ) 和在 u u 買進、在vv賣出的最大收益 w(u,v) w ( u , v ) 。

考慮二分答案 x x ,我們要判斷是否∑w∑dist≥x∑w∑dist≥x,即 ∑w−x×dist≥0 ∑ w − x × d i s t ≥ 0 ,我們将邊權變成 w−x×dist w − x × d i s t ,然後判斷是否有正環即可。

Code

/************************************************
 * Au: Hany01
 * Date: Aug 22nd, 2018
 * Prob: LOJ2308 BZOJ5367 APIO2017 商旅
 * Email: [email protected] & [email protected]
 * Inst: Yali High School
************************************************/

#include<bits/stdc++.h>

using namespace std;

typedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
#define rep(i, j) for (register int i = 0, i##_end_ = (j); i < i##_end_; ++ i)
#define For(i, j, k) for (register int i = (j), i##_end_ = (k); i <= i##_end_; ++ i)
#define Fordown(i, j, k) for (register int i = (j), i##_end_ = (k); i >= i##_end_; -- i)
#define Set(a, b) memset(a, b, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define x first
#define y second
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define SZ(a) ((int)(a).size())
#define INF (0x3f3f3f3f)
#define INF1 (2139062143)
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define y1 wozenmezhemecaia

template <typename T> inline bool chkmax(T &a, T b) { return a < b ? a = b,  : ; }
template <typename T> inline bool chkmin(T &a, T b) { return b <= a ? a = b,  : ; }

inline int read() {
    static int _, __; static char c_;
    for (_ = , __ = , c_ = getchar(); c_ < '0' || c_ > '9'; c_ = getchar()) if (c_ == '-') __ = -;
    for ( ; c_ >= '0' && c_ <= '9'; c_ = getchar()) _ = (_ << ) + (_ << ) + (c_ ^ );
    return _ * __;
}

const int maxn = , maxk = ;

int n, m, k, in[maxn][maxk], out[maxn][maxk], dis[maxn][maxn], ben[maxn][maxn];

inline bool SPFA(int x) {
    static queue<int> q;
    static LL dist[maxn];
    static int vis[maxn], cnt[maxn];

    while (!q.empty()) q.pop();
    For(i, , n) q.push(i);
    Set(dist, ), dist[] = , Set(cnt, ), Set(vis, );

    while (!q.empty()) {
        register int u = q.front(); q.pop(), vis[u] = ;
        For(v, , n)
            if (u != v && dis[u][v] != INF && chkmin(dist[v], dist[u] - ben[u][v] + (LL)x * dis[u][v]))
                if (!vis[v]) {
                    vis[v] = , q.push(v);
                    if ((cnt[v] = cnt[u] + ) >= n) return ;
                }
    }
    return ;
}

int main()
{
#ifdef hany01
    freopen("bzoj5367.in", "r", stdin);
    freopen("bzoj5367.out", "w", stdout);
#endif

    static int uu, vv, l, r, mid; Set(dis, );

    n = read(), m = read(), k = read();
    For(i, , n) For(j, , k) in[i][j] = read(), chkmax(r, out[i][j] = read());
    For(i, , m) uu = read(), vv = read(), dis[uu][vv] = read();

    For(k, , n) For(i, , n) For(j, , n)
        chkmin(dis[i][j], dis[i][k] + dis[k][j]);
    For(l, , n) For(r, , n) For(i, , k)
        if (in[l][i] != - && out[r][i] != -)
            chkmax(ben[l][r], out[r][i] - in[l][i]);

    while (l < r) {
        mid = (l + r + ) >> ;
        if (SPFA(mid)) l = mid; else r = mid - ;
    }
    printf("%d\n", l);

    return ;
}
           

繼續閱讀