Http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemid = 4439
Question:
A directed acyclic graph with n points and an edge with a length consumes the product of weight and edge length from one point to another. Selling items, value V, weight W, and quantity at each point are infinite. Given the starting point, and the size of the backpack, the minimum energy consumed can be stopped at any point.
Analysis:
I did not think about it for the first two days. I put it first. Today, I have been using water several times before =. F [I] [J] indicates the maximum value obtained by J at I. G [I] [J] indicates the minimum energy required to obtain the maximum value in the same state, then, sort the topology and transfer it. Because the question was read two days ago, I thought about the equation today and wrote it. I forgot to consider the connectivity of the graph. If the point cannot be reached from the starting point, I cannot use its answer to update the solution.
Thoughts:
The first two days of getting stuck mainly due to sudden unclear status transfer. As a matter of fact, status transfer can be separated, that is, when both push-down and reverse push are performed, and the status of loop to I and j is not only the status of I and J, but also the status of I, the status of J is transferred to the point to, J.
1 # include <cstdio> 2 # include <cstring> 3 # include <vector> 4 # include <queue> 5 using namespace STD; 6 7 typedef pair <int, int> edge; 8 int n, m, W, X, St; 9 vector <edge> E [610]; 10 long f [610] [2100], G [610] [2100]; 11 int C [610], V [610], bfn [610], in [610]; 12 bool reach [610]; 13 int main () 14 {15 while (scanf ("% d", & N, & M, & W, & X )! = EOF) 16 {17 for (INT I = 1; I <= N; I ++) {18 scanf ("% d", C + I, V + I); 19 E [I]. clear (); 20} 21 memset (in, 0, sizeof (in); 22 int XX, YY, WW; 23 for (INT I = 0; I <m; I ++) 24 {25 scanf ("% d", & XX, & YY, & ww); 26 E [XX]. push_back (make_pair (YY, WW); 27 in [YY] ++; 28} 29 queue <int> q; 30 for (INT I = 1; I <= N; I ++) 31 if (in [I] = 0) Q. push (I); 32 int CNT = 0; 33 while (! Q. empty () 34 {35 int u = Q. front (); q. pop (); 36 bfn [++ CNT] = u; 37 If (u = x) ST = CNT; 38 for (INT I = 0; I <E [u]. size (); I ++) {39 int v = E [u] [I]. first; 40 in [v] --; 41 if (! In [v]) Q. push (V); 42} 43} 44 memset (reach, false, sizeof (REACH); 45 reach [x] = true; 46 long ans = 1 <30, maxv = 0; 47 ans <= 30; 48 memset (F, 0, sizeof (f); 49 for (INT mark = sT; Mark <= N; mark ++) {50 int I = bfn [Mark]; 51 if (! Reach [I]) continue; 52 for (Int J = 0; j <= W; j ++) {53 If (j> = C [I]) {54 long TMP = f [I] [J-C [I] + V [I]; 55 if (F [I] [J] <TMP) {56 F [I] [J] = TMP; 57G [I] [J] = G [I] [J-C [I]; 58} 59 else if (F [I] [J] = TMP) {60g [I] [J] = min (G [I] [J], G [I] [J-C [I]); 61} 62} 63 for (int t = 0; t <E [I]. size (); t ++) {64 int v = E [I] [T]. first, L = E [I] [T]. second; // v certainly has not traversed 65 reach [v] = true; // F [v] [J] temporarily indicates that the capacity of V point j is changed from the previous point 66 If (F [I] [J]> F [v] [J]) {// maximum value obtained by consumption 67 F [v] [J] = f [I] [J]; 68g [v] [J] = G [I] [J] + L * J; 69} 70 else if (F [I] [J] = f [v] [J]) {71g [v] [J] = min (G [v] [J], G [I] [J] + L * j ); 72} 73} 74 if (F [I] [J]> maxv) {75 maxv = f [I] [J]; 76 ans = G [I] [J]; 77} 78 else if (F [I] [J] = maxv) ans = min (ANS, G [I] [J]); 79} 80} 81 printf ("% LLD \ n", ANS); 82} 83 return 0; 84}