A simple brute force problem. Time Limit:1000msmemory limit:65536kbthis problem'll be judged onHDU. Original id:4971
64-bit integer IO format: %i64d Java class name: Main There ' s a company with several projects to is done. Finish a project would get you profits. However, there is some technical problems for some specific projects. To solve the problem, the manager would train his employee which could cost his budget. There may is dependencies between technical problems, for example, A requires B means you need to solve problem B before s Olving problem A. If a requires B and B requires A, it means it should solve them at the same time. You can select the which problems to is solved and how to solve them freely before finish your projects. Can you tell me the maximum profit?
InputThe first line of the input was a single integer T (<=100) which is the number of the test cases.
Each test case contains a line with both integer n (<=20) and M (<=50) which is the number of project to select to Comp Lete and the number of technical problem.
Then a line with n integers. The i-th Integer (<=1000) means the profit of complete the I-TH project.
Then a line with M integers. The i-th Integer (<=1000) means the cost of training to solve the i-th technical problem.
then n lines. Each line contains some integers. The first integer k is the number of technical problems, followed by k integers implying the technical problems need Lve for the i-th project.
After that, there is m lines with all line contains m integers. If the i-th row of the j-th column is 1, it means so need to solve the i-th problem before solve the j-th problem. Otherwise the i-th row of the j-th column is 0.OutputFor each test case, click Output a line which are "case #X: Y", X means the number of the the the test case and Y means the the M Aximum profit.Sample Input
42 310 106 6 62 0 12 1 20 1 01 0 00 0 02 310 108 10 61 01 20 1 01 0 00 0 02 310 108 10 61 01 20 1 00 0 00 0 02 310 108 10 61 01 20 0 01 0 00 0 0
Sample Output
Case #1:2Case #2:4Case #3:4Case #4:6
SourceMulti-university Training Contest 10 problem solving: Online said the maximum right to close the sub-chart ... But I see If the i-th row of the j-th column is 1, it means so need to solve the i-th problem before solve the j-th probl Em I think is j+n to I+n, because the choice of J must first select I, by the nature of the closed sub-graph, J-i is the exit of J, then j-i must choose but do not know why is wrong
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = -;4 Const intINF =0x3f3f3f3f;5 structarc{6 intTo,flow,next;7Arcintx =0,inty =0,intz =-1){8to =x;9Flow =y;TenNext =Z; One } A}e[maxn*MAXN]; - inthead[maxn],d[maxn],cur[maxn],tot,s,t; - voidAddintUintVintflow) { theE[tot] =arc (V,flow,head[u]); -Head[u] = tot++; -E[tot] = arc (U,0, Head[v]); -HEAD[V] = tot++; + } - BOOLBFs () { +queue<int>Q; Amemset (d,-1,sizeofd); atD[s] =0; - Q.push (S); - while(!Q.empty ()) { - intU =Q.front (); - Q.pop (); - for(inti = Head[u]; ~i; i =E[i].next) { in if(E[i].flow && d[e[i].to] = =-1){ -D[e[i].to] = D[u] +1; to Q.push (e[i].to); + } - } the } * returnD[t] >-1; $ }Panax Notoginseng intDfsintUintLow ) { - if(U = = T)returnLow ; the intTMP =0, A; + for(int&i = Cur[u]; ~i; i =E[i].next) { A if(E[i].flow && d[e[i].to] = = d[u]+1&& (a=Dfs (E[i].to,min (e[i].flow,low)))) { theE[i].flow-=A; +Low-=A; -e[i^1].flow + =A; $TMP + =A; $ if(!low) Break; - } - } the if(!tmp) D[u] =-1; - returntmp;Wuyi } the intDinic () { - intRET =0; Wu while(BFS ()) { -memcpy (Cur,head,sizeofhead); AboutRET + =DFS (s,inf); $ } - returnret; - } - intMain () { A intTs,n,m,u,v,w,k,ret,cs =1; +scanf"%d",&Ts); the while(ts--){ -memset (head,-1,sizeofhead); $scanf"%d%d",&n,&m); thetot = RET = S =0; theT = n + M +1; the for(inti =1; I <= N; ++i) { thescanf"%d",&W); - Add (s,i,w); inRET + =W; the } the for(inti =1; I <= m; ++i) { Aboutscanf"%d",&W); theAdd (i+n,t,w); the } the for(inti =1; I <= N; ++i) { +scanf"%d",&k); - while(k--){ thescanf"%d",&u);BayiAdd (i,u + n +1, INF); the } the } - for(inti =1; I <= m; ++i) - for(intj =1; J <= M; ++j) { thescanf"%d",&W); the if(w) Add (i+n,j+n,inf); the } theprintf"Case #%d:%d\n", Cs++,ret-dinic ()); - } the return 0; the}
View Code
HDU 4971 A Simple brute force problem.