zoukankan      html  css  js  c++  java
  • POJ 3762 The Bonus Salary!(最小K覆盖)

    POJ 3762 The Bonus Salary!

    题目链接

    题意:给定一些任务。每一个任务有一个时间,有k天。一个时间仅仅能运行一个任务,每一个任务有一个价值。问怎么安排能得到最多价值

    思路:典型的区间k覆盖问题

    代码:

    #include <cstdio>
    #include <cstring>
    #include <vector>
    #include <queue>
    #include <algorithm>
    #include <map>
    using namespace std;
    
    const int MAXNODE = 4505;
    const int MAXEDGE = 100005;
    typedef int Type;
    const Type INF = 0x3f3f3f3f;
    
    struct Edge {
    	int u, v;
    	Type cap, flow, cost;
    	Edge() {}
    	Edge(int u, int v, Type cap, Type flow, Type cost) {
    		this->u = u;
    		this->v = v;
    		this->cap = cap;
    		this->flow = flow;
    		this->cost = cost;
    	}
    };
    
    struct MCFC {
    	int n, m, s, t;
    	Edge edges[MAXEDGE];
    	int first[MAXNODE];
    	int next[MAXEDGE];
    	int inq[MAXNODE];
    	Type d[MAXNODE];
    	int p[MAXNODE];
    	Type a[MAXNODE];
    
    	void init(int n) {
    		this->n = n;
    		memset(first, -1, sizeof(first));
    		m = 0;
    	}
    
    	void add_Edge(int u, int v, Type cap, Type cost) {
    		edges[m] = Edge(u, v, cap, 0, cost);
    		next[m] = first[u];
    		first[u] = m++;
    		edges[m] = Edge(v, u, 0, 0, -cost);
    		next[m] = first[v];
    		first[v] = m++;
    	}
    
    	bool bellmanford(int s, int t, Type &flow, Type &cost) {
    
    		for (int i = 0; i < n; i++) d[i] = INF;
    		memset(inq, false, sizeof(inq));
    		d[s] = 0; inq[s] = true; p[s] = s; a[s] = INF;
    		queue<int> Q;
    		Q.push(s);
    		while (!Q.empty()) {
    			int u = Q.front(); Q.pop();
    			inq[u] = false;
    			for (int i = first[u]; i != -1; i = next[i]) {
    				Edge& e = edges[i];
    				if (e.cap > e.flow && d[e.v] > d[u] + e.cost) {
    					d[e.v] = d[u] + e.cost;
    					p[e.v] = i;
    					a[e.v] = min(a[u], e.cap - e.flow);
    					if (!inq[e.v]) {Q.push(e.v); inq[e.v] = true;}
    				}
    			}
    		}
    		if (d[t] == INF) return false;
    		flow += a[t];
    		cost += d[t] * a[t];
    		int u = t;
    		while (u != s) {
    			edges[p[u]].flow += a[t];
    			edges[p[u]^1].flow -= a[t];
    			u = edges[p[u]].u;
    		}
    		return true;
    	}
    
    	Type Mincost(int s, int t) {
    		Type flow = 0, cost = 0;
    		while (bellmanford(s, t, flow, cost));
    		return cost;
    	}
    } gao;
    
    const int N = 2005;
    
    map<int, int> hash;
    int n, k, hn;
    
    int get(int x) {
    	if (!hash.count(x)) hash[x] = hn++;
    	return hash[x];
    }
    
    struct Task {
    	int l, r, w;
    } task[N];
    
    int main() {
    	while (~scanf("%d%d", &n, &k)) {
    		hash.clear();
    		hn = 1;
    		int h1, m1, s1, h2, m2, s2, w;
    		for (int i = 0; i < n; i++) {
    			scanf("%d:%d:%d %d:%d:%d %d", &h1, &m1, &s1, &h2, &m2, &s2, &w);
    			int t1 = h1 * 3600 + m1 * 60 + s1;
    			int t2 = h2 * 3600 + m2 * 60 + s2;
    			task[i].l = get(t1); task[i].r = get(t2); task[i].w = w;
    		}
    		gao.init(hn + 1);
    		for (int i = 0; i < n; i++)
    			gao.add_Edge(task[i].l, task[i].r, 1, -task[i].w);
    		int pre = 0;
    		for (map<int, int>::iterator it = hash.begin(); it != hash.end(); it++) {
    			gao.add_Edge(pre, it->second, k, 0);
    			pre = it->second;
    		}
    		gao.add_Edge(pre, hn, k, 0);
    		printf("%d
    ", -gao.Mincost(0, hn));
    	}
    	return 0;
    }


  • 相关阅读:
    linux php5.6 安装Redis扩展
    linux php7.2安装扩展memcached
    极简的switch控件
    整理了最近百年的藏历数据,做了个公历藏历映射的小工具
    我是怎么让全国最大的儿童失踪预警平台流量掉底的
    jq的getScript函数不支持chaset?override掉!
    大家好像都比较少关心webcrypto,试试写个简单的sha1/sha256/sha384/sha512实现看看
    惊喜:opera换webkit内核后完美支持SDCH压缩协议
    TCPIP协议实践:wireshark抓包分析之链路层与网络层
    使用unity3d和tensorflow实现基于姿态估计的体感游戏
  • 原文地址:https://www.cnblogs.com/mengfanrong/p/5355869.html
Copyright © 2011-2022 走看看