SCOJ 4429: frog's dice 最大流

4429: frog's dice

题目连接:

http://acm.scu.edu.cn/soj/problem.action?id=4429

Description

frog has many dices:)
Each dice has six surfaces and there is a lowercase letter on each surfaces.
Now, frog want to put these dices in a row so that the letters on the upper faces could form a certain word.
She can put these dices in any order but each dice can only be used once.
Please tell frog whether she could make that thing happen.?

Input

The first line of input is the number of test case.
Then for each case:
The first line is a number n(1 <= n <= 1000), indicating the number of dices.
Then there are n lines, each line has six lowercase letters(separated by space), indicating the letters on a dice.
The last line has n lowercase letters, indicating frog's special word.

Constraints:
For each case, print one line:
If frog can get her special word, print "Cong, frog!"(without quotation);
else print "Sorry, frog."

Output

For each case output the answer modulo 1000000007 in a single line.

Sample Input

2
3
s c u a c m
a b c d e f
a b c d e f
scu
4
a e c f a c
d z b g f a
p r j a a a
h e t g o a
frog

Sample Output

Sorry, frog.
Cong, frog!

Hint

题意

你有n个骰子,然后每个骰子的每一面都有一个字母

然后给你一个长度为n的字符串,问你这个字符串时候可以由这些骰子朝上的面摆出来

题解:

最大流

左边表示那26个字符,右边表示骰子,如果这个骰子存在这个字符,那么就建一条边

然后再看看是否满流就好了

注意,不要建成1000*1000的了

代码

#include<bits/stdc++.h>
using namespace std;
namespace NetFlow
{
    const int MAXN=100500,MAXM=305000,inf=1e9;
    struct Edge
    {
        int v,c,f,nx;
        Edge() {}
        Edge(int v,int c,int f,int nx):v(v),c(c),f(f),nx(nx) {}
    } E[MAXM];
    int G[MAXN],cur[MAXN],pre[MAXN],dis[MAXN],gap[MAXN],N,sz;
    void init(int _n)
    {
        N=_n,sz=0; memset(G,-1,sizeof(G[0])*N);
    }
    void link(int u,int v,int c)
    {
        E[sz]=Edge(v,c,0,G[u]); G[u]=sz++;
        E[sz]=Edge(u,0,0,G[v]); G[v]=sz++;
    }
    int ISAP(int S,int T)
    {//S -> T
        int maxflow=0,aug=inf,flag=false,u,v;
        for (int i=0;i<N;++i)cur[i]=G[i],gap[i]=dis[i]=0;
        for (gap[S]=N,u=pre[S]=S;dis[S]<N;flag=false)
        {
            for (int &it=cur[u];~it;it=E[it].nx)
            {
                if (E[it].c>E[it].f&&dis[u]==dis[v=E[it].v]+1)
                {
                    if (aug>E[it].c-E[it].f) aug=E[it].c-E[it].f;
                    pre[v]=u,u=v; flag=true;
                    if (u==T)
                    {
                        for (maxflow+=aug;u!=S;)
                        {
                            E[cur[u=pre[u]]].f+=aug;
                            E[cur[u]^1].f-=aug;
                        }
                        aug=inf;
                    }
                    break;
                }
            }
            if (flag) continue;
            int mx=N;
            for (int it=G[u];~it;it=E[it].nx)
            {
                if (E[it].c>E[it].f&&dis[E[it].v]<mx)
                {
                    mx=dis[E[it].v]; cur[u]=it;
                }
            }
            if ((--gap[dis[u]])==0) break;
            ++gap[dis[u]=mx+1]; u=pre[u];
        }
        return maxflow;
    }
    bool bfs(int S,int T)
    {
        static int Q[MAXN]; memset(dis,-1,sizeof(dis[0])*N);
        dis[S]=0; Q[0]=S;
        for (int h=0,t=1,u,v,it;h<t;++h)
        {
            for (u=Q[h],it=G[u];~it;it=E[it].nx)
            {
                if (dis[v=E[it].v]==-1&&E[it].c>E[it].f)
                {
                    dis[v]=dis[u]+1; Q[t++]=v;
                }
            }
        }
        return dis[T]!=-1;
    }
    int dfs(int u,int T,int low)
    {
        if (u==T) return low;
        int ret=0,tmp,v;
        for (int &it=cur[u];~it&&ret<low;it=E[it].nx)
        {
            if (dis[v=E[it].v]==dis[u]+1&&E[it].c>E[it].f)
            {
                if (tmp=dfs(v,T,min(low-ret,E[it].c-E[it].f)))
                {
                    ret+=tmp; E[it].f+=tmp; E[it^1].f-=tmp;
                }
            }
        }
        if (!ret) dis[u]=-1; return ret;
    }
    int dinic(int S,int T)
    {
        int maxflow=0,tmp;
        while (bfs(S,T))
        {
            memcpy(cur,G,sizeof(G[0])*N);
            while (tmp=dfs(S,T,inf)) maxflow+=tmp;
        }
        return maxflow;
    }
}
using namespace NetFlow;
char a[1200][6];
char ttt;
string ss;
int p[30];
void solve()
{
    memset(p,0,sizeof(p));
    init(50000);
    int s = 0;
    int t = 5000;
    int n;
    scanf("%d",&n);getchar();
    for(int i=1;i<=n;i++)
        for(int j=0;j<6;j++)
        {
            a[i][j]=getchar();
            getchar();
        }
    cin>>ss;
    for(int i=0;i<ss.size();i++)
        p[ss[i]-'a']++;
    for(int i=0;i<26;i++)
    {
        link(s,i+1,p[i]);
        for(int j=1;j<=n;j++)
        {
            for(int k=0;k<6;k++)
            {
                if((int)(a[j][k]-'a')==i)
                {
                    link(i+1,j+30,1);
                    break;
                }
            }
        }
    }
    for(int j=1;j<=1000;j++)
    {
        link(j+30,t,1);
    }
    int ans = dinic(s,t);
    if(ans==n)printf("Cong, frog!\n");
    else printf("Sorry, frog.\n");
}
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
        solve();
    return 0;
}
posted @ 2016-04-16 17:22  qscqesze  阅读(352)  评论(0编辑  收藏  举报