POJ 1087 A Plug for UNIX

#include<iostream>
#include<map>
#include<string>
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;

const int INF=0x7fffffff;
int cap[503][503],a[503],flow[503][503],p[503];
int ek(int s,int t,int n)
{

       memset(flow,0,sizeof(flow));
        int f=0;
        while(1)
        {
            queue<int> q;
            memset(a,0,sizeof(a));
            //memset(p,0,sizeof(p));
           a[s]=INF;
            q.push(s);
            while(!q.empty())
            {
                int u=q.front();
                q.pop();
                for(int v=1;v<n;v++)
                if(!a[v]&&cap[u][v]>flow[u][v])
                {
                    p[v]=u;
                    q.push(v);
                    a[v]=a[u]<cap[u][v]-flow[u][v]?a[u]:cap[u][v]-flow[u][v];
                }
            }
            if(a[t]==0)
            break;
            for(int u=t;u!=s;u=p[u])
            {
                flow[p[u]][u]+=a[t];
               flow[u][p[u]]-=a[t];
            }
            f+=a[t];
        }
        return f;
}


map<string,int> mp;
int main()
{
    string str1,str2;
    int s=1,t=2,num=3;
    memset(cap,0,sizeof(cap));
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>str1;
        mp[str1]=num;
        cap[num++][t]=1;
    }
    int m;
   cin>>m;
   for(int i=1;i<=m;i++)
   {
       cin>>str1;
       mp[str1]=num;
       cap[s][num++]=1;

       cin>>str2;
       if(!mp[str2])
       mp[str2]=num++;

       cap[mp[str1]][mp[str2]]=INF;
   }

   int k;
   cin>>k;
   for(int i=1;i<=k;i++)
   {

       cin>>str1>>str2;
       if(!mp[str1])
       mp[str1]=num++;

       if(!mp[str2])
       mp[str2]=num++;

       cap[mp[str1]][mp[str2]]=1;
   }

   printf("%d",m-ek(s,t,num));
    return 0;
}

  

posted @ 2011-07-23 20:42  Crazy_yiner  阅读(176)  评论(0编辑  收藏  举报