POJ1087A Plug for UNIX (Conference Room outlet) -- maximum stream

Source: Internet
Author: User
Tags alphanumeric characters

POJ1087A Plug for UNIX (Conference Room outlet) -- maximum stream


Now you are responsible for setting up the meeting room for the Internet Union's CEO to hold the press conference.
Since the conference room was designed to accommodate journalists around the world during construction, the conference room provides a variety of power outlets
To meet the different plug types and voltages in different countries (meeting room construction period. Unfortunately, the meeting room was built many years ago,
At that time, journalists seldom used electronic devices, so the conference room provided only one socket for each type. Press conference, news
Users need to use many electronic devices, such as laptops, microphones, recorders, pagers, and so on. Although many of these devices can
Battery, but since the press conference is long and monotonous, reporters want to use as many devices as possible (
Some devices need to use sockets) to pass the time.
Before the press conference, you collected information about the devices used by reporters and began to arrange the meeting room. You noticed that some devices
No suitable plug available. You suspect that these devices come from countries that do not exist when building meeting rooms. For some sockets
There are multiple device plug available for use. For other sockets, no device plug can be used.
To try to solve this problem, you patronize a nearby store that sells converters that can be inserted
The header is converted into another plug. In addition, the converter can be connected in series. The store does not have enough converter types to meet all the plug and
A combination of sockets, but for an existing converter, there can always be an unlimited number.

Input description:
The input file contains multiple test data. The first act of the input file is an integer N, followed by N after an empty row.
Input block. Each input block corresponds to a test data. Input blocks are separated by empty lines. The format of each input block is:
The first behavior of each input block is a positive integer n, 1 ≤ n ≤ 100, indicating the number of sockets provided by the meeting room; next n
The n sockets provided by the meeting room are listed in the rows. Each socket is described in a alphanumeric string (up to 24 characters );
The next row is a positive integer m, 1 ≤ m ≤ 100, indicating the number of devices to be inserted. In the next m row
The device name is followed by the plug name used by the device. The plug name is the same as the name of the socket used by the device;
The device name is a string containing up to 24 alphanumeric characters. The names of any two devices are different. The device name
And the plug are separated by spaces;
The next row is a positive integer k, 1 ≤ k ≤ 100, indicating the number of converter types that can be used; the next k rows, each line
A converter is described as follows: the first is the socket type provided by the converter, the middle is a space, and then the plug type.

Output description:
Output a non-negative integer for each test data in the input file, indicating how many devices cannot be inserted.

There are at most 200 sockets and 100 devices. From the source to each device, create an edge with a cap of 1. from each outlet to the sink, create an edge with a cap of 1. from each device, create an edge with a cap of 1 from the matching outlet, switch between sockets to create an edge with the cap as INF, and find the maximum flow

ISAP Memory: 336 K Time: 32 MS

        const int maxn=400;const int INF=0x3f3f3f3f;using namespace std;int n,s,t;struct Edge{ int from,to,cap,flow; Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}};vector
           G[maxn];int gap[maxn],d[maxn],cur[maxn],p[maxn];inline void addedge(int u,int v,int c){ edges.push_back(Edge(u,v,c,0)); edges.push_back(Edge(v,u,0,0)); int m=edges.size(); G[u].push_back(m-2); G[v].push_back(m-1);}int ISAP(){ memset(cur,0,sizeof(cur)); memset(d,0,sizeof(d)); memset(gap,0,sizeof(gap)); int x=s,flow=0,a=INF; while(d[s]
           e.flow&&d[e.to]+1==d[x]){ p[e.to]=G[x][i]; cur[x]=i; x=e.to; ok=1; a=min(a,e.cap-e.flow); break; } } if(!ok){ int m=n; for(int i=0;i
            e.flow) m=min(m,d[e.to]); } if(--gap[d[x]]==0) break; gap[d[x]=m+1]++; cur[x]=0; if(x!=s) x=edges[p[x]].from; } } return flow;}map
               dev;int N,M,K,cnt,cmt;void Init(){ cnt=0,cmt=200; s=302,t=303; cin>>N; for(int i=0;i
               >s; if(!rec.count(s)) rec[s]=++cnt; addedge(rec[s],t,1); } cin>>M; for(int i=0;i
                >s1>>s2; dev[s1]=++cmt; if(!rec.count(s2)) rec[s2]=++cnt; addedge(dev[s1],rec[s2],1); addedge(s,dev[s1],1); } cin>>K; for(int i=0;i
                 >s1>>s2; if(!rec.count(s1)) rec[s1]=++cnt; if(!rec.count(s2)) rec[s2]=++cnt; addedge(rec[s1],rec[s2],INF); } n=cnt+cmt-200+2;}int main(){ Init(); printf(%d,M-ISAP()); return 0;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.