Test instructions: input n, there are n sockets, the following n line is the type of each socket (the last 24 letters to indicate a socket, no spaces at ease with the scanf, but it is possible that the socket will be the same, but this has no effect)
Input m, there are m electrical appliances, the following m line of two words per line is the name of the appliance and the plug type (the same 24 letter word inside no space, two words separated by space)
Input k, there are K converters, the following K lines per line of two words, respectively, the converter's entry type and plug type
The number of each converter is infinite, and the converter itself can be connected to the converter
To ask you, so that the largest number of appliances can be plugged into the socket (can be assisted by the converter can also be directly plugged in), enter the amount of electrical appliances can not be plugged in
#include <iostream> #include <cstdio> #include <string> #include <utility> #include <queue > #include <vector>//#define tusing namespace Std;const int infinity = 1000000000;int count = 0;struct edge{in T from,to, flow, capacity; Edge (const int &V,CONST int &A, const int &c,const int &f) {from=v,to = a, capacity = c,flow=f; }};int Vexnum, edgenum;//indicates the number of vertices and the number of edges int receptacle, device, adapter;//means number of sockets, number of appliances, converter type Vector<string>table_ receptacle;vector<string>table_device;vector<pair<string,string> >table_adapter;int SRC, sink;/ /represents the source and sink int max_flow;//maximum flow vector<edge>edge;//twice times the number of edges vector<vector<int> >adjlist;//adjacency table void AddEdge (const int& from, const int& To,const int& capacity) {Edge.push_back (Edge (from,to,capacity,0)); Adjlist[from].push_back (Edge.size ()-1); Edge.push_back (Edge (to,from,0,0)); Adjlist[to].push_back (Edge.size ()-1);} void Init () {table_adapter.clear (); Table_device.clear (); Table_receptacle.clear (); Edge.clear (); Adjlist.clear ();} void Network () {max_flow = 0; src = 0;//Source Point set to 0//accept receptacle cin >> receptacle; Table_receptacle.resize (1); for (int i = 0; i < receptacle; i++) {string type; CIN >> type;//Socket Type table_receptacle.push_back (type); }//Receiving electrical cin >> device; Table_device.resize (1); for (int i = 0; i < device; i++) {string name, type; CIN >> name >> type; Table_device.push_back (type); }//Accept adapter CIN >> adapter; Table_adapter.resize (1); for (int i = 0; i < adapter; i++) {string type1, type2; CIN >> type1 >> type2; Table_adapter.push_back (Make_pair (type1,type2)); }//Initialize source point, sink point, adjacency table, vertex number src = 0, sink = device + receptacle + adapter+1, Vexnum = 1 + sink; Adjlist.resize (Vexnum); Edge for (int i = 1; i<=device; i++) {Addedge (SRc, I, 1); for (int j = 1; J <= Receptacle; j + +) {if (table_device[i]== Table_receptacle[j]) { Addedge (I, Device + j, 1); }} for (int j = 1; J <= Adapter; j + +) {if (table_device[i] = = Table_adapter[j].first) {Addedge (i, device + receptacle + j, infinity); }}} for (int i = 1; I <= receptacle; i++) {Addedge (device + i, sink, 1); for (int j = 1; J <= Adapter; j + +) {if (table_receptacle[i] = = Table_adapter[j].second) { Addedge (device + receptacle + j, device + i,1); }}} for (int i = 1, I <= adapter; i++) {for (int j = 1; J <= Adapter; j + +) { if (i = = j) {continue; } if (Table_adapter[i].first = = Table_adapter[j].second) {Addedge (device + receptacle + j, device + receptacle + i,infinity); }}}}void Getmaxflow () {Vector<int>path (vexnum); while (1) {vector<int>augument (vexnum); queue<int>q; Q.push (SRC); AUGUMENT[SRC] =infinity; while (! Q.empty ()) {int x = Q.front (); Q.pop (); for (unsigned int i = 0; i < adjlist[x].size (); i++) {edge&e = edge[adjlist[x][i]]; if (!augument[e.to]&&e.capacity>e.flow) {path[e.to] = adjlist[x] [i]; Augument[e.to] = std::min (Augument[x],e.capacity-e.flow); Q.push (e.to); }} if (Augument[sink]) {break; }} if (!augument[sink]) {break; } for (int u = sink; u!=src; u=edge[path[u]].from) {EDGE[PATH[U]].FLow + = Augument[sink]; edge[path[u]^1].flow-= Augument[sink]; } Max_flow + = Augument[sink]; }}void print () {cout << device-max_flow << Endl; count++;} int main () {#ifdef T freopen ("In.txt", "R", stdin); Freopen ("OUT.txt", "w", stdout); #endif//T int n; CIN >> N; while (n--) {init (); Network (); Getmaxflow (); Print (); if (n) {cout << Endl; count++; }} return 0;}
UVA 753 A Plug for UNIX