ACM ICPC 2013-2014 H. Those are not the droids you're looking for(二分匹配-Dinic)

Description
士兵在一家酒吧寻找逃跑的机器人,酒吧老板说酒吧里没有机器人,只有走私者和货运者两种人,走私者最少停留a min,货运者最多停留b min,现在士兵只知道酒吧的进出情况,即某时刻有人进入酒吧或者有人离开酒吧,问酒吧老板是否说谎,如果没有说谎则输出每个人的进出时间点
Input
第一行为两个整数a和b分别表示走私者停留的最少时间和货运者停留的最长时间,第二行为一整数n表示酒吧的进出记录数,之后n行每行两个整数t和state表示t时刻有人离开酒吧(state=1)或者有人进入酒吧(state=0)
Output
如果老板说谎则输出Liar,否则输出No reason,然后输出每个人的进出时刻,如果有多组可行解,输出其中一组即可
Sample Input
6 3
4
1 0
2 0
5 1
10 1
Sample Output
No reason
1 10
2 5
Solution
二分匹配,两排点建图,第一排为state=1的点,第二排为state=0的点,对于任一state=1的点,将其与在第二排和其可能匹配的点(时间差大于a或者小于b)建一条容量为1的边,源点向第一排点建容量为1的边,第二排点向汇点建容量为1的边,然后用Dinic求出最大流判断是否满流,如果不满流则说明老板说谎,如果满流则说明老板没有说谎,此时枚举两排点间所有边的正向弧,如果正向弧流量为0则说明该条边有流经过,那么这条边的起点和终点就为一个人的进出时刻
Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
#define maxn 1111
#define maxm 1111111
#define INF 0x3f3f3f3f
int head[maxn],cur[maxn],d[maxn],st[maxm],s,e,no;
struct point
{
    int u,v,flow,next;
    point(){};
    point(int x,int y,int z,int w):u(x),v(y),next(z),flow(w){};
}p[maxm];
void add(int x,int y,int z)
{
    p[no]=point(x,y,head[x],z); 
    head[x]=no++;
    p[no]=point(y,x,head[y],0); 
    head[y]=no++;
}
void init()
{
    memset(head,-1,sizeof(head));
    no=0;
}
bool bfs()
{
    int i,x,y;
    queue<int>q;
    memset(d,-1,sizeof(d));
    d[s]=0; 
    q.push(s);
    while(!q.empty())
    {
        x=q.front();    
        q.pop();
        for(i=head[x];i!=-1;i=p[i].next)
        {
            if(p[i].flow&& d[y = p[i].v]<0)
            {
                d[y]=d[x]+1;
                if(y==e)    
                    return true;
                q.push(y);
            }
        }
    }
    return false;
}
int dinic()
{
    int i,loc,top,x=s,nowflow,maxflow=0;
    while(bfs()){
        for(i=s;i<=e;i��f�B+)   
            cur[i]=head[i];
        top=0;
        while(true)
        {
            if(x==e)
            {
                nowflow=INF;
                for(i=0;i<top;i++)
                {
                    if(nowflow>p[st[i]].flow)
                    {
                        nowflow=p[st[i]].flow;
                        loc=i;
                    }
                }
                for(i=0;i<top;i++)
                {
                    p[st[i]].flow-=nowflow;
                    p[st[i]^1].flow+=nowflow;
                }
                maxflow+=nowflow;
                top=loc;    
                x=p[st[top]].u;
            }
            for(i=cur[x];i!=-1;i=p[i].next)
                if(p[i].flow&&d[p[i].v]==d[x]+1) 
                    break;
            cur[x]=i;
            if(i!=-1)
            {
                st[top++]=i;
                x=p[i].v;
            }
            else 
            {
                if(!top)    
                    break;
                d[x]=-1;
                x=p[st[--top]].u;
            }
        }
    }
    return maxflow;
}
int pot[maxn];//记录每个点表示的时刻 
int cnt0[maxn],cnt1[maxn],tol1,tol0,a,b,n;//cnt0记录进入酒吧的时刻点,cnt1记录离开酒吧的时刻点,tol0,tol1分别记录两者数量 
int main()
{
    scanf("%d%d%d",&a,&b,&n);
    init();//初始化 
    tol1=tol0=0;
    for(int i=1;i<=n;i++)
    {
        int sta;
        scanf("%d%d",&pot[i],&sta);
        if(sta)//有人离开酒吧 
            cnt1[tol1++]=i;
        else//有人进入酒吧 
            cnt0[tol0++]=i;
    }
    s=0;//源点为0 
    e=tol0+tol1+1;//汇点为tol0+tol1+1 
    int res=0;//res记录两排点间建边的数量 
    for(int i=0;i<tol0;i++)//在两排点中满足条件的两点间建容量为1的边 
        for(int j=0;j<tol1;j++)
        {
            int temp=pot[cnt1[j]]-pot[cnt0[i]];
            if(temp>0&&(temp<=b||temp>=a))
                add(cnt0[i],cnt1[j],1),res++;
        }
    for(int i=0;i<tol0;i++)//源点向第一排点建容量为1的边 
        add(s,cnt0[i],1);
    for(int i=0;i<tol1;i++)//第二排点向汇点建容量为1的边 
        add(cnt1[i],e,1);
    int ans=dinic();//求最大流 
    if(tol0==tol1&&ans==tol1)//满流 
    {
        printf("No reason\n");
        for(int i=0;i<res;i++)
            if(p[2*i].flow==0)//输出正向弧流量为0的边的起点和终点即为某人的进出时刻 
                printf("%d %d\n",pot[p[2*i].u],pot[p[2*i].v]);
    }
    else//不满流则老板说谎 
        printf("Liar\n");
    return 0;
}

你可能感兴趣的:(ACM ICPC 2013-2014 H. Those are not the droids you're looking for(二分匹配-Dinic))