Poj 1087 a plug for Unix conference room outlet problem diagram + maximum flow

Source: Internet
Author: User
Tags strcmp alphanumeric characters

Link: poj 1087 a plug for Unix

A plug for Unix
Time limit:1000 ms   Memory limit:65536 K
Total submissions:13809   Accepted:4623

Description

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 appliances tacles 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 policyears ago when reporters used very few electric and electronic devices and is equipped with only one primary tacle of each type. these days, like everyone else, reporters require login such devices to do their jobs: laptops, cell phones, tape recorders, pagers, coffee pots, microwave ovens, blow dryers, curling
Irons, tooth brushes, etc. naturally, thanks 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 ready as you can.
Before the meeting begins, you gather up all the devices that the reporters wocould like to use, and attempt to set them up. you notice that some of the devices use plugs for which there is no such tacle. you wonder if these devices are from countries that didn't exist when the room was built. for some sort tacles, there are several devices that use the corresponding plug. for other extends tacles, 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 essential 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 specified tacles in the room. the next n lines list the specified tacle types found in the room. each role tacle 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 wowould 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 each tacle 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 role tacle 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

Source

East central North America 1999

Analysis:

After finding that the basic diagram is complete, the questions about the network flow can be easily solved.

(1) Take 0 as the source point, 1 as the sink point, and other sockets and equipment are used as the center point

(2) The meeting room provides N sockets and connects one edge from the source point to each socket. The capacity is 1.

(3) The meeting room has m devices and connects one edge from each device to the sink point. The capacity is 1.

(4) Each device uses one socket and connects one edge from the corresponding socket to the device. The capacity is 1.

(5) There is a K adapter, from the plug to the adapter provides a socket type to connect an edge, that is, the former can be converted to the latter, the capacity is infinite, because it can be connected in series.

(6) calculate the maximum flow from the source point to the sink point and the maximum number of devices used maxflow. The final result is m-maxflow.

Code;

Dinic:

# Include <iostream> # include <cstdio> # include <cstring> # include <string> # include <vector> # include <queue> using namespace STD; # define maxn 1010 # define INF 0x3f3f3fstruct edge {int from, to, Cap ;}; vector <edge> EG; vector <int> G [maxn]; int N, S, t, d [maxn], cur [maxn], MP [maxn] [maxn]; char name [maxn] [30]; int CNT; void addedge (int from, int to, int cap) {eg. push_back (edge) {from, to, Cap}); eg. push_back (E DGE) {to, from, 0}); int x = eg. size (); G [from]. push_back (X-2); G [to]. push_back (x-1);} bool BFS () {memset (D,-1, sizeof (d); queue <int> q; q. push (s); D [s] = 0; while (! Q. empty () {int x = Q. front (); q. pop (); For (INT I = 0; I <G [X]. size (); I ++) {edge & E = eg [G [x] [I]; If (d [E. to] =-1 & E. cap> 0) {d [E. to] = d [x] + 1; q. push (E. to) ;}} return (d [T]! =-1);} int DFS (int x, int A) {If (x = T | A = 0) return a; int flow = 0, F; for (Int & I = cur [X]; I <G [X]. size (); I ++) {edge & E = eg [G [x] [I]; If (d [x] + 1 = d [E. to] & (F = DFS (E. to, min (A, E. CAP)> 0) {e. cap-= f; eg [G [x] [I] ^ 1]. cap + = f; flow + = f; A-= f; if (a = 0) break;} return flow;} int dinic () {int ans = 0; while (BFS () {memset (cur, 0, sizeof (cur); ans + = DFS (S, INF);} eg. clear (); For (INT I = 0; I <n; ++ I) g [I]. clear (); Return ans;} int find (char * Str) {int I; for (I = 2; I <CNT; ++ I) if (strcmp (name [I], STR) = 0) return I; strcpy (name [I], STR); CNT ++; return I;} int main () {// freopen ("poj_00007.txt", "r", stdin); int M, K; char str1 [30], str2 [30]; while (~ Scanf ("% d", & N) {S = 0, T = 1; CNT = 2; // two for (INT I = 0; I <n; I ++) {scanf ("% s", str1); strcpy (name [CNT], str1); // The socket is not from the parent, insert CNT ++ directly; addedge (0, I + 2, 1); // edge creation. The source point connects one side to each socket} scanf ("% d ", & M); For (INT I = 0; I <m; I ++) {scanf ("% S % s", str1, str2 ); strcpy (name [CNT], str1); // the device will not repeat. Insert CNT ++ directly; int u = find (str2); // the Zari socket may be repeated and you need to find addedge (u, cnt-1, 1); // edge creation, the socket connects a side with a capacity of 1 to the device addedge (cnt-1, 1, 1); // the edge is built, and the device connects an edge to the sink point, capacity: 1} scanf ("% d", & K); For (INT I = 0; I <K; I ++) {scanf ("% S % s ", str1, str2); int u = find (str1); int v = find (str2); addedge (v, U, INF); // edge creation, from the latter to the former, the capacity is infinite} n = CNT; int ans = dinic (); printf ("% d \ n", M-ans);} return 0 ;}

ISAP:

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <vector>#include <queue>using namespace std;#define maxn 1010#define INF 0x3f3f3f3fstruct Edge{    int from, to, cap, flow;};char name[maxn][30];vector<Edge> EG;vector<int> G[maxn];int n, s, t, d[maxn], cur[maxn], p[maxn], num[maxn], mp[maxn][maxn];bool vis[maxn];int cnt;void addEdge(int from, int to, int cap){    EG.push_back((Edge){from, to, cap, 0});    EG.push_back((Edge){to, from, 0, 0});    int x = EG.size();    G[from].push_back(x-2);    G[to].push_back(x-1);}void bfs(){    memset(vis, false, sizeof(vis));    queue<int> q;    vis[t] = true;    d[t] = 0;    q.push(t);    while(!q.empty()) {        int x = q.front();        q.pop();        for(int i = 0; i < G[x].size(); i++) {            Edge& e = EG[G[x][i]^1];            if(!vis[e.from] && e.cap > e.flow) {                vis[e.from] = true;                d[e.from] = d[x]+1;                q.push(e.from);            }        }    }}int augment(){    int x = t, a = INF;    while(x != s) {        Edge& e = EG[p[x]];        a = min(a, e.cap-e.flow);        x = EG[p[x]].from;    }    x = t;    while(x != s) {        EG[p[x]].flow += a;        EG[p[x]^1].flow -= a;        x = EG[p[x]].from;    }    return a;}int ISAP(){    int ans =0;    bfs();    memset(num, 0, sizeof(num));    for(int i = 0; i < n; i++)        num[d[i]]++;    int x = s;    memset(cur, 0, sizeof(cur));    while(d[s] < n) {        if(x == t) {            ans += augment();            x = s;        }        bool flag = false;        for(int i = cur[x]; i < G[x].size(); i++) {            Edge& e = EG[G[x][i]];            if(e.cap > e.flow && d[x] == d[e.to]+1) {                flag = true;                p[e.to] = G[x][i];                cur[x] = i;                x = e.to;                break;            }        }        if(!flag) {            int m = n-1;            for(int i = 0; i < G[x].size(); i++) {                Edge& e = EG[G[x][i]];                if(e.cap > e.flow)                    m = min(m, d[e.to]);            }            if(--num[d[x]] == 0) break;            num[d[x] = m+1]++;            cur[x] = 0;            if(x != s)                x = EG[p[x]].from;        }    }    EG.clear();    for(int i = 0; i < n; ++i)        G[i].clear();    return ans;}int Find(char* str){    int i;    for(i = 2; i < cnt; ++i)        if(strcmp(name[i], str) == 0)            return i;    strcpy(name[i], str);    cnt++;    return i;}int main(){    //freopen("poj_1087.txt", "r", stdin);    int m, k;    char str1[30], str2[30];    while(~scanf("%d", &n)) {        s = 0, t = 1;        cnt = 2;        for(int i = 0; i < n; i++) {            scanf("%s", str1);            strcpy(name[cnt], str1);            cnt++;            addEdge(0, i+2, 1);        }        scanf("%d", &m);        for(int i = 0; i < m; i++) {            scanf("%s%s", str1, str2);            strcpy(name[cnt], str1);            cnt++;            int u = Find(str2);            addEdge(u, cnt-1, 1);            addEdge(cnt-1, 1, 1);        }        scanf("%d", &k);        for(int i = 0; i < k; i++) {            scanf("%s%s", str1, str2);            int u = Find(str1);            int v = Find(str2);            addEdge(v, u, INF);        }        n = cnt;        int ans = ISAP();        printf("%d\n", m-ans);    }    return 0;}

I don't know why. I always feel that my dinic is faster than ISAP, and it is almost every time. No.

Poj 1087 a plug for Unix conference room outlet problem diagram + maximum flow

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.