Hdu3572:task Schedule (maximum flow)

Source: Internet
Author: User
Tags cas printf

Task Schedule Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 8673 Accepted Submission (s): 2664


Problem Description Our geometry Princess XMM have stoped her study in computational geometry to concentrate on her newly o Pened Factory. Her factory had introduced M new machines in order to process the coming N tasks. For the i-th task, the factory have to start processing it on or after day Si, process it for Pi days, and finish the task Before or at day Ei. A machine can have work on one task at a time, and each task can be processed by at a time. However, a task can is interrupted and processed on different machines on different days.
Now she wonders whether he had a feasible schedule to finish all the tasks in time. She turns to the help.

Input on the first line comes an integer T (t<=20), indicating the number of test cases.

You are given-N (n<=500) and M (m<=200) on the first line of all test case. Then on each of next N lines is three integers Pi, si and Ei (1<=pi, Si, ei<=500), which has the meaning described In the description. It is guaranteed, this in a feasible schedule every task, can be finished would be do 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 acm-icpc multi-university Training Contest (--host by UESTC)

Test Instructions: to n tasks, each task within the Si~ei days of any pi days to complete, there are M machines, each time each machine can only handle a task, ask whether you can complete all tasks.

idea: The maximum flow, set a super source point, to each task to make the edge, the edge of the task of pi, each task to its si~ei as the edge, the edge of 1, each selected Si~ei these points to the meeting point of the Edge, the edge of M, indicating that the maximum number of M machines at the same time each day operation, Then see if the maximum flow is pi and can, consider the source and meeting point exchange over, faster than 700 MS, using the Dinic algorithm, previously written dinic too weak infinite tle, there are two optimizations, one is in the hierarchy of the DFS processing all the augmented road, Second, the current node can not reach the meeting point is set to 1, the next time no longer search this road.

# include <iostream> # include <cstdio> # include <cstring> using namespace std;
const int MAXN = 2E3+3;
const int INF = 0x7fffffff;
int cnt=0, DIS[MAXN], NEXT[MAXN], VIS[MAXN], Q[MAXN]; struct Node {int E, W, next;}

EDGE[200000];
    void Add_edge (int u, int v, int w) {edge[cnt].e = v;
    EDGE[CNT].W = W;
    Edge[cnt].next = Next[u];
    Next[u] = cnt++;
    EDGE[CNT].E = u;
    EDGE[CNT].W = 0;
    Edge[cnt].next = Next[v];
NEXT[V] = cnt++;
    } bool BFs () {memset (DIS,-1, sizeof (DIS));
    Dis[0] = 0;
    int l=0, r=0;
    q[r++] = 0;
        while (l<r) {int u = q[l];
        ++l;
            for (int i=next[u]; i!=-1; i=edge[i].next) {int v = edge[i].e, w = EDGE[I].W;
                if (dis[v]==-1 && w>0) {Dis[v] = Dis[u] + 1;
            q[r++] = v;
}}} return dis[1888]! =-1;
    } int Dfs (int u, int pre) {int f = 0, ans = 0; if (U = = 1888) retUrn Pre;
        for (int i=next[u]; i!=-1; i=edge[i].next) {int v = edge[i].e, w = EDGE[I].W;
            if (dis[v]==dis[u]+1 && w>0 && (F=dfs (V, Min (pre, W)))) {EDGE[I].W-= f;
            EDGE[I^1].W + = f;
            Ans + = f;
            Pre-= f;
        if (!pre) break;
    }} if (ans) return ans;
    Dis[u] =-1;
return 0;
    } int main () {int A, B, C, T, N, M, Sum, Total,cas=1;
    scanf ("%d", &t);
        while (t--) {total = cnt = SUM = 0;
        scanf ("%d%d", &n,&m);
        Memset (Next,-1, sizeof (next));
        memset (Vis, 0, sizeof (VIS));
        int imin = INF, Imax = 0;
            for (int i=1; i<=n; ++i) {scanf ("%d%d%d", &a,&b,&c);
                for (int j=b; j<=c; ++j) {Add_edge (J, i+500, 1);
                    if (!vis[j]) {vis[j] = 1; Add_edge (0, J, M);
            }} Add_edge (i+500, 1888, a);
        Total + = A;
        } int add;
        while (BFS ()) sum + = DFS (0, INF);
        if (sum = = total) printf ("Case%d:yes\n\n", cas++);
    else printf ("Case%d:no\n\n", cas++);
} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.