Task Schedule
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11515 Accepted Submission(s): 3507
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
思路:很经典的建图模型,源点与任务连接一条容量为p的边,任务与每天连接一条容量为1的边,每天与汇点连接一条容量为m的边,然后跑网络流
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 4010;
const int MAXM = 40010;
const int INF = 0x3f3f3f3f;
struct Edge1
{
int from,to,cap,flow;
};
struct Dinic
{
int n,m,s,t;
vector<Edge1> edges;
vector<int> G[MAXN];
bool vis[MAXN];
int d[MAXN];
int cur[MAXN];
void init(int n)
{
this -> n = n;
for(int i = 0; i <= n + 1; i++){
G[i].clear();
}
edges.clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back((Edge1){from,to,cap,0});
edges.push_back((Edge1){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));
queue<int> Q;
Q.push(s);
d[s] = 0;
vis[s] = 1;
while(!Q.empty()) {
int x = Q.front();
Q.pop();
for(int i = 0; i < G[x].size(); i++) {
Edge1& e = edges[G[x][i]];
if(!vis[e.to] && e.cap > e.flow) {
vis[e.to] = 1;
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++) {
Edge1& 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 s,int t) {
this -> s = s,this -> t = t;
int flow = 0;
while(BFS()) {
memset(cur,0,sizeof(cur));
flow += DFS(s,INF);
}
return flow;
}
}din;
int vis[MAXN];
int main(void)
{
int T,n,m,flow;
int p,s,e;
int kase = 0;
scanf("%d",&T);
while(T--) {
kase++;
scanf("%d%d",&n,&m);
memset(vis,0,sizeof(vis));
flow = 0;
din.init(550 + n);
for(int i = 1; i <= n; i++) {
scanf("%d %d %d",&p,&s,&e);
flow += p;
//源点与任务连接一条容量为p的边
din.AddEdge(0,500 + i,p);
for(int j = s; j <= e; j++) {
//任务与每天连接一条容量为1的边
din.AddEdge(500 + i,j,1);
vis[j] = 1;
}
}
for(int i = 1; i <= 500; i++) {
if(vis[i]) {
//天与汇点连接一条容量为m的边
din.AddEdge(i,500 + n + 1,m);
}
}
printf("Case %d: %s\n\n",kase,din.Maxflow(0,500 + n + 1) == flow? "Yes" : "No");
}
return 0;
}
/*
2
4 3
1 3 5
1 1 4
2 3 7
3 5 9
2 2
2 1 3
1 2 2
*/