HDU3572-网络流。。。不会建图

Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 12417 Accepted Submission(s): 3771

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
题意:就是说某人有m个机器人,n个任务,之后给出每个任务的开始时间和最迟结束时间,还有一个任务完成需要的时间。问能不能再规定时间内完成所有的任务
思路:先建图。。。。但是一开始建图都不会,然后去看了下博客才知道怎么建图。首先0是起点,之后连接每个任务,任务连接从开始时间到结束时间的每个时间点,最后时间点连接终点。然后用网络流最大流就行了,每个任务所需的时间和是最大流就是Yes。
AC代码:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn=1000000+10;
const int INF=0x7f7f7f7f;
struct Node
{
	int end,value,next;
}map[maxn];
int head[maxn],n,m,start,index,sum;
int Max,End;
int flow[maxn],pre[maxn];
void Init()
{
	sum=0;index=0;Max=-1;start=0;
	memset(head,-1,sizeof(head));
	return ;
}
void addedge(int start,int end,int value)
{
	map[index].end=end;
	map[index].value=value;
	map[index].next=head[start];
	head[start]=index++;
	map[index].next=head[end];
	map[index].value=value;
	map[index].end=start;
	head[end]=index++;
	return ;
}
bool bfs()
{
	queue<int> q;
	q.push(start);
	memset(pre,-1,sizeof(pre));
	pre[start]=0;
	while (!q.empty())
		{
			int now=q.front();
			q.pop();
			for (int i=head[now];i!=-1;i=map[i].next)
				if (pre[map[i].end]<0&&map[i].value>0)
					{
						pre[map[i].end]=pre[now]+1;	//注意这里要+1,不然会超内存
						q.push(map[i].end);
					}
		}
	return pre[End]!=-1;
}
int dfs(int now,int flow)
{
	int deta=0,temp=0;
	if (now==End)
		return flow;
	for (int i=head[now];i!=-1;i=map[i].next)
		if (map[i].value>0&&pre[now]==pre[map[i].end]-1)
			{
				temp=dfs(map[i].end,min(flow-deta,map[i].value));
				if (temp>0)
					{
						map[i].value-=temp;
						map[i^1].value+=temp;
						deta=deta+temp;
						if (deta==flow) break;
					}
				else pre[map[i].end]=-1;
			}
	return deta;
}
int dinic()
{
	int ans=0,flow=0;
	while (bfs())
		ans=ans+dfs(0,INF);
	return ans;
}
int main()
{
	int pi,si,ei;
	int t,k=1;
	cin>>t;
	while (t--)
		{
			cin>>n>>m;
			Init();
			for (int i=1;i<=n;i++)
				{
					scanf("%d%d%d",&pi,&si,&ei);
					sum=sum+pi;
					Max=max(Max,ei);
					addedge(start,i,pi);
					for (int j=si;j<=ei;j++)
						addedge(i,j+n,1);
				}
			End=n+Max+1;
			for (int i=1;i<=Max;i++)
				addedge(i+n,End,m);
			int ans=dinic();
			printf("Case %d: ",k++);
			if (ans!=sum) cout<<"No"<<endl;
			else cout<<"Yes"<<endl;
			cout<<endl;
		}
	return 0;
}
发布了46 篇原创文章 · 获赞 2 · 访问量 3211

猜你喜欢

转载自blog.csdn.net/z1164754004z/article/details/88087879