ZOJ3524: Crazy Shopping (Topology Sorting + full backpack) and zoj3524shopping

Source: Internet
Author: User

ZOJ3524: Crazy Shopping (Topology Sorting + full backpack) and zoj3524shopping

Because of the 90th anniversary ofCoherent & Cute Patchouli(C. C.P ),Kawashiro NitoriDecides to buy a lot of rare things to celebrate.

Kawashiro NitoriIs a very shyKappa(A type of water sprite that live in rivers) and she lives onYoukai Mountain.Youkai MountainIs a dangerous place fullYoukai, So normally humans are unable to be close to the mountain. But because of the financial crisis, something have changed. For example,Youkai MountainBecomes available for tourists.

On the mountain there areNTourist attractions, and there is a shop in each tourist attraction. to make the tourists feel more challenging (for example, to collect all kinds of souvenirs), each shop sells only one specific kind of souvenir that can not buy in any other shops. meanwhile, the number of the souvenirs which sells in each shop is infinite.NitoriAlso knows that each kind of souvenir has a weightTWi(In kilogram) and a valueTVi.

NowNitoriIs ready to buy souvenirs. For convenience,NitoriNumbered the tourist attraction from 1N. At the beginningNitoriIs located at the tourist attractionXAnd there areMRoads connect some pairs of tourist attractions, and each road has a lengthL. However, becauseYoukai MountainIs very steep, all roads are uni-directional. by the way, for same strange reason, the roads ensure that when someone left one tourist attraction, he can not arrive at the same tourist attraction again if he goes along the road.

NitoriHas one bag and the maximal load isWKilogram. When there areKKilogram things inNitori'S bag, she needs to costKUnits energy for walking one unit length road. Of course she doesn't want to waste too much energy, so please calculate the minimal cost of energyNitoriWhen the value is maximal.

Notice:NitoriCan buy souvenir at tourist attractionX, And she can stop at any tourist attraction. also, there are no two different roads between the same two tourist attractions. moreover, though the shop sells different souvenirs, it is still possible for two different kinds of souvenir have the same weight or value.

Input

There are multiple test cases. For each test case:

The first line contains four numbersN(1 <=N<= 600)-the number of tourist attractions,M(1 <=M<= 60000)-the number of roads,W(1 <=W<= 2000)-the load of the bag andX(1 <=X<=N)-The starting pointNitori.

Then followedNLines, each line contains two integers which means the shop on tourist attractionISellsTWiAndTViThings (1 <=TWi<= W, 1 <=TVi<= 10000 ).

Next, there areMLines, each line contains three numbers,Xi,YiAndLi, Which means there is a one-way road from tourist attractionXiToYi, And the length isLi(1 <=Xi,Yi<= N, 1 <=Li<= 10000 ).

Output

For each test case, output the answer as the description required.

Sample Input
4 4 10 11 12 33 44 51 2 51 3 42 4 43 4 5
Sample Output
0
 
Question:
There is a directed acyclic graph with n points on it. Each place has an item that occupies V space and has a value of W. Now, a person starts from a certain point, if a backpack contains items with a weight of C and the distance traveled is K, the physical strength of C * K will be consumed. If the backpack has a certain capacity, what is the minimum physical strength I need to get the maximum value?
 
Ideas:
Perform a topological sorting first. Based on the characteristics of the sorting, we only need to perform a full backpack in this order.
 
 
# Include <stdio. h> # include <algorithm> # include <string. h> # include <vector> # include <math. h> using namespace std; struct node {int next, dis ;}; vector <node> a [605]; int n, m, sum, st; int v [605], w [605], root [605]; int dp [605] [2005], tp [605], tem [605], top, len; int power [605] [2005], vis [605]; int max_val, min_pow; void topoo () // topological sorting {int I, j; len = top = 0; for (I = 1; I <= n; I ++) {if (! Root [I]) tem [++ top] = I;} while (top) {int k = tem [top --]; tp [++ len] = k; int end = a [k]. size (); for (I = 0; I <end; I ++) {node ss = a [k] [I]; root [ss. next] --; if (! Root [ss. next]) tem [++ top] = ss. next ;}} void solve () {max_val = 0; int I, j, k; vis [st] = 1; for (I = 0; I <= sum; I ++) // initialize {power [st] [I] = 0; if (I> = v [st]) dp [st] [I] = max (dp [st] [I], dp [st] [I-v [st] + w [st]);} max_val = dp [st] [sum]; min_pow = 0; for (I = 1; I <= n; I ++) {int x = tp [I]; if (! Vis [x]) continue; int l = a [x]. size (); for (j = 0; j <l; j ++) {node sk = a [x] [j]; int y = sk. next; vis [y] = 1; for (k = 0; k <= sum; k ++) // first compare the condition of the x position with that of the y position, select the optimal first shift to the y position {if (dp [x] [k]> dp [y] [k]) {dp [y] [k] = dp [x] [k]; power [y] [k] = power [x] [k] + sk. dis * k;} else if (dp [x] [k] = dp [y] [k]) {if (power [y] [k] =-1) power [y] [k] = power [x] [k] + sk. dis * k; else power [y] [k] = min (power [y] [k], power [x] [k] + sk. dis * k );}} For (k = v [y]; k <= sum; k ++) // perform a full backpack {if (dp [y] [k] <dp [y] [k-v [y] + w [y]) {dp [y] [k] = dp [y] [k-v [y] + w [y]; power [y] [k] = power [y] [k-v [y];} else if (dp [y] [k] = dp [y] [k-v [y] + w [y]) {power [y] [k] = min (power [y] [k], power [y] [k-v [y]);} for (k = 0; k <= sum; k ++) // find the answer {if (dp [y] [k]> max_val | (dp [y] [k] = max_val & power [y] [k] <min_pow )) {max_val = dp [y] [k]; min_pow = power [y] [k] ;}}} int main () {int I, J, k; while (~ Scanf ("% d", & n, & m, & sum, & st) {for (I = 1; I <= n; I ++) scanf ("% d", & v [I], & w [I]); memset (root, 0, sizeof (root )); memset (dp, 0, sizeof (dp); memset (power,-1, sizeof (power); memset (vis, 0, sizeof (vis); int x, y, z; for (I = 0; I <= n; I ++) a [I]. clear (); for (I = 0; I <m; I ++) {scanf ("% d", & x, & y, & z ); root [y] ++; node ss; ss. next = y; ss. dis = z; a [x]. push_back (ss);} topoo (); solve (); printf ("% d \ n", min_pow);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.