Exercise # # Special strongly connected component, condensing point
1. Luo Gu P1455 with purchase
Title Description
Tomorrow is Mother's Day, a group which kids busy thinking about what I think hard lessons of the gift to express their intention to send it? I heard on a website to sell the clouds, the children decided to travel together to see this amazing merchandise, this store has n cloud, clouds has been the boss numbered 1,2,3, ......, n and every cloud has a value, but the shop owner is a very strange man, he will tell you some clouds to match up buy and sold, that is to say with this is to buy a cloud cloud cloud has a mix of both buy, computer group do you think this gift is too strange, but your money is limited, so you definitely want to use existing money to buy as much as possible the value of the cloud.
Input Format
Line 1 n, m, w, n represents the number of cloud, m and with a conventional money you
Row 2 to row n + 1, each row ci, di represents the price and cloud value i
Of the n + 2 to n + 1 + m, each line ui, ui vi represents buy must buy vi, the same token, if it is necessary to buy ui vi buy
Output Format
A line representing the maximum value that can be obtained.
Q: meaning of the questions is: there are n clouds, clouds each with its own price and value ci di, Xiao Ming want to use the money in the hands of as large as possible value. But the owners have fixed with the program between the clouds are not open to buy. Xiao Ming wants to know that he can buy the greatest value is how much?
A: To understood meaning of the questions, between clouds with, i.e. each with a strongly connected components. First, we identify strongly connected components of each, and calculate the total price and total value of each strongly connected component; the second step, we calculate Xiao Ming maximum value of existing money can buy, it is clearly a 01 simple backpack, but this time out with a two-dimensional backpack, needs to be optimized as a one-dimensional array with a scroll. This concludes. (As if disjoint-set +01 backpack can write, think of it next time to make up.)
1 #include<algorithm> 2 #include<iostream> 3 #include<cstring> 4 #include<cstdio> 5 #include<stack> 6 using namespace std; 7 const int maxv= 10010; 8 const int maxe= 10100; //可能的最大值 9 10 struct ENode 11 { 12 int to; 13 int Next; 14 }; 15 ENode edegs[maxe]; 16 int Head[maxv], tnt; 17 void init() 18 { 19 memset(Head, -1, sizeof(Head)); 20 tnt= -1; 21 } 22 void Add_ENode (int a, int b) 23 { 24 ++ tnt; 25 edegs[tnt].to= b; 26 edegs[tnt].Next= Head[a]; 27 Head[a]= tnt; 28 ++ tnt; 29 edegs [TNT] .to = A; 30 edegs [TNT] = .NEXT Head [B]; 31 is Head [B] = TNT; 32 } 33 is 34 is int m; 35 int DFN [MAXV]; // depth-first search time accessed vertex 36 int Low [MAXV]; // vertex v and its neighbors in the Low [] of minimum 37 [ int TEMP [MAXV]; // determine whether the node has been accessed (not accessed 0- 1 - 2- visited visited deleted deleted) 38 is int stack [MAXV]; // manual stack 39 int TarBfs ( int V, int Lay, int &scc_num) 40 { 41 is / * V: newly added points; Lay: timestamp; scc_num: Record Number strongly connected component * / 42 is 43 is / * Step 2: Initialization dfn [v] and Low [V] * / 44 is TEMP [V] = . 1 ; 45 Low [V] = Lay; 46 is DFN [V] = Lay; 47 Stack [++ m] = V; 48 for ( int K = Head [V]; K = -! . 1 ; = K edegs [K] .NEXT) 49 { 50 / * for all adjacent node v U: * / 51 is int U = edegs [K] .to; 52 is IF (TEMP [U] == 0 ) 53 is { 54 is / * Step 2-1: If not visited, then jump to step 2, while maintaining Low [V] * / 55 TarBfs (U, ++ Lay , scc_num); 56 is 57 is } 58 IF (TEMP [U] == . 1 ) 59 { 60 / * step 2-2: If visited, but not removed, maintenance Low [V] * / 61 is Low [V] = min (Low [V], Low [U]); 62 is } 63 is 64 } 65 IF (DFN [V] == Low [V]) 66 { 67 / * If the low [v] == dfn [v ], the current node is the root of a strongly connected component, 68 then outputs a corresponding strongly connected components. * / 69 ++ scc_num; 70 do 71 is { 72 Low [Stack [m]] = scc_num; 73 is temp [Stack [m]] = 2 ; // deleted node updates temp 2 74 } the while (Stack [m ! -] = V); 75 } 76 return 0 ; 77 } 78 79 int Tarjan ( int n-) 80 { 81 int scc_num = 0 , Lay = . 1 ; 82 m = 0 ; 83 Memset (TEMP, 0 , the sizeof (TEMP)); 84 Memset (Low, 0 , the sizeof (Low)); 85 for ( int I = . 1 ; I < n-=; I ++ ) 86 { 87 IF (TEMP [I] == 0 ) 88 { 89 / * step 1: Get a node v has not been visited, otherwise the algorithm ends * / 90 TarBfs (I, Lay , scc_num); 91 } 92 } 93 / * Returns the number of strongly connected components * / 94 return scc_num; 95 } 96 97 int CI [MAXV]; 98 int DI [MAXV]; 99 int rock_ci [MAXV]; // for each communication block cost and 100 int rock_di [MAXV]; // value of each block and communicating 101 int DP [ 10010 ]; 102 int main () 103 { 104 int n-, m, cost; 105 int a, B; 106 Scanf ("%d %d %d", &n, &m, &cost); 107 for (int i= 1; i<= n; i ++) 108 { 109 scanf("%d %d", &a, &b); 110 ci[i]= a; 111 di[i]= b; 112 } 113 init(); 114 for (int i= 0; i< m; i ++) 115 { 116 scanf("%d %d", &a, &b); 117 Add_ENode(a, b); 118 } 119 120 int ans= Tarjan(n); 121 memset(rock_ci, 0, sizeof(rock_ci)); 122 memset(rock_di, 0, sizeof(rock_di)); 123 for (int i= 1; i<= n; i ++) 124 { 125 int tmp= low[i]; 126 rock_ci[tmp]+= ci[i]; 127 rock_di[tmp]+= di[i]; 128 } 129 int sum= 0; 130 // for (int i= 1; i<= ans; i ++) 131 // { 132 // printf("--> %d %d\n", rock_ci[i], rock_di[i]); 133 // } 134 // cout << cost << endl; 135 for (int i= 1; i<= ans; i ++) 136 { 137 for (int j= cost; j>= 0; j --) 138 { 139 if (j- rock_ci[i]>= 0) dp[j]= max(dp[j], dp[j- rock_ci[i]]+ rock_di[i]); 140 else dp[j]= dp[j]; 141 // printf("-%d- -%d- -%d-\n", dp[i][j], dp[i- 1][j], rock_di[i]); 142 } 143 } 144 printf("%d\n", dp[cost]); 145 return 0; 146 }
end;