Poj1087-a plug for Unix

Source: Internet
Author: User


View more problem solving reports: http://blog.csdn.net/wangjian8006/article/details/7870410

Reprinted with the source: http://blog.csdn.net/wangjian8006

The meaning of the question is too difficult to understand, but it is better to understand the meaning of the question.
There are two kinds of items in this question: one is a socket and the other is an electrical appliance.
The socket has only one Jack and one plug, and the electrical appliance has only one plug.
There are N types of sockets, which are represented by strings. These N types of sockets can be understood as sockets inserted into the power supply.
Then there are m electrical appliances. Now the electrical appliances need to be charged. The electrical appliances are represented by strings. Each electrical appliance has its own socket.
(This socket can be not the N sockets inserted in the power supply, but other sockets)
There are now K pieces of information
S1 S2 indicates that S1 socket can be inserted into S2 socket, which is similar to converting the plug
These S1 and S2 may not be the N sockets plugged into the power supply.

Provide this information and ask you how many other electrical appliances can be used without a socket.

Solution:
It is easy to think of the largest stream:
Build a source point pointing to all appliances with a capacity of 1
All appliances point to the plug they can plug in, with a capacity of 1
If one plug can be inserted into another, point S1 to S2 with an infinite capacity
Point all plug in the power supply to the sink point with a capacity of 1

Then, find the maximum stream from the source point to the sink point.
However, creating a map is complicated because it involves string processing, so it is better to use the map container.

 

/* Memory 2800 ktime 47 Ms */# include <iostream> # include <string> # include <map> # include <queue> using namespace STD; # define maxv 810 // if the maximum value is set to 400, RE is contributed once. # define INF int_max # define min (A, B) (A> B? B: A) Map <string, int> rec_dev; char STR [30], stmp [30]; int rec_dev_sum; int res [maxv] [maxv]; // Save the Graph Information int n, m, K; intdis [maxv]; int source, sink, maxflow; int BFS () {int K; queue <int> q; memset (DIS,-1, sizeof (DIS); DIS [sink] = 0; q. push (sink); While (! Q. empty () {k = Q. front (); q. pop (); For (INT I = 0; I <maxv; I ++) {If (DIS [I] =-1 & res [I] [k]) {dis [I] = dis [k] + 1; q. push (I) ;}} if (k = source) return 1;} return 0;} int DFS (INT cur, int CP) {If (cur = sink) return CP; int TMP = CP, t; for (INT I = 0; I <maxv & TMP; I ++) {If (DIS [I] + 1 = dis [cur] & res [cur] [I]) {T = DFS (I, min (RES [cur] [I], TMP); Res [cur] [I]-= T; Res [I] [cur] + = T; TMP-= T;} return CP-TMP;} void dinic () {// dinic No need to explain maxflow = 0; while (BFS () maxflow + = DFS (source, INF);} int main () {int I; while (~ Scanf ("% d", & N) {memset (Res, 0, sizeof (RES); rec_dev.clear (); rec_dev_sum = 1; Source = 0; sink = MAXV-1; for (I = 0; I <n; I ++) {scanf ("% s", STR); rec_dev [STR] = rec_dev_sum ++; res [rec_dev [STR] [sink] = 1; // point to the sink point in the power outlet} scanf ("% d", & M); for (I = 0; I <m; I ++) {scanf ("% S % s", stmp, STR); rec_dev [stmp] = rec_dev_sum ++; If (! Rec_dev [STR]) {// Add rec_dev [STR] = rec_dev_sum ++ if the socket does not exist;} res [Source] [rec_dev [stmp] = 1; // The Source point points to the device res [rec_dev [stmp] [rec_dev [STR] = 1; // The device points to the socket} scanf ("% d", & K ); for (I = 0; I <K; I ++) {scanf ("% S % s", STR, stmp); If (! Rec_dev [STR]) {// Add rec_dev [STR] = rec_dev_sum ++;} If (! Rec_dev [stmp]) {rec_dev [stmp] = rec_dev_sum ++;} res [rec_dev [STR] [rec_dev [stmp] = inf; // socket to socket is infinite} dinic (); printf ("% d \ n", M-maxflow);} return 0 ;}

 

 

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.