hdu3572Task Schedule

m个机器,n个任务,最多500分钟,每个任务只能在特定的时间段内被执行,可以分段之行。

每台机器一分钟就1个工作量,第i个任务共需要p[i]个工作量。

超级源点s和每个任务相连,容量为p,任务与对应的可执行的时间点相连,容量为1.

每个时间点与汇点相连,容量为m。

求maxflow != ∑p;

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2016
File Name   :
*****************************************/
// #pragma comment(linker, "/STACK:1024000000,1024000000")
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <sstream>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <map>
#include <set>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <climits>
using namespace std;
#define MEM(x,y) memset(x, y,sizeof x)
#define pk push_back
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
typedef pair<ii,int> iii;
const double eps = 1e-10;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int maxn = 2000;


struct Edge{
	int from, to, cap, flow;
	Edge(){}
	Edge(int from,int to,int cap,int flow):from(from),to(to),cap(cap),flow(flow){}
};
struct ISAP{
	int p[maxn], num[maxn], cur[maxn], d[maxn];
	int s, t, n, m;
	bool vis[maxn];

	vector<int> G[maxn];
	vector<Edge> edges;

	void init(int n) {
		this->n = n;
		for (int i = 0;i <= n;++i) {
			G[i].clear();
			d[i] = INF;
		}
		edges.clear();
	}

	void addedge(int from,int to,int cap) {
		edges.push_back(Edge(from, to, cap, 0));
		edges.push_back(Edge(to, from, 0, 0));
		m = (int)edges.size();
		G[from].push_back(m - 2);
		G[to].push_back(m - 1);
	}

	bool bfs() {
		memset(vis, false,sizeof vis);

		queue<int> que;
		d[t] = 0;
		vis[t] = true;
		que.push(t);

		while(!que.empty()) {
			int u = que.front();
			que.pop();

			for (int i = 0;i < G[u].size();++i) {
				Edge& e = edges[G[u][i]^1];
				if (e.cap > e.flow && !vis[e.from]) {
					vis[e.from] = true;
					d[e.from] = d[u] + 1;
					que.push(e.from);
				}
			}
		}
		return vis[s];
	}

	int Augment() {
		int u = t, flow = INF;
		while(u != s) {
			Edge& e = edges[p[u]];
			flow = min(flow, e.cap - e.flow);
			u = edges[p[u]].from;
		}

		u = t;
		while(u != s) {
			edges[p[u]].flow += flow;
			edges[p[u]^1].flow -= flow;
			u = edges[p[u]].from;
		}
		return flow;
	}

	int MaxFlow(int s,int t) {
		this->s = s,this->t = t;
		int ret = 0;
		bfs();
		if (d[s] >= n) return 0;

		memset(num, 0,sizeof num);
		memset(cur, 0,sizeof cur);
		for (int i = 0;i < n;++i) {
			if (d[i] < INF) num[d[i]]++;
		}
		int u = s;

		while(d[s] < n) {

			if (u == t) {
				ret += Augment();
				u = s;
			}

			bool ok = false;
			for (int i = cur[u];i < G[u].size();++i) {
				Edge& e = edges[G[u][i]];
				if (e.cap > e.flow && d[u] == d[e.to] + 1) {
					ok = true;
					p[e.to] = G[u][i];
					cur[u] = i;
					u = e.to;
					break;
				}
			}

			if (!ok) {
				int Min = n - 1;
				for (int i = 0;i < G[u].size();++i) {
					Edge& e = edges[G[u][i]];
					if (e.cap > e.flow) Min = min(Min, d[e.to]);
				}
				if (--num[d[u]] == 0) break;
				num[d[u] = Min + 1]++;
				cur[u] = 0;
				if (u != s) u = edges[p[u]].from;
			}
		}
		return ret;
	}
};

ISAP isap;
struct Item{
	int p, s, t;
};
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int n, m, tt;
	int icase = 0;
	scanf("%d",&tt);
	while(tt--) {
		scanf("%d%d",&n,&m);
		int u, v, w;
		int s = 0,t = n + 500 + 1;
		isap.init(t);
		int sum = 0;
		for (int i = 1;i <= n;++i) {
			scanf("%d%d%d",&w,&u,&v);
			sum += w;
			for (int j = u;j <= v;++j)
				isap.addedge(i,j+n,1);
			isap.addedge(s, i, w);
		}
		for (int i = 1;i <= 500;++i)
			isap.addedge(i+n,t, m);
		int ans = isap.MaxFlow(s, t);
		// puts("");
		printf("Case %d: ", ++icase);
		if (ans == sum) puts("Yes");
		else puts("No");
		puts("");
	}
	return 0;
}


你可能感兴趣的:(最大流)