2944. Mussy Paper
Time limit: 2.0 Seconds Memory Limit: 65536K Special Judge
Total Runs: 381 Accepted Runs: 98
A good mathematical joke is better, and better mathematics,
than a dozen mediocre papers.
--j E Littlewood
RoBa, an undergraduate student, was preparing for his thesis. He collects many papers about artificial intelligence, the field that he was very interested in. However, he doesn ' t know. You know at the tail of every paper, there are a list of reference papers, describing the relative work of other researcher S. So these papers may form a very complicated net. Now, RoBa turns-to-help.
RoBa has given every paper a interesting value (which can be negative). A greater value indicates a more interesting paper. He wants to select a paper set S, such that, for every paper in this set, all the papers in its reference list AR E also contained in the set S. And what's more, he wants to make the sum of the interesting value in the set maximum.
InputThere is multiple test cases in the input. The first line of all test case contains an integer
N,(
N≤100) indicating the amount of papers. Then
NLines followed. Each line contains integers
Vi(|
Vi| ≤10,000) and
PiAt first.
Viindicating the interesting value of the
I-th paper,
Piindicating the amount of reference papers of the
I-th paper. Then
PiNumbers followed, indicating all the reference papers. The papers is numbered from 1 to
N.
The input is terminated with N = 0.
OutputIf the maximum possible sum is greater than zero, output both lines for each test case. The first line contains-integers, the maximum sum of interesting value, and the amount of papers in the selected set. The next line contains all the papers, separated by space. If there is more than one valid set to get the maximum sum, anyone'll be OK. Please note the set cannot is empty.
If the maximum possible sum is no more than zero, you should only output one line says "refused" instead, which means RoBa Would refuse to does this.
Sample Input
4-10 1 210 2 3 4-3 0-3 04-10 1 2-10 2 3 4-3 0-3 00
Sample Output
4 3 4Refused
Problem Setter:roba
Note: Special judge problem, you may get the "wrong Answer" when the output in wrong format.
Source: tju Team Selection Contest (4)
Solving the problem: Maximum weight closure sub-graph
1#include <bits/stdc++.h>2 using namespacestd;3 Const intMAXN = the;4 Const intINF = ~0U>>2;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],gap[maxn],d[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++; + } - intDfsintUintLow ) { + if(U = = T)returnLow ; A intTMP =0, MinH = T-1; at for(inti = Head[u]; ~i; i =E[i].next) { - if(e[i].flow) { - if(D[u] = = D[e[i].to] +1) { - intA =Dfs (E[i].to,min (e[i].flow,low)); -E[i].flow-=A; -e[i^1].flow + =A; inTMP + =A; -Low-=A; to } + if(e[i].flow) MinH =min (minh,d[e[i].to]); - if(!low) Break; the } * } $ if(!tmp) {Panax Notoginseng if(--gap[d[u]] = =0) D[s] =T; -++gap[d[u] = MinH +1]; the } + returntmp; A } the intsap () { +Memset (Gap,0,sizeofgap); -memset (D,0,sizeofd); $Gap[s] =T; $ intRET =0; - while(D[s] < T) ret + =DFS (s,inf); - returnret; the } - intn,m;Wuyi BOOLVIS[MAXN]; thevector<int>ans; - voidDfsintu) { WuVis[u] =true; - if(U! = S && u! =T) Ans.push_back (u); About for(inti = Head[u]; ~i; i =e[i].next) $ if(!vis[e[i].to] &&e[i].flow) DFS (e[i].to); - } - - intMain () { A while(SCANF ("%d",&N), N) { +memset (head,-1,sizeofhead); theS = n +1; -T = S +1; $ intsum = tot =0, W; the for(intU =1, V; U <= N; ++u) { thescanf"%d%d",&w,&m); the if(W >0) { the Add (s,u,w); -Sum + =W; in}ElseAdd (u,t,-W); the while(m--) { thescanf"%d",&v); About Add (u,v,inf); the } the } theSum-=SAP (); + if(!sum) puts ("refused"); - Else{ the ans.clear ();Bayimemset (Vis,false,sizeofvis); the DFS (S); theprintf"%d%d\n", Sum,ans.size ()); - sort (Ans.rbegin (), Ans.rend ()); - for(inti = ans.size ()-1; I >=0; --i) theprintf"%d%c", Ans[i],i?' ':'\ n'); the } the } the return 0; -}
View Code
TOJ 2944 Mussy Paper