POJ_P1698 Alice's Chance(拆点最大流+Dinic算法)

23 篇文章 0 订阅
23 篇文章 0 订阅

Alice’s Chance
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 6587 Accepted: 2696

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,
it will be made ONLY on some fixed days in a week, i.e., Alice can only work for the film on these days;
Alice should work for it at least for specified number of days;
the film MUST be finished before a prearranged deadline.
For example, assuming a film can be made only on Monday, Wednesday and Saturday; Alice should 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 should 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
2
2
0 1 0 1 0 1 0 9 3
0 1 1 1 0 0 0 6 4
2
0 1 0 1 0 1 0 9 4
0 1 1 1 0 0 0 6 2

Sample Output
Yes
No

Hint
A proper schedule for the first test case:
date Sun Mon Tue Wed Thu Fri Sat
week1 film1 film2 film1 film1
week2 film1 film2 film1 film1
week3 film1 film2 film1 film1
week4 film2 film2 film2

Source
POJ Monthly–2004.07.18

寒假ing;
题目很简单,也很容易的想到拆点,就不啰嗦了,直接说构图
按星期拆点,将源点连接所有电影,容量为D,电影连接,前W个星期可以参加的日子,再将每天和汇点相连即可,容量为1,限制每天只能一件事;
最后判断流量是否等于所有需要的天数即可;

#include<cstdio>
#include<climits>
#include<queue>
#include<vector>
#include<cstring>
#include<iostream>
using namespace std;
#define N 21
#define M 51
#define INF INT_MAX/3*2
struct NetWork{
    struct Edge{int fr,to,flow;};
    vector<Edge> edge;vector<int> g[7*M+N+2];
    int flow,tot,S,T;
    int cur[7*M+N+2],d[7*M+N+2],p[7*M+N+2];bool b[7*M+N+2];

    void Add_Edge(int fr,int to,int flow){
        edge.push_back(Edge{fr,to,flow});edge.push_back(Edge{to,fr,0});
        g[fr].push_back(edge.size()-2);g[to].push_back(edge.size()-1);
    }

    void in(int &x){
        x=0;char ch=getchar();
        while(ch>'9'||ch<'0') ch=getchar();
        while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
    }

    int hash(int i,int j){return (i-1)*7+j+N+2;}

    void init(int n){
        S=0,T=1;int p[8],D,W;
        for(int i=1;i<=7;i++) in(p[i]);in(D),in(W);
        tot+=D;Add_Edge(S,n+2,D);
        for(int i=1;i<=7;i++){
            if(p[i])
                for(int j=1;j<=W;j++){
                    Add_Edge(n+2,hash(j,i),1);
                } 
        }
    }

    bool bfs(){
        memset(b,0,sizeof(b));memset(d,0,sizeof(d));
        queue<int> q;q.push(S);b[S]=true;d[S]=1;
        while(!q.empty()){
            int x=q.front();q.pop();
            for(int i=0;i<g[x].size();i++){
                Edge &e=edge[g[x][i]];
                if(!b[e.to]&&e.flow>0)
                    q.push(e.to),b[e.to]=true,d[e.to]=d[x]+1;
            }
        }
        return b[T];
    }

    void MaxFlow(){
        while(bfs()){
            memset(cur,0,sizeof(cur));
            int x=S,k=0;
            for(;;){
                 if(x==T){
                    int mine=-1,minf=INF;
                    for(int i=0;i<k;i++)
                        if(edge[p[i]].flow<minf){
                            minf=edge[p[i]].flow;
                            mine=i;
                        }
                    for(int i=0;i<k;i++){
                        edge[p[i]].flow-=minf;
                        edge[p[i]^1].flow+=minf;
                    }
                    k=mine;x=edge[p[mine]].fr;flow+=minf;
                }
                for(int &i=cur[x];i<g[x].size();i++){
                    Edge &e=edge[g[x][i]];
                    if(e.flow>0&&d[x]+1==d[e.to]) break;
                }
                if(cur[x]<g[x].size()){
                    p[k++]=g[x][cur[x]];
                    x=edge[g[x][cur[x]]].to;
                }
                else{
                    if(k==0) break;
                    d[x]=-1;k--;x=edge[p[k]].fr;
                }
            }
        }
    }

    bool cando(){
        bool b;
        for(int i=1;i<M;i++)
            for(int j=1;j<=7;j++)
                Add_Edge(hash(i,j),T,1);
        MaxFlow();
//      printf("%d %d\n",tot,flow);
        b=flow>=tot?true:false;
        for(int i=0;i<7*M+N+2;i++) g[i].clear();edge.clear();
        tot=flow=0;
        return b;
    }
}s;
int main(){
    int t,n;scanf("%d",&t);
    while(t--){
        scanf("%d",&n);
        while(n--)
            s.init(n);
        if(s.cando()) printf("Yes\n");else printf("No\n");
//      cout<<"qwq"<<endl;
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值