A Plug for UNIX (最大流)

You are in charge of setting up the press room for the inaugural meeting of the United Nations Internet eXecutive (UNIX), which has an international mandate to make the free flow of information and ideas on the Internet as cumbersome and bureaucratic as possible. 
Since the room was designed to accommodate reporters and journalists from around the world, it is equipped with electrical receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed when the room was built. Unfortunately, the room was built many years ago when reporters used very few electric and electronic devices and is equipped with only one receptacle of each type. These days, like everyone else, reporters require many such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling 
irons, tooth brushes, etc. Naturally, many of these devices can operate on batteries, but since the meeting is likely to be long and tedious, you want to be able to plug in as many as you can. 
Before the meeting begins, you gather up all the devices that the reporters would like to use, and attempt to set them up. You notice that some of the devices use plugs for which there is no receptacle. You wonder if these devices are from countries that didn't exist when the room was built. For some receptacles, there are several devices that use the corresponding plug. For other receptacles, there are no devices that use the corresponding plug. 
In order to try to solve the problem you visit a nearby parts supply store. The store sells adapters that allow one type of plug to be used in a different type of outlet. Moreover, adapters are allowed to be plugged into other adapters. The store does not have adapters for all possible combinations of plugs and receptacles, but there is essentially an unlimited supply of the ones they do have.
Input
The input will consist of one case. The first line contains a single positive integer n (1 <= n <= 100) indicating the number of receptacles in the room. The next n lines list the receptacle types found in the room. Each receptacle type consists of a string of at most 24 alphanumeric characters. The next line contains a single positive integer m (1 <= m <= 100) indicating the number of devices you would like to plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which is identical to the type of receptacle it requires). A device name is a string of at most 24 alphanumeric 
characters. No two devices will have exactly the same name. The plug type is separated from the device name by a space. The next line contains a single positive integer k (1 <= k <= 100) indicating the number of different varieties of adapters that are available. Each of the next k lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed by a space, followed by the type of plug.
Output
A line containing a single non-negative integer indicating the smallest number of devices that cannot be plugged in.
Sample Input






laptop B 
phone C 
pager B 
clock B 
comb X 

B X 
X A 
X D 

Sample Output

1


代码:

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int n,np,nc,m;
char x;
const int maxE = 200000;
const int maxN = 800000;
const int maxQ = 200000;
const int MAXN=505;
const int MAXM=5105;
const int oo = 1e+9;
const int inf=0x3f3f3f3f;

struct Edge {
   int v;//弧尾
   int c;//容量
   int n;//指向下一条从同一个弧头出发的弧
} edge[maxE];//边组


int adj[maxN], cntE;//前向星的表头
int Q[maxQ], head, tail;//队列
int d[maxN], cur[maxN], pre[maxN], num[maxN];
int source, sink, nv;//sourse:源点,sink:汇点,nv:编号修改的上限


void add(int u, int v, int c) {//添加边
    //正向边
    edge[cntE].v = v;
    edge[cntE].c = c;//正向弧的容量为c
    edge[cntE].n = adj[u];
    adj[u] = cntE++;

    //反向边
    edge[cntE].v = u;
    edge[cntE].c = 0;//反向弧的容量为0
    edge[cntE].n = adj[v];
    adj[v] = cntE++;
}

void rev_bfs () {//反向BFS标号
    memset(num,0,sizeof(num));
    memset(d,-1,sizeof(d));//没标过号则为-1

    d[sink] = 0;//汇点默认为标过号
    num[0] = 1;
    head = tail = 0;
    Q[tail++] = sink;

    while (head != tail) {
        int u = Q[head++];
        for (int i = adj[u]; ~i; i = edge[i].n) {
            int v = edge[i].v;
            if (~d[v]) continue;//已经标过号
            d[v] = d[u] + 1;//标号
            Q[tail++] = v;
            num[d[v]]++;
        }
    }
}

int ISAP() {
    //copy (cur, adj);//复制,当前弧优化
    memcpy(cur,adj,sizeof(cur));
    rev_bfs ();//只用标号一次就够了,重标号在ISAP主函数中进行就行了
    int flow = 0, u = pre[source] = source, i;

    while (d[sink] < nv) {//最长也就是一条链,其中最大的标号只会是nv - 1,如果大于等于nv了说明中间已经断层了。
        if (u == sink) {//如果已经找到了一条增广路,则沿着增广路修改流量
            int f = oo, neck;
            for (i = source; i != sink; i = edge[cur[i]].v) {
                if (f > edge[cur[i]].c){
                    f = edge[cur[i]].c;//不断更新需要减少的流量
                    neck = i;//记录回退点,目的是为了不用再回到起点重新找
                }
            }
            for (i = source; i != sink; i = edge[cur[i]].v) {//修改流量
                edge[cur[i]].c -= f;
                edge[cur[i] ^ 1].c += f;
            }
            flow += f;//更新
            u = neck;//回退
        }
        for (i = cur[u]; ~i; i = edge[i].n) if (d[edge[i].v] + 1 == d[u] && edge[i].c) break;
        if (~i) {//如果存在可行增广路,更新
            cur[u] = i;//修改当前弧
            pre[edge[i].v] = u;
            u = edge[i].v;
        }
        else {//否则回退,重新找增广路
            if (0 == (--num[d[u]])) break;//GAP间隙优化,如果出现断层,可以知道一定不会再有增广路了
            int mind = nv;
            for (i = adj[u]; ~i; i = edge[i].n) {
                if (edge[i].c && mind > d[edge[i].v]) {//寻找可以增广的最小标号
                    cur[u] = i;//修改当前弧
                    mind = d[edge[i].v];
                }
            }
            d[u] = mind + 1;
            num[d[u]]++;
            u = pre[u];//回退
        }
    }

    return flow;
}
struct  device  //电器
{
    char s1[30],s2[30];
}dd[MAXN];
struct adapter //转换器
{
    char s1[30], s2[30];
}a[MAXN];
struct receptacle  //插座
{
    char s[30];
}r[MAXN];

void init () {//初始化
    memset(adj,-1,sizeof(adj));
    cntE = 0;
}
int main()
{
    scanf("%d",&n);
    for(int i=1; i<=n; i++)
        scanf("%s",r[i].s);

    scanf("%d",&m);
    for(int i=1; i<=m; i++)
        scanf("%s%s",dd[i].s1,dd[i].s2);
    int K;
    scanf("%d",&K);
    for(int i=1; i<=K; i++)
        scanf("%s%s",a[i].s1,a[i].s2);
    init();
    source=0;  sink=n+m+K+1; //源点,汇点
    nv=sink+1;
    for(int i=1; i<=m; i++)  //所有电器
    {
        add(source,i,1);  //源点和电器建边容量为1

        for(int j=1; j<=K ; j++) //所有电器和转换器的连接
            if(!strcmp(dd[i].s2 , a[j].s1)) //电器可以插入转换器中
                add(i,j+m,oo);

        for(int j=1; j<=n; j++)  //电器和插座直接相连
            if(!strcmp(dd[i].s2 , r[j].s))
                add(i,m+K+j,1);
    }

    for(int i=1; i<=K; i++)
    {
        for(int j=1; j<=K; j++) //所有转换器和转换器之间的连接
            if(i!=j && !strcmp(a[i].s2 , a[j].s1))
                //不用考虑相同种类的转换器的连接,因为没有意义
                add(i+m,j+m,oo);

        for(int j=1; j<=n; j++) //所有转换器和所有插座连接
            if(!strcmp(a[i].s2 , r[j].s)) //转换器可插入插座中
                add(i+m , m+K+j , oo);  //转化器和插座相连
    }

    for(int i=1; i<=n; i++)  //插座和汇点相连
        add(m+K+i,sink,1);
        nv=sink+1;

    cout<<m-ISAP()<<endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值