程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> HDU3572_Task Schedule(網絡流最大流)

HDU3572_Task Schedule(網絡流最大流)

編輯:C++入門知識

HDU3572_Task Schedule(網絡流最大流)


解題報告

題意:

工廠有m台機器,需要做n個任務。對於一個任務i,你需要花費一個機器Pi天,而且,開始做這個任務的時間要>=Si,完成這個任務的時間<=Ei。對於一個任務,只能由一個機器來完成,一個機器同一時間只能做一個任務。但是,一個任務可以分成幾段不連續的時間來完成。問,能否做完全部任務。

思路:

網絡流在於建模,這題建模方式是:

把每一天和每個任務看做點。由源點到每一任務,建容量為pi的邊(表示任務需要多少天完成)。每個任務到每一天,若是可以在這天做任務,建一條容量為1的邊,最後,把每天到匯點再建一條邊容量m(表示每台機器最多工作m個任務)。

#include 
#include 
#include 
#include 
#include 
#include 
#define inf 99999999
using namespace std;
int n,m,l[2010],head[2010],cnt,M;
struct node
{
    int v,w,next;
} edge[555000];
void add(int u,int v,int w)
{
    edge[M].v=v;
    edge[M].w=w;
    edge[M].next=head[u];
    head[u]=M++;

    edge[M].v=u;
    edge[M].w=0;
    edge[M].next=head[v];
    head[v]=M++;
}
int bfs()
{
    memset(l,-1,sizeof(l));
    l[0]=0;
    int i,u,v;
    queueQ;
    Q.push(0);
    while(!Q.empty())
    {
        u=Q.front();
        Q.pop();
        for(i=head[u]; i!=-1; i=edge[i].next)
        {
            v=edge[i].v;
            if(l[v]==-1&&edge[i].w>0)
            {
                l[v]=l[u]+1;
                Q.push(v);
            }
        }
    }
    if(l[cnt]>0)return 1;
    return 0;
}
int dfs(int u,int f)
{
    int a,i;
    if(u==cnt)return f;
    for(i=head[u]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(l[v]==l[u]+1&&edge[i].w>0&&(a=dfs(v,min(f,edge[i].w))))
        {
            edge[i].w-=a;
            edge[i^1].w+=a;
            return a;
        }
    }
    l[u]=-1;//沒加優化會T
    return 0;
}
int main()
{
    int t,i,j,s,p,e,k=1;
    scanf("%d",&t);
    while(t--)
    {
        M=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        int sum=0,maxx=0;
        for(i=1; i<=n; i++)
        {
            scanf("%d%d%d",&p,&s,&e);
            add(0,i,p);
            sum+=p;
            if(maxx

Task Schedule

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


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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved