Beloved sons http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 1338
The king has n sons. Now every son can get a happy value when he is married. The prince number is 1-N, and the number of N girls is also 1-N, every prince has a favorite girl in his heart. Now, if asked, the style and maximum given in the question can be made.
Analysis: in fact, the root number in the question is a smoke bullet, as long as you care about the square of the value of joy. Then, for the sides between the Prince and the girl, the edge weight is the square of the value of joy. For each Prince, the virtual edge right of a girl is 0, this is to enable all the princes to have matching girls so that the algorithms can be correctly executed.
To find the optimal match for a bipartite graph, the maximum weight is required, and lmatch returns an optimal match.
1 # include <cstdio> 2 # include <cstring> 3 # define MT (a, B) memset (a, B, sizeof (a) 4 const int INF = 0x3f3f3f3f; 5 class kuhn_munkras {// The Optimal Match O (LN * ln * rn) of the Bipartite Graph adjacent array 6 typedef int typec; /// edge weight type 7 static const int mv = 1024; // number of points 8 int ln, RN, s [mV], t [mV], ll [mV], RR [mV], p, q, I, J, K; 9 typec mat [mV] [mV]; 10 public: 11 int lmatch [mV], rmatch [mV]; 12 Void Init (int tln, int TRN) {// enter the left vertex number of LN and the right vertex number of Rn. ln <= rn is required, subscript 0 start 13 Ln = TLN; 14 Rn = TRN; 15 for (I = 0; I <ln; I ++) 16 for (j = 0; j <rn; j ++) 17 mat [I] [J] =-INF; 18} 19 void add (INT U, int V, typec W) {// The opposite number of weights can be obtained for the minimum weight match. 20 mat [u] [v] = W; 21} 22 typec solve () {// returns the optimal matching value.-1 indicates that the matching fails. 23 typec ret = 0; 24 for (I = 0; I <ln; I ++) {25 for (LL [I] =-INF, j = 0; j <rn; j ++) 26 ll [I] = mat [I] [J]> ll [I]? Mat [I] [J]: ll [I]; 27 if (LL [I] =-INF) Return-1; // cannot match! 28} 29 for (I = 0; I <rn; RR [I ++] = 0); 30 Mt (lmatch,-1); 31 MT (rmatch, -1); 32 for (I = 0; I <ln; I ++) {33 MT (T,-1 ); 34 For (s [p = q = 0] = I; P <= Q & lmatch [I] <0; P ++) 35 For (k = s [p], j = 0; j <rn & lmatch [I] <0; j ++) 36 IF (LL [k] + RR [J] = mat [k] [J] & T [J] <0) {37 s [++ q] = rmatch [J], t [J] = K; 38 If (s [Q] <0) 39 for (P = J; p> = 0; j = P) 40 rmatch [J] = k = T [J], P = lmatch [K], lmatch [k] = J; 41} 42 if (lmatch [I] <0) {43 for (I --, P = inf, K = 0; k <= Q; k ++) 44 f Or (j = 0; j <rn; j ++) 45 if (T [J] <0 & ll [s [k] + RR [J]-mat [s [k] [J] <p) 46 p = ll [s [k] + RR [J]-mat [s [k] [J]; 47 for (j = 0; j <rn; rr [J] + = T [J] <0? 0: P, J ++); 48 for (k = 0; k <= Q; ll [s [k ++]-= P ); 49} 50} 51 for (I = 0; I <ln; I ++) {52 If (lmatch [I] <0) Return-1; 53 If (MAT [I] [lmatch [I] <=-INF) Return-1; 54 RET + = mat [I] [lmatch [I]; 55} 56 return ret; 57} 58} GX; 59 int A [1, 512]; 60 int main () {61 int t, n, m; 62 while (~ Scanf ("% d", & T) {63 while (t --) {64 scanf ("% d", & N); 65 for (INT I = 0; I <n; I ++) {66 scanf ("% d", & A [I]); 67 a [I] * = A [I]; 68} 69 GX. init (n, n <1); 70 for (INT I = 0, J; I <n; I ++) {71 scanf ("% d ", & M); 72 while (M --) {73 scanf ("% d", & J); 74 GX. add (I, J-1, a [I]); 75} 76 GX. add (I, I + N, 0); 77} 78 int flag = GX. solve (); 79 for (INT I = 0; I <n; I ++) {80 int ans = 0; 81 If (Gx. lmatch [I] <n) {82 ans = GX. lmatch [I] + 1; 83} 84 printf ("% d", ANS); 85} 86 puts (""); 87} 88} 89 return 0; 90}
View code
End
The best match in the bipartite graph to find the maximum or minimum weight match