Poj1698_alice's chance (bipartite graph multi-maximum matching/maximum Stream)

Source: Internet
Author: User

Solution report

Http://blog.csdn.net/juncoder/article/details/38237641

Question Portal

Question:

There are n movies, each of which has a fixed recording time every week and must be completed by W weeks ago. There is an actor who can only play one movie every day. For each movie, it must be completed after D days.

Ideas:

The multi-maximum matching problem in the bipartite graph. For each movie, the source point is connected to each movie with a side capacity of D, and the movie is mapped to the video produced seven days a week. The size is 1, the capacity for connecting to the sink point every day is 1


In the maximum matching of a bipartite graph, each vertex (either X or Y) can only be associated with a matching edge. However, we often encounter this problem, that is, a vertex in a bipartite graph can be associated with multiple matching edges, but there is an upper limit, or, lI indicates the maximum number of matching edges that a vertex I can associate.

Create Source Vertex S and sink vertex T on the source image, and s connects an edge with the capacity of the X vertex L value to each X vertex, each y-square vertex connects to t an edge with a capacity of the L value of the Y-square vertex. In the original bipartite graph, each edge still exists in the new network, the capacity is 1 (if the edge can be used multiple times, the capacity is greater than 1). The maximum flow of the network is the value of the Multi-maximum matching of the Bipartite Graph.


#include <queue>#include <cstdio>#include <cstring>#include <iostream>#define inf 99999999using namespace std;int mmap[400][400],n,l[400],m;int bfs(){    memset(l,-1,sizeof(l));    queue<int>Q;    Q.push(0);    l[0]=0;    while(!Q.empty()) {        int u=Q.front();        Q.pop();        for(int i=0; i<=m; i++) {            if(l[i]==-1&&mmap[u][i]) {                l[i]=l[u]+1;                Q.push(i);            }        }    }    if(l[m]>1)        return 1;    return 0;}int dfs(int x,int f){    int a;    if(x==m)return f;    for(int i=0; i<=m; i++) {        if(l[i]==l[x]+1&&mmap[x][i]&&(a=dfs(i,min(f,mmap[x][i])))) {            mmap[x][i]-=a;            mmap[i][x]+=a;            return a;        }    }    l[x]=-1;    return 0;}int main(){    int t,_h[10],d,w;    cin>>t;    while(t--) {        memset(mmap,0,sizeof(mmap));        memset(_h,0,sizeof(_h));        cin>>n;        int mon=0;        int sum=0;        for(int i=1; i<=n; i++) {            scanf("%d%d%d%d%d%d%d%d%d",&_h[1],&_h[2],&_h[3],&_h[4],&_h[5],&_h[6],&_h[7],&d,&w);            mmap[0][i]=d;            sum+=d;            if(mon<w)                mon=w;            for(int j=0; j<w; j++) {                for(int k=1; k<=7; k++) {                    if(_h[k])                        mmap[i][n+j*7+k]=1;                }            }        }        m=n+mon*7+1;        for(int i=n+1; i<=n+mon*7; i++)            mmap[i][m]=1;        int ans=0,a;        while(bfs())            while(a=dfs(0,inf))                ans+=a;        if(sum==ans)            printf("Yes\n");        else printf("No\n");    }    return 0;}

Alice's chance
Time limit:1000 ms   Memory limit:10000 K
Total submissions:5277   Accepted:2168

Description

Alice, a charming girl, have been dreaming of being a movie star for long. her chances will come now, for several filmmaking companies invite her to play the chief role in their new films. unfortunately, all these companies will start making the films at the same time, and the greedy Alice doesn't want to miss any of them !! You are asked to tell her whether she can act in all the films.

As for a film,
  1. It will be made only on some fixed days in a week, I. e., Alice can only work for the film on these days;
  2. Alice shoshould work for it at least for specified number of days;
  3. The film must be finished before a prearranged deadline.

For example, assuming a film can be made only on Monday, Wednesday and Saturday; Alice shocould work for the film at least for 4 days; and it must be finished within 3 weeks. in this case she can work for the film on Monday of the first week, on Monday and Saturday of the second week, and on Monday of the third week.

Notice that on a single day Alice can work on at most one film.

Input

The first line of the input contains a single integer T (1 <= T <= 20), the number of test cases. then T cases follow. each test case begins with a single line containing an integer N (1 <= n <= 20), the number of films. each of the following n lines is in the form of "F1 F2 F3 F4 F5 F6 F7 d w ". FI (1 <= I <= 7) is 1 or 0, representing whether the film can be made on the I-th day in a week (a week starts on Sunday ): 1 means that the film can be made on this day, while 0 means the opposite. both D (1 <= d <= 50) and W (1 <= W <= 50) are integers, and Alice shoshould go to the film for D days and the film must be finished in W weeks.

Output

For each test case print a single line, 'yes' if Alice can attend all the films, otherwise 'no '.

Sample Input

220 1 0 1 0 1 0 9 30 1 1 1 0 0 0 6 420 1 0 1 0 1 0 9 40 1 1 1 0 0 0 6 2

Sample output

YesNo

Hint

A proper schedule for the first test case:date     Sun    Mon    Tue    Wed    Thu    Fri    Satweek1          film1  film2  film1         film1week2          film1  film2  film1         film1week3          film1  film2  film1         film1week4          film2  film2  film2

Source


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.