POJ-1087-A Plug for UNIX
Http://poj.org/problem? Id = 1087
N1 sockets, n2 electrical appliances and their corresponding sockets, n3 converters, the former can be converted to the latter one. Ask the minimum number of devices that are not used for sockets. The number of converters is unlimited.
The maximum flow, the source point is built to the outlet, the capacity is 1, the electrical appliance is built to the junction point, the capacity is 1, the corresponding outlet and electrical appliance are connected to the edge, the capacity is 1, the first socket is converted to the next socket, and the last socket is built to the front of a socket. The capacity is infinite, and the maximum flow obtained is the most paired electrical appliance.
# Include <iostream> # include <cstdio> # include <cstring> # include <queue> # include <algorithm> using namespace std; const int maxn = 30000; const int maxm = 50000; const int INF = 0x7fffff; int idx; int cur [maxn], pre [maxn]; int dis [maxn], gap [maxn]; int aug [maxn], head [maxn]; char name [3000] [30]; int k; struct Node {int u, v, w; int next ;} edge [maxm]; void addEdge (int u, int v, int w) {edge [idx]. u = u; edge [idx]. v = V; edge [idx]. w = w; edge [idx]. next = head [u]; head [u] = idx ++; edge [idx]. u = v; edge [idx]. v = u; edge [idx]. w = 0; edge [idx]. next = head [v]; head [v] = idx ++;} int sol (char * str) {int I; if (k = 0) {strcpy (name [1], str); k = 1; return 1 ;}for (I = 1; I <= k; I ++) if (strcmp (name [I], str) = 0) return I; k ++; strcpy (name [k], str); return k ;} int SAP (int s, int e, int n) {int max_flow = 0, v, u = s; int id, mindis; aug [s] = INF; p Re [s] =-1; memset (dis, 0, sizeof (dis); memset (gap, 0, sizeof (gap); gap [0] = n; // I think this sentence is optional, because dis [e] is always 0 for (int I = 0; I <= n; ++ I) // initialize the current arc as the first arc cur [I] = head [I]; while (dis [s] <n) {bool flag = false; if (u = e) {max_flow + = aug [e]; for (v = pre [e]; v! =-1; v = pre [v]) // path tracing updates residual network {id = cur [v]; edge [id]. w-= aug [e]; edge [id ^ 1]. w + = aug [e]; aug [v]-= aug [e]; // modify the augmented quantity. if (edge [id] will be used in the future. w = 0) u = v; // do not roll back to the source point, only roll back to the arc end of the arc with the capacity of 0} for (id = cur [u]; id! =-1; id = edge [id]. next) {// start from the current arc to find the allowable arc v = edge [id]. v; if (edge [id]. w> 0 & dis [u] = dis [v] + 1) // find the allowable arc {flag = true; pre [v] = u; cur [u] = id; aug [v] = min (aug [u], edge [id]. w); u = v; break;} if (flag = false) {if (-- gap [dis [u] = 0) break;/* gap optimization, the Hierarchy Tree ends with a fault */mindis = n; cur [u] = head [u]; for (id = head [u]; id! =-1; id = edge [id]. next) {v = edge [id]. v; if (edge [id]. w> 0 & dis [v] <mindis) {mindis = dis [v]; cur [u] = id; // modify the current arc while modifying the label} dis [u] = mindis + 1; gap [dis [u] ++; if (u! = S) u = pre [u]; // trace back to continue searching for allowed arcs} return max_flow;} int main () {int I, n1, n2, n3; int st, ed; int a, B; char str1 [30], str2 [30]; while (scanf ("% d", & n1 )! = EOF) {idx = 0; memset (head,-1, sizeof (head); st = 0; k = 0; for (I = 1; I <= n1; I ++) {scanf ("% s", str1); a = sol (str1); addEdge (st, a, 1 ); // Source Vertex edge creation} scanf ("% d", & n2); ed = n1 + n2 + 1; for (I = 1; I <= n2; I ++) {scanf ("% s", str1, str2); a = sol (str1); B = sol (str2); addEdge (B,, 1); // electrical outlet build edge addEdge (a, ed, 1); // electrical appliance build edge to sink point} scanf ("% d", & n3 ); for (I = 1; I <= n3; I ++) {scanf ("% s", str1, str2); a = sol (str1 ); B = sol (str2); addEdge (B, a, INF);} printf ("% d \ n", n2-SAP (st, ed, k + 2 ));} return 0 ;}