Task Schedule-最大流判断满流

Problem Description

Our geometry princess XMM has stoped her study in computational geometry to concentrate on her newly opened factory. Her factory has introduced M new machines in order to process the coming N tasks. For the i-th task, the factory has to start processing it at or after day Si, process it for Pi days, and finish the task before or at day Ei. A machine can only work on one task at a time, and each task can be processed by at most one machine at a time. However, a task can be interrupted and processed on different machines on different days.
Now she wonders whether he has a feasible schedule to finish all the tasks in time. She turns to you for help.

 

 

Input

On the first line comes an integer T(T<=20), indicating the number of test cases.

You are given two integer N(N<=500) and M(M<=200) on the first line of each test case. Then on each of next N lines are three integers Pi, Si and Ei (1<=Pi, Si, Ei<=500), which have the meaning described in the description. It is guaranteed that in a feasible schedule every task that can be finished will be done before or at its end day.

 

 

Output

For each test case, print “Case x: ” first, where x is the case number. If there exists a feasible schedule to finish all the tasks, print “Yes”, otherwise print “No”.

Print a blank line after each test case.

 

 

Sample Input

 

2 4 3 1 3 5 1 1 4 2 3 7 3 5 9 2 2 2 1 3 1 2 2

 

 

Sample Output

 

Case 1: Yes Case 2: Yes

 

 

Author

allenlowesy

 

 

Source

2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC

 

 

Recommend

zhouzeyong   |   We have carefully selected several similar problems for you:  1532 3491 1533 3416 3549 

题意就是:给你n个任务,每个任务有开始时间和做这个活要多长时间还有最迟的完成时间还有你有几台机器问你是否能完成所有任务每个任务可以一会用这台机器写一会用另外一个

思路:

我们可以先设一个源点和汇点和500个点用来代表时间和n个点代表工作,然后我用源点连接所有的任务点每点的权值为这一任务点到源点的权值,然后让每一个任务点连接这一个任务的时间,权值设为1,代表这个时间段成功操作机器,然后把时间和汇点连接权值为机器的数量因为超过就没有机器做这个活了,求出的最大流就是成功的时间让时间总数看看是否和他一样就yes不一样no,不可能大于因为汇点 >= 源点 的量

代码

#include 
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 2005;
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 Dinic{
	int n, m, s, t;
	vectoredges;
	vectorG[maxn];
	int d[maxn];
	int cur[maxn];
	int vis[maxn];
	void init(int n, int s, int t)
	{
		this->n = n;this->s = s;this->t = t;
		edges.clear();
		for(int i = 0;i <= n;++i) G[i].clear();
	}
	
	void add_edge(int from, int to, int cap)
	{
		edges.push_back( Edge(from, to, cap, 0) );
		edges.push_back( Edge(to, from, 0, 0) );
		m = edges.size();
		G[from].push_back(m-2);
		G[to].push_back(m-1); 
	}
	
	bool bfs(){
		memset(vis, 0, sizeof(vis));
		queueQ;
		Q.push(s);
		d[s] = 0;
		vis[s] = true;
		while(!Q.empty())
		{
			int x = Q.front();
			Q.pop();
			for(int i = 0;i < G[x].size();++i)
			{
				Edge& e = edges[G[x][i]];
				if(!vis[e.to] && e.cap > e.flow)
				{
					vis[e.to] = true;
					d[e.to] = d[x] + 1;
					Q.push(e.to);
				}
			}		
		}
		return vis[t];
	}
	int dfs(int x,int a)
	{
		if(x == t || a == 0)return a;
		int flow = 0, f;
		for(int& i = cur[x];i < G[x].size();++i)
		{
			Edge& e = edges[G[x][i]];
			if(d[x] + 1 == d[e.to] && (f = dfs( e.to, min(a, e.cap-e.flow)))>0)
			{
				e.flow += f;
				edges[G[x][i]^1].flow -= f;
				flow += f;
				a -= f;
				if(a == 0)break; 
			}
		}
		return flow;
	}
	int maxflow()
	{
		int flow = 0;
		while(bfs())
		{
			memset(cur, 0, sizeof(cur));
			flow += dfs(s,inf);
		}
		return flow;
	}
}gao;//刘汝佳网络流dinic板子
int main()
{
	int t;
	scanf("%d", &t);
	int cnt = 1;
	while(t--)
	{
		int n, m;
		printf("Case %d: ", cnt++);
		scanf("%d%d", &n, &m);
		gao.init(n+500+2, n+500+1, n+500+2);
		int sum = 0;
		for(int i = 1;i <= n;++i)
		{
			int pi, si, ei;
			scanf("%d%d%d", &pi, &si, &ei);
			sum += pi;
			gao.add_edge(n+500+1, i, pi);
			for(int j = si;j <= ei;j++)
			{
				gao.add_edge(i, n+j, 1);
			}
		}
		for(int i = 1;i <= 500;i++)
		{
			gao.add_edge(n+i, n+500+2, m);
		}
		int d = gao.maxflow();
		if(d == sum)
		{
			printf("Yes\n\n");	
		} 
		else{
			printf("No\n\n");
		}
	} 
	return 0;
}

 

你可能感兴趣的:(图论)