hdu 3572 仪器与任务 最大流 好题 体会建图思想

Posted smilesundream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hdu 3572 仪器与任务 最大流 好题 体会建图思想相关的知识,希望对你有一定的参考价值。

Task Schedule

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


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
 
题意:给N个任务,M台机器。每个任务有最早才能开始做的时间S,deadline E,和持续工作的时间P。每个任务可以分段进行,但是在同一时刻,一台机器最多只能执行一个任务. 问存不存在可行的工作时间。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
#define MM(a,b) memset(a,b,sizeof(a))
typedef long long ll;
typedef unsigned long long ULL;
const int mod = 1000000007;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int big=50000;
int max(int a,int b) {return a>b?a:b;};
int min(int a,int b) {return a<b?a:b;};
struct edge{
   int to,cap,rev;
};

vector<edge> G[1010];
map<string,int> mp;
int n,m,k,daymax,level[1010],iter[1010];
int p[1010],s[1010],e[1010],sum;

void add_edge(int u,int v,int cap)
{
    G[u].push_back(edge{v,cap,G[v].size()});
    G[v].push_back(edge{u,0,G[u].size()-1});
}

void bfs(int s)
{
    queue<int> q;
    q.push(s);
    level[s]=1;
    while(q.size())
    {
        int now=q.front();q.pop();
        for(int i=0;i<G[now].size();i++)
        if(G[now][i].cap>0)
        {
            edge e=G[now][i];
            if(level[e.to]<0)
              {
                  level[e.to]=level[now]+1;
                  q.push(e.to);
              }
        }
    }
}
int dfs(int s,int t,int minn)
{
    if(s==t)
        return minn;
    for(int &i=iter[s];i<G[s].size();i++)
    {
        edge &e=G[s][i];
        if(level[e.to]>level[s]&&e.cap>0)
        {
            int k=dfs(e.to,t,min(minn,e.cap));
            if(k>0)
             {
                 e.cap-=k;
                 G[e.to][e.rev].cap+=k;
                 return k;
             }
        }
    }
    return 0;
}

int max_flow(int s,int t)
{
    int ans=0,temp;
    for(;;)
    {
        memset(level,-1,sizeof(level));
        bfs(s);
        if(level[t]<0)
            return ans;
        memset(iter,0,sizeof(iter));
        while((temp=dfs(s,t,inf))>0)
            ans+=temp;
    }
    return ans;
}

void build()
{
    for(int i=0;i<=n+daymax+1;i++) G[i].clear();

    for(int i=1;i<=n;i++)
       add_edge(0,i,p[i]);

    for(int i=n+1;i<=n+daymax;i++)
       add_edge(i,n+daymax+1,m);

    for(int i=1;i<=n;i++)
        for(int j=1;j<=daymax;j++)
        if(j>=s[i]&&j<=e[i])
            add_edge(i,j+n,1);
}

int main()
{
    int cas,kk=0;
    scanf("%d",&cas);
    while(cas--)
    {
        daymax=0;sum=0;
        scanf("%d %d",&n,&m);
        for(int i=1;i<=n;i++)
                {
                    scanf("%d %d %d",&p[i],&s[i],&e[i]);
                    if(e[i]>daymax) daymax=e[i];
                    sum+=p[i];
                }

        build();
        printf("Case %d: ",++kk);
        if(max_flow(0,n+daymax+1)==sum)
           printf("Yes\n");//刚开始输出YES,wa了好久,剁手了,以后输出格式都直接粘贴!
        else printf("No\n");
        printf("\n");
    }
    return 0;
}

  体会建图思想,刚开始我想的是建立一个天数与机器的二元组,然后向汇点连接一条容量为1的边,但是算下来就是会超时了,,因为点太多了,,,其实只要将天数向汇点连接容量为仪器数量的边就好了,这样就控制了仪器的使用数量,然后就是任务向仪器连边,跑跑最大流就可以了

以上是关于hdu 3572 仪器与任务 最大流 好题 体会建图思想的主要内容,如果未能解决你的问题,请参考以下文章

HDU 3572最大流+数组开小会TLE

hdu-3572 Task Schedule---最大流判断满流+dinic算法

HDU3572构造图的模型

HDU 3572 Task Schedule(ISAP模板&amp;&amp;最大流问题)

HDU 3572 Task Schedule(最大流判断满流)

hdu3572 最大流