POJ 1087 A Plug for UNIX (network stream, dinic build)

Source: Internet
Author: User
Tags socket alphanumeric characters

A Plug for UNIX

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 17529 Accepted: 6055

Description charge of setting up the Press hostel for the inaugural meeting of the All Nations Internet execut Ive (UNIX), which has a international mandate to make the free flow of information and ideas on the Internet as Cumbersom E and bureaucratic as possible.
Since the designed to accommodate reporters and journalists from around the world, it's equipped with electrical Receptacles to suit the different shapes of plugs and voltages used by appliances in all of the countries that existed wh En the hostel was built. Unfortunately, the built many years ago when reporters used very few electric and electronic devices and is equip PED 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 was likely to being long and tedious, you wan T to is able to plug in as many as can.
Before the meeting begins, you gather up all the devices so the reporters would like-to-use, and attempt-to-set them up. You notice this some of the devices use plugs for which there are no receptacle. You wonder if these devices is from countries this didn ' t exist when the the hostel was built. For some receptacles, there is several devices that use the corresponding plug. For other receptacles, there is 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 is allowed to being plugged into other adapters. The store does not has adapters for all possible combinations of plugs and receptacles, but there was essentially an unlim ited supply of the ones they do has.

Input the input would consist of one case. The first line contains a, positive integer n (1 <= n <=) indicating the number of receptacles in the . The next n lines list the receptacle types found in the. Each receptacle type consists of a string of in most alphanumeric characters. The next line contains a single positive integer m (1 <= m <=) indicating the number of devices your would like T o plug in. Each of the next m lines lists the name of a device followed by the type of plug it uses (which are identical to the type O F receptacle it requires). A device name is a string of in most alphanumeric
Characters. No. Devices would 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 <=) indicating the number of different varieties of a Dapters that is available. Each of the next K lines describes a variety of adapter, giving the type of receptacle provided by the adapter, followed B Y 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 being 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 

Sample Output

1


This problem, built for two days, MMP is going crazy.

Test instructions: N sockets, M electrical Appliances K converters, ask there are at least a few appliances not connected, ask for the maximum network flow and then M-maxflow


Major issues on the map, because there is a converter and the converter is unrestricted;

Note: 1, converter conversion may exist in the socket not,

2, there is a duplicate electrical plug, so to check the reconstruction diagram,

3. Manually establish source and sink points


Similar to a diagram like this:



Apply Dinic Template


#include <iostream> #include <stdio.h> #include <algorithm> #include <math.h> #include < cmath> #include <cstring> #include <string> #include <queue> #include <stack> #include < stdlib.h> #include <list> #include <map> #include <set> #include <bitset> #include <vector > #define MEM (A, B) memset (A,b,sizeof (a)) #define FINDX (x) lower_bound (b+1,b+1+bn,x)-B #define FIN freopen ("input.t XT "," R ", stdin) #define FOUT freopen (" Output.txt "," w ", stdout) #define S1 (n) scanf ("%d ", &n) #define SL1 (n) scan F ("%i64d", &n) #define S2 (n,m) scanf ("%d%d", &n,&m) #define SL2 (n,m) scanf ("%i64d%i64d", &n,&m) #
Define PR (N) printf ("%d\n", N) using namespace std;

typedef long Long LL;
const int inf=0x3f3f3f3f;
const double esp=1e-6;
const int maxn=1e6+5;
const int mod=1e9+7;
const int mod=1e9+7;


int dir[5][2]={0,1,0,-1,1,0,-1,0}; struct node{int v,w,next;//u v from u-v weight to W}edge[maxn];
int head[maxn],num[maxn],start,end,cnt,sum;
int n,m,k;
    void Add (int u,int v,int W) {edge[cnt].v=v;
    Edge[cnt].w=w;
    Edge[cnt].next=head[u];
    head[u]=cnt++;
    Edge[cnt].v=u;
    edge[cnt].w=0;
    EDGE[CNT].NEXT=HEAD[V];
head[v]=cnt++;
    } int BFs () {queue<int>q;
    MEM (num,0);
    Num[start]=1;
    Q.push (start); while (!
        Q.empty ()) {int T=q.front ();
        Q.pop ();
        if (t==end) return 1; for (int i=head[t];i!=-1;i=edge[i].next)//chain forward star Access find augmented path {int t1= edge[i].v;//next node int t2= edge[i].w;//Current point weights if (t2&&num[t1]==0)//current point exists and the next point does not have access {Num[t1]=num[t]
                +1;//Point =1 if (t1==end)//end return 1;
            Q.push (t1);
}}} return 0;
    } int dfs (int u,int maxflow) {if (u==end) return maxflow;

    int res=0;
     for (int i=head[u];i!=-1;i=edge[i].next) {   int t1=edge[i].v;//next node int t2=edge[i].w;//current node if (t2&&num[t1]==num[u]+1) { int Temp=dfs (T1,min (MAXFLOW-RES,T2));//Select a small portion of the stream edge[i].w-=temp;//positive reduction edge[i^1].w+=temp;/
            /reverse increase res+=temp;

        if (maxflow==res) return res;
    }} if (!res) num[u]=-1;
return res;
    } void Dinic () {int ans=0;
    while (BFS ()) {Ans+=dfs (start,inf);
} cout<<m-ans<<endl;
} map<string,int>maps;

Char sp[200],plug[200];
        int main () {while (~S1 (n)) {mem (head,-1);
        cnt=0;
        Maps.clear ();
        start=0,end=1000;

        int num=1;
            for (int i=1;i<=n;i++) {scanf ("%s", SP);
            maps[sp]=num++;
        Add (start,maps[sp],1);//socket to Source point} S1 (M);
            for (int i=1;i<=m;i++) {scanf ("%s%s", Sp,plug);
     maps[sp]=num++;       if (!maps[plug])//no maps[plug]=num++;
            Add (maps[plug],maps[sp],1);
        Add (maps[sp],end,1);
        } S1 (k);
            for (int i=1;i<=k;i++) {scanf ("%s%s", Sp,plug);
            if (!MAPS[SP]) maps[sp]=num++;
            if (!maps[plug]) maps[plug]=num++;
        Add (Maps[plug],maps[sp],inf);
    } dinic ();
} return 0;
 }


123

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.