hdu3572(DINIC最大流)

思路:n个任务,m台机器,每个任务有起止时间,和需要的时间,每个机器同一段时间内只能执行一个任务,一个任务在同一时间内只能被同一机器执行,中途可被打断;

每天每台机器是能完成一天的工作量,把这个工作量作为流量比较合理。题目中有三个量,时间,机器,任务。

根据题意,每天我m台机器可以完成m天的任务量,所以图中只需要有任务,时间即可。源点和任务连边,容量p,任务和对应的时间连边,容量1,时间和汇点连边,容量m。

/*****************************************
Author      :Crazy_AC(JamesQi)
Time        :2015
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 <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
#define MEM(a,b) memset(a,b,sizeof a)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> ii;
const int inf = 1 << 30;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
inline int Readint(){
	char c = getchar();
	while(!isdigit(c)) c = getchar();
	int x = 0;
	while(isdigit(c)){
		x = x * 10 + c - '0';
		c = getchar();
	}
	return x;
}
int head[2222],cap[555555],nxt[555555],pnt[555555];
int n,m,cnt;
void add(int u,int v,int c)
{
	pnt[cnt] = v;cap[cnt] = c;nxt[cnt] = head[u];head[u] = cnt++;
	pnt[cnt] = u;cap[cnt] = 0;nxt[cnt] = head[v];head[v] = cnt++;
}
int sum;
int vt;
int d[2000];
int q[2000];
int BFS(int st,int ed)
{
	int rear = 0;
	memset(d, -1,sizeof d);
	d[st] = 0;
	q[rear++] = st;
	for (int i = 0;i < rear;i++){
		for (int j = head[q[i]];j != -1;j = nxt[j]){
			if (cap[j] > 0 && d[pnt[j]] == -1){
				q[rear++] = pnt[j];
				d[pnt[j]] = d[q[i]] + 1;
				if (pnt[j] == ed) return 1;
			}
		}
	}
	return 0;
}
int first[2222];
int dfs(int u,int ed,int c)
{
	// if (c == 0) return c;
	if (u == ed) return c;
	for (int &i = first[u];i != -1;i = nxt[i])
	{
		int v = pnt[i];
		if (cap[i] > 0 && d[u] + 1 == d[v])
		{
			int d = dfs(v,ed,min(c,cap[i]));
			if (d > 0)
			{
				cap[i] -= d;
				cap[i ^ 1] += d;
				return d;
			}
		}
	}
	return 0;
}
int max_flow(int st,int ed)
{
	int flow = 0;
	while(BFS(st,ed))//增广成功
	{
		memcpy(first, head,sizeof head);
		while(true){
			int f = dfs(st,ed,inf);
			if (f > 0)
				flow += f;
			if (f == 0) break;
		}
	}
	return flow;
}
int main()
{	
	// freopen("in.txt","r",stdin);
	// freopen("out.txt","w",stdout);
	int t;
	scanf("%d",&t);
	// t = Readint();
	int icase = 0;
	while(t--)
	{
		scanf("%d%d",&n,&m);
		// n = Readint();
		// m = Readint();
		memset(head, -1,sizeof head);
		cnt = 0;
		int p,st,ed;
		int dmax = 0;
		sum = 0;
		for (int i = 1;i <= n;i++)
		{
			scanf("%d%d%d",&p,&st,&ed);
			// p = Readint();
			// st = Readint();
			// ed = Readint();
			dmax = max(dmax,ed);
			sum += p;
			add(0,i,p);
			for (int j = st;j <= ed;j++){
				add(i,n + j,1);
			}
		}
		vt = n + dmax + 1;//汇点
		for (int i = 1;i <= dmax;i++)
		{
			add(i + n,vt,m);
		}
		printf("Case %d: ",++icase);
		int ans = max_flow(0,vt);
		if (sum == ans) puts("Yes\n");
		else puts("No\n");
		// if (t) puts("");
	}
	return 0;
}


你可能感兴趣的:(图论,网络流)