Zoj3524:crazy Shopping (topological sort + full backpack)

Source: Internet
Author: User

Because of the 90th anniversary of the coherent & Cute patchouli (C.C.P), Kawashiro Nitori decides t o buy a lot of rare things to celebrate.

Kawashiro Nitori  is A Very shy  Kappa   (a type of water sprite that live in rivers) and she lives on  Youkai mountain .  Youkai Mountain  is A dangerous place full of  Youkai , so normally humans is unable t O is close to the mountain. But because of the financial crisis, something has changed. For example, , Youkai Mountain  becomes available for tourists.

on the mountain there are  N  tourist Attractions, and there are a shop in each tourist attraction. The tourists feel more challenging (for example, to collect all kinds of souvenirs), each shop sells only one spec Ific kind of souvenir that can is not buy on any other shops. Meanwhile, the number of the souvenirs which sells is infinite.  Nitori  also knows the EAC H kind of souvenir has a weight , TWi   (in kilogram) and a value  TVi .

NOW  Nitori  is Ready to buy souvenirs. For convenience, , Nitori  numbered The tourist attraction from 1 to  N . At the beginning , Nitori  is located at the tourist attraction  X  and there are& nbsp M  roads Connect some pairs of tourist attractions, and each road have a length  L . However, because  Youkai Mountain  is very steep, all roads is uni-directional. By the the-the-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.

Nitori have one bag and the maximal load is W kilogram. When there K is kilogram things in Nitori's bag, she needs to cost units energy for K walking one unit l Ength Road. Of course she doesn ' t want to waste too much energy, so please calculate the minimal cost of energy of Nitori whe n the value is maximal.

Notice: Nitori can buy souvenir at tourist attraction X , and she can stop at any tourist attraction. Also, there is no, different roads between the same, tourist attractions. Moreover, though the shop sells different souvenirs, it's still possible for the different kinds of souvenir has the SAM e weight or value.

Input

There is multiple test cases. For each test case:

The first line contains four numbers N (1 <= N <=)-The number of tourist attractions, M (1 <= c3/> <= 60000)-The number of roads, W (1 <= W <=)-The load of the bag and X (1 <= X & lt;= N )-The starting point ofNitori.

Then followed N by lines, each line contains the integers which means the shop on tourist attraction i sells the c2/> TVi and Things (1 <= TWi <= W, 1 <= TVi <= 10000).

Next, there is M lines, each line contains three numbers, Xi , Yi and Li , which means there is a one-way road From tourist attraction Xi to Yi , and the length is Li (1 <= Xi , Yi <= N, 1 <= Li <= 1 0000).

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
Test instructions
There is a direction-free graph, there are n points on the graph, each place has a occupy V space, the value of the goods, and now a person from a point, if the backpack has c weight of the goods, walk through the distance of K, will consume c*k of energy, then in the backpack capacity certain circumstances, What is the minimum amount of energy I need to get the most value?
Ideas:
First, the topology is sorted, according to the characteristics of its sorting, we just need to follow this order to complete the backpack can be
#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 sort {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++)//Compare the position of x with the optimal y position first, select the optimal first shift to 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++)//Complete 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],p                Ower[y][k-v[y]]); }} for (k = 0; k<=sum; k++)//Find 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%d%d%d", &n,&m,&sum,&st)) {for (i = 1; i<=n; i++) scanf ("%d%d", &amp        ; 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%d%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;}


Zoj3524:crazy Shopping (topological sort + full backpack)

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.