hdu 3572(板子

来源:互联网 发布:装修软件哪个好 编辑:程序博客网 时间:2024/06/15 18:31

传送门

Task Schedule

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


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
24 31 3 5 1 1 42 3 73 5 92 22 1 31 2 2
 

Sample Output
Case 1: Yes Case 2: Yes
 

Author
allenlowesy
 

Source
2010 ACM-ICPC Multi-University Training Contest(13)——Host by UESTC
 

Recommend
zhouzeyong

题意:T组样例。n组数据。在si--ei这些天里要完成pi天的任务,问在m个机器人下。是否能完成所有的任务。

idea:每次输入数据的时候找一下最大的天数。每个任务要完成pi天。所以建每个任务i(0,i,pi)然而每天只能有一台机器可以运行,所以限制容量(i,n+j,1)n+j是从si到ei的每天。建一个比较大的点作为汇点n+maxc+1。每个任务最多用m台机器(对容量进行限制) (n+i,sink,m)

最后看每个pi汇总之后是否跟在流中的流量一样就是了。

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>using namespace std;const int maxn = 50007;const int maxm = 505000;const int oo = 99999999;int idx;int cur[maxn], pre[maxn];int dis[maxn], gap[maxn];int aug[maxn], head[maxn];struct Node{    int u, v, w;    int next;}edge[maxm];void addEdge(int u, int v, int w){    edge[idx].u = u;    edge[idx].v = v;    edge[idx].w = w;    edge[idx].next = head[u];    head[u] = idx++;    edge[idx].u = v;    edge[idx].v = u;    edge[idx].w = 0;    edge[idx].next = head[v];    head[v] = idx++;}int SAP(int s, int e, int n){    int max_flow = 0, v, u = s;    int id, mindis;    aug[s] = oo;    pre[s] = -1;    memset(dis, 0, sizeof(dis));    memset(gap, 0, sizeof(gap));    gap[0] = n; // 我觉得这一句要不要都行,因为dis[e]始终为0    for (int i = 0; i <= n; ++i)    {   // 初始化当前弧为第一条弧        cur[i] = head[i];    }    while (dis[s] < n)    {        bool flag = false;        if (u == e)        {            max_flow += aug[e];            for (v = pre[e]; v != -1; v = pre[v]) // 路径回溯更新残留网络            {                id = cur[v];                edge[id].w -= aug[e];                edge[id^1].w += aug[e];                aug[v] -= aug[e]; // 修改可增广量,以后会用到                if (edge[id].w == 0) u = v; // 不回退到源点,仅回退到容量为0的弧的弧尾            }        }        for (id = cur[u]; id != -1; id = edge[id].next)        {   // 从当前弧开始查找允许弧            v = edge[id].v;            if (edge[id].w > 0 && dis[u] == dis[v] + 1) // 找到允许弧            {                flag = true;                pre[v] = u;                cur[u] = id;                aug[v] = min(aug[u], edge[id].w);                u = v;                break;            }        }        if (flag == false)        {            if (--gap[dis[u]] == 0) break; /* gap优化,层次树出现断层则结束算法 */            mindis = n;            cur[u] = head[u];            for (id = head[u]; id != -1; id = edge[id].next)            {                v = edge[id].v;                if (edge[id].w > 0 && dis[v] < mindis)                {                    mindis = dis[v];                    cur[u] = id; // 修改标号的同时修改当前弧                }            }            dis[u] = mindis + 1;            gap[dis[u]]++;            if (u != s) u = pre[u]; // 回溯继续寻找允许弧        }    }    return max_flow;}int map[300][300];int main(){    int t, n, m, pi, si, ei;    int Max, sum, source, sink, vn;    scanf("%d", &t);    //t=1;    for (int cas = 1; cas <= t; ++cas)    {        scanf("%d%d",&n,&m);        //while(scanf("%d%d",&n,&m)!=EOF)        {            idx = 0;            memset(head, -1, sizeof(head));            sum = 0; source = 0; Max = 0;            for (int i = 1; i <= n; ++i)            {                scanf("%d %d %d", &pi, &si, &ei);                sum += pi;                Max = max(Max, ei);                addEdge(source, i, pi);                for (int j = si; j <= ei; ++j)                {                    addEdge(i, n + j, 1);                }            }            sink = n + Max + 1;            vn = sink + 1;            for (int i = 1; i <= Max; ++i)            {                addEdge(n + i, sink, m);            }            if (SAP(source, sink, vn) == sum)                printf("Case %d: Yes\n\n", cas);            else printf("Case %d: No\n\n", cas);        }    }    return 0;}



原创粉丝点击