A Plug for UNIX POJ - 1087 最大流 思维建图

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
4 
A 
B 
C 
D 
5 
laptop B 
phone C 
pager B 
clock B 
comb X 
3 
B X 
X A 
X D 
Sample Output
1

两种思路都是用最大流:

难点就是求出floyd算法求出各种电器连接插头的所有情况,也就是传递闭包。

第二种就是把所有的转换接头加入网络流中,然后建立边,容量INF。

之后就是所有电器与超级起点建立边,所有插排与超级终点建立边。

之后从超级起点到终点跑一个dinic,求出最大流,电器数-最大流就是答案。


#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
using namespace std;
const int maxn=400;
struct node
{
    int u,v,next,c;
};
node edge[2500];
struct aa
{
    char str[50];
    char str1[50];
} chazuo[maxn],dianqi[maxn],trans[maxn];
int head[maxn];
int cnt;
int dis[maxn];
int ans,a,b,c;
int f;
int s;
void init()
{
    memset(head,-1,sizeof(head));
    cnt=0;
    memset(dis,-1,sizeof(dis));
    ans=0;
}
void add(int a,int b,int c)
{
    edge[cnt].u=a;
    edge[cnt].v=b;
    edge[cnt].c=c;
    edge[cnt].next=head[a];
    head[a]=cnt++;
}
int bfs()// 给各点分层,离源点的远近分
{
    memset(dis, -1, sizeof(dis));
    queue<int> q;
    dis[s] = 0;
    q.push(s);
    int i;
    int cur;
    while(!q.empty())
    {
        cur = q.front();
        q.pop();
        for(i = head[cur]; i != -1; i = edge[i].next)
        {
            if(dis[edge[i].v] == -1 && edge[i].c > 0)
            {
                dis[edge[i].v] = dis[cur] + 1;
                q.push(edge[i].v);
            }
        }
    }
    if(dis[f] < 0)
        return 0;
    return 1;
}
int Find(int x,int low) //找增广
{
    int a;
    if(x==f) return low;
    for(int i=head[x]; i!=-1; i=edge[i].next)
    {
        int v=edge[i].v;
        if(dis[v]==dis[x]+1 && edge[i].c>0 &&(a=Find(v,min(low,edge[i].c))))
        {
            edge[i].c -=a;
            edge[i^1].c +=a;
           // printf("%d %d\n",x,v);
            return a;
        }
    }
    return 0;
}
void dinic()
{
    int temp;
    while(bfs())
    {
        temp=Find(s,0x3f3f3f3f);
        ans+=temp;
    }
    printf("%d\n",b-ans);
}
int main()
{

    init();
    char str[50];
    scanf("%d",&a);//节点规划:1-a是插排,a+1-a+b是电器,a+b+1-a+b+c为转换器
    for(int i=1; i<=a; i++)
    {
        scanf("%s",chazuo[i].str);
    }
    scanf("%d",&b);
    for(int i=1; i<=b; i++)
    {
        scanf("%s",str);
        scanf("%s",dianqi[i].str);
    }
    scanf("%d",&c);
    for(int i=1; i<=c; i++)//str1->str2
    {
        scanf("%s%s",trans[i].str,trans[i].str1);
    }
    s=0,f=a+b+c+1;
    for(int i=1; i<=a; i++)//插排与汇点
    {
        add(i,f,1);
        add(f,i,0);
    }
    for(int i=1; i<=b; i++)
    {
        add(s,a+i,1);
        add(a+i,s,0);
        for(int j=1; j<=a; j++)
        {
            if(strcmp(dianqi[i].str,chazuo[j].str)==0)
            {
                add(a+i,j,1);
                add(j,a+i,0);
            }
        }
        for(int j=1; j<=c; j++)
        {
            if(strcmp(dianqi[i].str,trans[j].str)==0)
            {
                add(a+i,a+b+j,0x3f3f3f3f);
                add(j+b+a,a+i,0);

            }
        }
    }
    for(int i=1; i<=c; i++)
    {
        for(int j=1; j<=c; j++)
        {
            if(i!=j)
            {
                if(strcmp(trans[i].str1,trans[j].str)==0)
                {
                    add(a+b+i,a+b+j,0x3f3f3f3f);
                    add(a+b+j,a+b+i,0);
                    //printf("%s %s->%s %s\n",trans[i].str,trans[i].str1,trans[j].str,trans[j].str1);
                }
            }
        }
        for(int j=1; j<=a; j++)
        {
            if(strcmp(trans[i].str1,chazuo[j].str)==0)
            {
                add(a+b+i,j,0x3f3f3f3f);
                add(j,a+b+i,0);
            }
        }
    }
    dinic();
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值