bzoj1391 [Ceoi2008]order

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1391

【题解】

简单最小割建模。

当前弧优化!!!

# include <queue>
# include <stdio.h>
# include <string.h>
# include <iostream>
# include <algorithm>
// # include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int M = 3e6 + 10, N = 2510;
const int mod = 1e9+7;

# define RG register
# define ST static

int n, m, S, T;

int cur[N], head[N], nxt[M], to[M], tot=1, flow[M];
inline void add(int u, int v, int fl) {
    ++tot; nxt[tot] = head[u]; head[u] = tot; to[tot] = v; flow[tot] = fl;
}
inline void adde(int u, int v, int fl) {
    add(u, v, fl), add(v, u, 0);
}

namespace MF {
    queue<int> q; int c[N];
    inline bool bfs() {
        while(!q.empty()) q.pop();
        for (int i=1; i<=T; ++i) c[i] = -1;
        c[S] = 1; q.push(S);
        while(!q.empty()) {
            int top = q.front(); q.pop();
            for (int i=head[top]; i; i=nxt[i]) {
                if(flow[i] == 0 || c[to[i]] != -1) continue;
                c[to[i]] = c[top] + 1;
                q.push(to[i]);
                if(to[i] == T) return 1;
            }
        }
        return 0;
    }
    inline int dfs(int x, int low) {
        if(x == T) return low;
        int r = low, fl;
        for (int i=cur[x]; i; i=nxt[i]) {
            if(flow[i] == 0 || c[to[i]] != c[x] + 1) continue;
            fl = dfs(to[i], min(r, flow[i]));
            flow[i] -= fl;
            if(flow[i] > 0) cur[x] = i; 
            flow[i^1] += fl;
            r -= fl;
            if(!r) return low;
        }
        if(low==r) c[x]=-1;
        return low-r;
    }
    inline int main() {
        int ret = 0;
        while(bfs()) {
            for (int i=1; i<=n+m+2; ++i)
                cur[i] = head[i];
            ret += dfs(S, 1e9);
        }
        return ret;
    }
}

int main() {
    int ans = 0;
    cin >> n >> m;
    S = n+m+1; T = n+m+2;
    for (int i=1, P, pn, t; i<=n; ++i) {
        scanf("%d%d", &P, &pn); ans += P;
        adde(S, i, P); 
        while(pn--) {
            scanf("%d%d", &t, &P);
            adde(i, t+n, P);
        }
    }
    for (int i=1, P; i<=m; ++i) {
        scanf("%d", &P);
        adde(i+n, T, P);
    }
    
    cout << ans - MF::main(); 
    
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/galaxies/p/bzoj1391.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值