• 【BZOJ 1061】【Vijos 1825】【NOI 2008】志愿者招募


    http://www.lydsy.com/JudgeOnline/problem.php?id=1061
    https://vijos.org/p/1825
    直接上姜爷论文。。。



    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    using namespace std;
    typedef long long ll;
    const int N = 1013;
    const int M = 40003;
    const int inf = 0x7fffffff;
    
    struct node {int nxt, to, c, w, from;} E[M];
    int cnt = 1, point[N << 1];
    
    void ins(int u, int v, int c, int w) {
    	E[++cnt] = (node) {point[u], v, c, w, u}; point[u] = cnt;
    	E[++cnt] = (node) {point[v], u, 0, -w, v}; point[v] = cnt;
    }
    
    bool inq[N];
    int dist[N], pre[N], q[N];
    
    bool spfa(int s, int t) {
    	for (int i = s; i <= t; ++i) dist[i] = inf, pre[i] = 0;
    	int u, v, tt, head = 0, tail = 1; q[1] = s; inq[s] = true; dist[s] = 0;
    	while (head != tail) {
    		++head; if (head == N) head = 0;
    		u = q[head]; inq[u] = false;
    		for (int i = point[u]; i; i = E[i].nxt)
    			if (E[i].c && dist[v = E[i].to] > (tt = dist[u] + E[i].w)) {
    				dist[v] = tt; pre[v] = i;
    				if (!inq[v]) {
    					inq[v] = true;
    					++tail; if (tail == N) tail = 0;
    					q[tail] = v;
    				}
    			}
    	}
    	return dist[t] != inf;
    }
    
    ll MCMF(int s, int t) {
    	ll ret = 0;
    	while (spfa(s, t)) {
    		int f = inf;
    		for (int u = t; u != s; u = E[pre[u]].from) f = min(f, E[pre[u]].c);
    		for (int u = t; u != s; u = E[pre[u]].from) E[pre[u]].c -= f, E[pre[u] ^ 1].c += f;
    		ret += f * dist[t];
    	}
    	return ret;
    }
    
    int n, m, s, t, c, S, T, a[N];
    
    int main() {
    	scanf("%d%d", &n, &m);
    	S = 0; T = n + 2;
    	for (int i = 1; i <= n; ++i) scanf("%d", a + i);
    	for (int i = 0; i <= n; ++i) {
    		int num = a[i + 1] - a[i];
    		if (num < 0) ins(i + 1, T, -num, 0);
    		if (num > 0) ins(S, i + 1, num, 0);
    	}
    	for (int i = 1; i <= n; ++i) ins(i + 1, i, inf, 0);
    	for (int i = 1; i <= m; ++i) {
    		scanf("%d%d%d", &s, &t, &c);
    		ins(s, t + 1, inf, c);
    	}
    	
    	printf("%lld
    ", MCMF(S, T));
    	return 0;
    }
    
  • 相关阅读:
    Celery框架
    Tensorflow安装记录
    OpenFace的一些了解
    CentOS+Uwsgi+Nginx发布Flask开发的WebAPI
    Always On 集群监听创建失败问题
    SQL Server 安装好后 Always On群组配置
    Sql server 2016 Always On 搭建Windows集群配置
    汕头市队赛 SRM13 T3
    bzoj 1314: River过河 树套树+单调队列
    hdu 6119 …&&百度之星 T6
  • 原文地址:https://www.cnblogs.com/abclzr/p/6219672.html
Copyright © 2020-2023  润新知