【BZOJ 3504 】[Cqoi2014]危桥

好像 无向图并没有什么处理...
没用邻接表 慢了很多.....

/**************************************************************
    Problem: 3504
    User: sxb_201
    Language: C++
    Result: Accepted
    Time:1136 ms
    Memory:3616 kb
****************************************************************/
 
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
 
#define MAXN 25000
#define INF 100000000
int s,t;
int n,a1,a2,an,b1,b2,bn;
int a[500][500],b[500][500];
int team[MAXN*4],head,tail;
int d[500];
bool bfs(int a[][500])
{
    head=tail=0;memset(d,0,sizeof(d));
    d[s]=1;team[++tail]=s;
    while(head<tail)
    {
        int x=team[++head];
        for(int i=1;i<=n;i++)
            if(d[i]==0&&a[x][i])
                d[i]=d[x]+1,team[++tail]=i;
    }
    if(d[t]==0) return false;
    else        return true;
}
int dfs(int x,int mmin,int a[][500])
{
    int tmp;if(x==t) return mmin;
    int flow=0;
    for(int i=1;i<=n;i++)
        if(d[i]==d[x]+1&&(tmp=dfs(i,min(mmin,a[x][i]),a)))
        {
            a[x][i]-=tmp,a[i][x]+=tmp,mmin-=tmp,flow+=tmp;
            if(mmin==0) return flow;
        }
    return flow;
}
bool dinic(int a[][500])
{
    int ans=0;
    while(bfs(a)) ans+=dfs(s,INF,a);
    if(ans==an+bn) return true;
    else        return false;
}
int main()
{
    while(scanf("%d %d %d %d %d %d %d",&n,&a1,&a2,&an,&b1,&b2,&bn)==7)
    {
        memset(a,0,sizeof(a));
        memset(b,0,sizeof(b));
        char c=getchar();
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                c=getchar();
                if(c=='X')
                    ;
                else if(c=='O')
                    a[i][j]=2,b[i][j]=2;
                else
                    a[i][j]=INF,b[i][j]=INF;
            }
            c=getchar();
        }
        a1++,a2++,b1++,b2++;
        s=++n,t=++n;
        an*=2,bn*=2;
        a[s][a1]=an;a[s][b1]=bn;
        a[a2][t]=INF;a[b2][t]=INF;
        if(dinic(a)==false) {printf("%s\n","No");continue;}
        b[s][a1]=an;b[s][b2]=bn;
        b[a2][t]=INF;b[b1][t]=INF;
        if(dinic(b)==false) {printf("%s\n","No");continue;}
        printf("%s\n","Yes");
    }
    return 0;
}

posted @ 2016-01-06 19:21  sxb_201  阅读(160)  评论(0编辑  收藏  举报