网址http://acm.hdu.edu.cn/showproblem.php?pid=3572
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.
题意:T组数据,N个任务,M个机器。第i个任务要进行Si天,必须在Pi到Ei天之间内完成。每个任务不能同时由多个机器做。每个机器一天只能做一个任务。问任务能否完成。
思路:建立一个源点S=0.然后把每个任务和天当做一个点。任务为编号501,502······,天的编号为1,2,3······。最后设一个汇点,我把汇点编号设为1004。源点和任务点建边,容量为该任务可完成的时间长度(Ei-Pi+1)。任务点和对应的天数点连边,容量为1。每个天跟汇点连边,容量为机器数量。最后比较最大流和总任务完成天数是否相等。相等则说明可以完成。否则不可。
#include<iostream>
#include<string>
#include<algorithm>
#include<stdlib.h>
#include<stdio.h>
#include<queue>
#include<vector>
using namespace std;
#define N 1000+5
#define INF 0x3f3f3f3f
#define mem(arr,a) memset(arr,a,sizeof(arr))
/*********************************/
int t;
int n, m;
int cost[N][N];
int ranks[N];
int iter[N];
int vis[N];
struct edge{
int to, cap, rev;
};
vector<edge> G[N];
const int T = 500;
void add(int from, int to, int cap){
G[from].push_back(edge{ to, cap, G[to].size() });
G[to].push_back(edge{ from, 0, G[from].size() - 1 });
}
bool bfs(){
mem(ranks, -1);
ranks[0] = 0;
queue<int>q;
q.push(0);
while (!q.empty()){
int t = q.front(); q.pop();
for (int i=0; i<G[t].size(); i++){
edge&e = G[t][i];
if (e.cap>0&&ranks[e.to]<0){
ranks[e.to] = ranks[t] + 1;
q.push(e.to);
}
}
}
if (ranks[N - 1]>0)return 1;
return 0;
}
int dfs(int x, int flow){
if (x == N - 1)return flow;
for (int &i = iter[x]; i<G[x].size(); i++){
int a;
edge&e = G[x][i];
if (e.cap>0&&ranks[e.to] == ranks[x] + 1 && (a = dfs(e.to, min(e.cap, flow)))){
if (a>0){
e.cap -= a;
G[e.to][e.rev].cap += a;
return a;
}
}
}
return 0;
}
int dinic(){
int sum = 0;
while (bfs()){
mem(iter, 0);
while (int a = dfs(0, INF))
sum += a;
}
return sum;
}
int main(){
cin >> t;
int cnt=1;
while (t--){
scanf("%d%d", &n, &m);
mem(G,0);
mem(vis, 0);
int sum = 0;
for (int i = 0; i < n; i++){
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
for (int j = b; j <= c; j++){
add(T + i + 1, j, 1);
if (!vis[j])
{
vis[j] = 1;
add(j, N - 1, m);
}
}
sum += a;
add(0, T + i + 1, a);
}
printf("Case %d: ", cnt++);
if (sum == dinic()){
printf("Yes\n");
}
else printf("No\n");
cout << endl;
}
}