HDU 3572 Task Schedule 网络流 判断满流 建图

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Tawn0000/article/details/82594984

                                               Task Schedule

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 11635    Accepted Submission(s): 3533


 

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

一开始读错题, 以为每个任务都要连续完成。 然后gg

网络流 判断满流 建图 : 一个源点0,一个汇点t = 500 + n + 1;  每天建一个点 1 - 500 ,每个人物建一个点 500 + 1 - 500 + n;

从源点连边到每个task ,权值 为pi 。  从每个task 连边都每一个可以处理它的day的节点,权值为1,从每天连边到t,权值为m。从0-t 跑最大流,判断是否等于所有p之和,等于则满足。

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstring>
#include <map>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 2000 + 100;
const int maxm = 1000000 + 100;
typedef pair<int,int> P;
int n,m;
int l[maxn];//记录层数
int h[maxn];//链式前向星
int cur[maxn];
int tot = 0;
struct edge
{
 int to;
 int c;
 int next;
 edge(int x = 0, int y = 0, int z = 0) : to(x), c(y), next(z) {}
}es[maxm*2];//记录边 注意是2倍

void add_edge(int u, int v, int c)
{
   es[tot] = edge(v,c,h[u]);
   h[u] = tot++;
}



bool bfs(int s, int t)
{
  memset(l,0,sizeof(l));
  l[s] = 1;
  queue <int> q;
  q.push(s);
  while(!q.empty())
  {
   int u = q.front();
   q.pop();
   //cout << u <<  " " <<l[u] << endl;
   if(u == t)  return true;
   for(int i = h[u]; i != -1; i = es[i].next)
       {
        int v = es[i].to;
        if(!l[v] && es[i].c) {l[v] = l[u] + 1; q.push(v);}
       }
  }
  return false;
}

int dfs(int x, int t, int mf)
{

   if(x == t) return mf;
   int ret = 0;
   for(int &i = cur[x]; i != -1; i = es[i].next)
   {
    //  cout << x << " " << l[es[i].to]  << " "<< "mf = " << mf << endl;
     if(es[i].c && l[x] == l[es[i].to] - 1)
     {
      // cout << es[i].c << " "<< l[es[i].to] << endl;
       int f = dfs(es[i].to,t,min(es[i].c,mf - ret));
       es[i].c -= f;
       es[i^1].c += f;
       ret += f;
       if(ret == mf) return ret;
     }
   }
   return ret;
}

int dinic(int s, int t)
{
 int ans = 0;
 while(bfs(s,t))  {
   for(int i =
      0; i <= t; i++) cur[i] = h[i];
   int f = dfs(s,t,INF);
   ans += f;
   //if(f == 0) return ans;
   //cout << f << endl;
 }
 return ans;
}
int main()
{
  int T;
  scanf("%d",&T);
  int TT = T;
   while(T--)
   {
   scanf("%d%d",&n,&m);
   tot = 0;
   memset(h,-1,sizeof(h));
   int  t = 500 + n + 1;
   int p,s,e;
   int res = 0;
   for(int i = 1; i <= 500; i++) {add_edge(i,t,m);add_edge(t,i,0);}
   for(int i = 1; i <= n; i++)
   {
    scanf("%d%d%d",&p,&s,&e);
    add_edge(0,500+i,p);
    add_edge(500+i,0,0);
    res += p;
    for(int j = s; j <= e; j++)
    {
      add_edge(i+500,j,1);
      add_edge(j,i+500,0);//增加反向边
    }
   }
   int ans = dinic(0,t);
   printf("Case %d: ",TT-T);
   if(ans >= res) printf("Yes\n");
   else printf("No\n");
   printf("\n");
   }
   return 0;
}

猜你喜欢

转载自blog.csdn.net/Tawn0000/article/details/82594984