C - A Plug for UNIX (Network Flow)

Keywords: socket Unix network

C - A Plug for UNIXPOJ - 1087

Title:

Give you some plugs and sockets. Plug and socket have different models, only the same model can match. Now there are k converters, each converter has infinite, but not necessarily all converter models. The function of the converter is to convert one plug into another plug. Now please at least how many plugs can't be plugged into the socket.

Analysis:

As long as the maximum number of plugs can be plugged into the socket, then we can use the network flow algorithm.

  1. Let source point to all plugs with a weight of 1.
  2. Let all sockets point to the confluence End with a weight of 1.
  3. Then according to all plugs and sockets and converter set out all plug types.
  4. Let the plug point to the corresponding plug type with a weight of 1.
  5. Let the plug type point to the corresponding socket, the weight is 1.
  6. According to the converter, let the plug type point to the plug type, and the weight is inf.

Then find the maximum flow from source to End.

** Providing a set of data**

Input:
2
A
A
2
laptop C
comb D
3
C X
X A
D X
//Output:
0

Code:

#include<vector>
#include<queue>
#include<cstdio>
#include<set>
#include<cstring>
#include<string>
#include<algorithm>
#include<iostream>
#define mset(a,b)   memset(a,b,sizeof(a))
using namespace std;
const int maxn=710;
const int inf=0x3f3f3f3f;
string seat[110],head[110],Convert[300][2];
int htot,stot,ztot;//Plug-socket converter
vector<string>headCla;
class EK
{
public:
    vector<int> adja[maxn];//
    int cap[maxn][maxn];
    int dis[maxn],pre[maxn],tot;
    void init(int n)
    {
        for(int i=0; i<n; ++i)
            adja[i].clear();
        mset(cap,0);
        tot=n;
    }
    void addEdge(int s,int t,int f)
    {
        cap[s][t]=f;
        cap[t][s]=0;
        adja[s].push_back(t);
        adja[t].push_back(s);
    }
    void bfs(int s,int t)//Broad search for an augmented path
    {
        mset(dis,-1);
        queue<int>mmp;
        mmp.push(s);
        dis[s]=s;
        while(!mmp.empty())
        {
            int u=mmp.front();
            mmp.pop();
            for(int i=0; i<adja[u].size(); ++i)
            {
                int v=adja[u][i];
                if(dis[v]==-1&&cap[u][v]>0)
                {
                    dis[v]=dis[u]+1;
                    pre[v]=u;
                    mmp.push(v);
                }
            }
        }
    }
    int maxFlow(int s,int t)
    {
        int flow=0;
        for(;;)
        {
            bfs(s,t);
            if(dis[t]==-1)
                return flow;
            int last=t,minn=inf;
            while(last!=pre[last])
            {
                minn=min(minn,cap[pre[last]][last]);
                last=pre[last];
            }
            last=t;
            while(last!=pre[last])
            {
                cap[pre[last]][last]-=minn;
                cap[last][pre[last]]+=minn;
                last=pre[last];
            }
            flow+=minn;
        }
    }
};
set<string> Unique;
EK kit;
int main()
{
    int source=0,Endd;
    string name;
    while(~scanf("%d",&stot))
    {
        Unique.clear();
        for(int i=0; i<stot; ++i){
         cin>>seat[i];
         Unique.insert(seat[i]);
        }
        scanf("%d",&htot);
        for(int i=0; i<htot; ++i){
         cin>>name>>head[i];
         Unique.insert(head[i]);
        }
        /*Input sockets and plugs*/
        sort(seat,seat+stot);
        sort(head,head+htot);
        scanf("%d",&ztot);
        for(int i=0; i<ztot; ++i)
        {
            cin>>Convert[i][0]>>Convert[i][1];
            Unique.insert(Convert[i][0]);
            Unique.insert(Convert[i][1]);
        }
        headCla.clear();
        for(set<string>::iterator it=Unique.begin();it!=Unique.end();++it)   headCla.push_back(*it);
        kit.init(stot+htot+headCla.size()+2);//Initialization
        Endd=stot+htot+headCla.size()+1;
        /*Plug is connected to plug type*/
        int TotHeadCla=htot+stot;
        int TotHead=stot,Totseat=0;
        for(int i=0; i<htot; ++i)
        {
            int th=lower_bound(headCla.begin(),headCla.end(),head[i])-headCla.begin();
            kit.addEdge(TotHead+i+1,TotHeadCla+th+1,1);
        }
        /*Plug type is connected to socket*/
        for(int i=0; i<stot; ++i)
        {
            int th=lower_bound(headCla.begin(),headCla.end(),seat[i])-headCla.begin();
            if(th==headCla.size())
                continue;
            kit.addEdge(TotHeadCla+th+1,Totseat+i+1,1);
        }
        /*The plug is connected to the plug by a converter.*/
        for(int i=0;i<ztot;++i){
            string a=Convert[i][0];
            string b=Convert[i][1];
            int ath,bth;
            ath=lower_bound(headCla.begin(),headCla.end(),a)-headCla.begin();
            bth=lower_bound(headCla.begin(),headCla.end(),b)-headCla.begin();
            if(ath==bth||ath==headCla.size()||bth==headCla.size())
                continue;
            kit.addEdge(TotHeadCla+ath+1,TotHeadCla+bth+1,inf);
        }
        for(int i=1; i<=htot; ++i) //Opposite the plug
            kit.addEdge(source,TotHead+i,1);
        for(int i=1; i<=stot; ++i)
            kit.addEdge(Totseat+i,Endd,1);
        int ans=kit.maxFlow(source,Endd);
        printf("%d\n",htot-ans);
    }
    return 0;
}

Posted by Anti-Moronic on Sun, 17 Mar 2019 04:03:26 -0700