POJ-1087 A Plug for UNIX (网络流)

思路

电器数1 ~ 100,附带100种接口,注意题目:You notice that some of the devices use plugs for which there is no receptacle.

墙上接口1 ~ 100,还有1 ~ 100转接口,转接口有两个接口。假设所有的接口类型都不相同,接口就有400种。所以汇点值 >= 501。

读入电器的时候,它的接口可能在墙上并没有,所以要判断在此之前是否已经给该接口分配过标号。

读入转接口的时候,先读入两个转接口,然后分别判断是否已经分配过标号,最后在 转接口入口 -> 转接口出口 ,这两个接口之间建立一条INF的边。

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

const int INF=0x3f3f3f3f;
const int maxn=110;
map<string,int>rec,dev;
int dev_cnt=1,rec_cnt=101;
int n,m,k;

struct Edge {
    int to,cap,flow;
};

struct Dinic {
    int cnt,s,t;
    int head[6*maxn];
    int deep[6*maxn];
    int vis[6*maxn];
    int cur[6*maxn];
    int next[maxn*maxn*4];
    Edge edge[maxn*maxn*4];

    void addEdge(int u,int v,int w)
    {
        edge[cnt].to=v;
        edge[cnt].cap=w;
        edge[cnt].flow=0;
        next[cnt]=head[u];
        head[u]=cnt++;

        edge[cnt].to=u;
        edge[cnt].cap=0;
        edge[cnt].flow=0;
        next[cnt]=head[v];
        head[v]=cnt++;
    }

    void init()
    {
        memset(head,-1,sizeof(head));
        cnt=0;
        s=0;
        t=601;
    }

    bool bfs()
    {
        memset(vis,0,sizeof(vis));
        memset(deep,0,sizeof(deep));
        deep[s]=0;
        vis[s]=1;
        queue<int> q;
        q.push(s);
        while (!q.empty()) {
            int u=q.front();
            q.pop();
            for (int i=head[u];i!=-1;i=next[i]) {
                Edge &e=edge[i];
                if (!vis[e.to]&&e.cap>e.flow) {
                    vis[e.to]=1;
                    deep[e.to]=deep[u]+1;
                    q.push(e.to);
                }
            }
        }
        return vis[t];
    }

    int dfs(int u,int in)
    {
        if (in==0||u==t) {
            return in;
        }
        int f=0,out=0;
        for (int &i=cur[u];i!=-1;i=next[i]) {
            Edge &e=edge[i];
            if (deep[e.to]==deep[u]+1&&(f=dfs(e.to,min(in,e.cap-e.flow)))>0) {
                edge[i].flow+=f;
                edge[i^1].flow-=f;
                in-=f;
                out+=f;
                if (in==0) {
                    break;
                }
            }
        }
        return out;
    }

    int maxflow()
    {
        int ans=0;
        while (bfs()) {
            for (int i=0;i<6*maxn;i++) {
                cur[i]=head[i];
            }
            ans+=dfs(s,INF);
        }
        return ans;
    }

}DC;

int main()
{
    DC.init();
    cin>>n;
    string tmp;
    for (int i=0;i<n;i++) {
        cin>>tmp;
        rec[tmp]=rec_cnt;
        DC.addEdge(rec_cnt,601,1);
        rec_cnt++;
    }
    cin>>m;
    for (int i=0;i<m;i++) {
        cin>>tmp;
        dev[tmp]=dev_cnt;
        DC.addEdge(0,dev_cnt,1);
        cin>>tmp;
        if (rec[tmp]==0) {
            rec[tmp]=rec_cnt++;
        }
        DC.addEdge(dev_cnt,rec[tmp],1);
        dev_cnt++;
    }
    cin>>k;
    string in,out;
    for (int i=0;i<k;i++) {
        cin>>in;
        cin>>out;
        if (rec[in]==0) {
            rec[in]=rec_cnt++;
        }
        if (rec[out]==0) {
            rec[out]=rec_cnt++;
        }
        DC.addEdge(rec[in],rec[out],INF);
    }
    printf("%d\n",dev_cnt-1-DC.maxflow());
    return 0;
}

posted @ 2019-08-15 17:12  xyee  阅读(111)  评论(0编辑  收藏  举报