Zoj3524crazy Shopping (Full backpack + topological sort) Classic

Source: Internet
Author: User

H-Crazy ShoppingTime limit:MS Memory Limit:65536KB 64bit IO Format:%lld & Amp %llu SubmitStatus

Description

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 this live in rivers) and she lives on Y Oukai Mountain. Youkai Mountain is a dangerous place full of Youkai, so normally humans was unable to being close to the MO Untain. 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. T O make the tourists feel more challenging (for example, to collect all kinds of souvenirs), each shop sells only one speci Fic kind of souvenir that can is not buy in 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 ofNitori when 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 &L t;= N )-The starting point of Nitori.

Then followed N by lines, each line contains the integers which means the shop on tourist attraction i sells the TWi 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 <= 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
Hint

It's no hard-know that Nitori can buy all things at tourist attraction 2, so she's cost 0 unit energy

Test instructions: In a plurality of forward sub-graphs, give a starting point, the person carrying a large volume of packaging items, from one point to another point, and the cost of physical strength: the weight of the goods in the bag * two points between the distance, each point has countless of the same goods, each commodity has weight and value, Ask how much it costs at the same time you get the maximum value.


At first I used a deep search + full backpack tle, and later looked all over the internet with a topological sort + full backpack, the following analysis of the advantages of using topological sorting:

Because the graph is a direction-free graph, then a vertex into the degree of 0, and the degree of 0 points must not be updated, so we just have to connect with the point update, you can no longer use that point, and it is so, you can layer of elimination point, each road must be used only once. If it is a deep search, a road may not only walk once, it will take a lot of time, this is the reason for the time-out.

#include <stdio.h> #include <vector> #include <string.h> #include <iostream>using namespace std ; #define LL Intconst int inf=999999999;struct node{ll Cost,value;}; struct sss{int u,s;}; Node Dp[602][2002];vector<sss>map[605];ll oder[605],top,in[605],n,m,w,x;node Node[605],ans;ll S[602][602];            void Init () {for (ll i=1;i<=n;i++) {for (ll j=0;j<=w;j++) Dp[i][j].value = 0;            dp[i][0].cost=0;            Map[i].clear ();        In[i] = 0;    }}void Tope_oder () {top=0;    for (int i=1;i<=n;i++) if (!in[i]) oder[++top]=i;    int k=1,v,u;        while (K<=top) {v=oder[k++];            for (int i=0;i<map[v].size (); i++) {u=map[v][i].u;            in[u]--;        if (!in[u]) oder[++top]=u;    }}}void Solve () {int vist[605]={0};    Vist[x]=1; for (int j=node[x].cost; j<=w; J + +) if (dp[x][j-node[x].cost].value!=-1) {dp[x][J].value=dp[x][j-node[x].cost].value+node[x].value;    dp[x][j].cost=0;        } for (int t=1;t<=top;t++) {int x=oder[t];        if (vist[x]==0) continue;        int len=map[x].size (), u,s;            for (int k=0;k<len; k++) {u = map[x][k].u;            s = map[x][k].s;            Vist[u]=1; for (ll w=0; w<=w; w++) if (dp[x][w].value!=-1) {if (Dp[u][w].value<dp[x][w].valu                    e) {dp[u][w].value=dp[x][w].value;                dp[u][w].cost=dp[x][w].cost+w*s;                    } else if (Dp[u][w].value==dp[x][w].value) if (dp[u][w].cost>dp[x][w].cost+w*s)            dp[u][w].cost=dp[x][w].cost+w*s;                } for (ll w=node[u].cost; w<=w;w++) if (dp[u][w-node[u].cost].value!=-1) {            if (Dp[u][w].value<dp[u][w-node[u].cost].value+node[u].value) {        Dp[u][w].value=dp[u][w-node[u].cost].value+node[u].value;                Dp[u][w].cost=dp[u][w-node[u].cost].cost; } else if (Dp[u][w].value==dp[u][w-node[u].cost].value+node[u].value) if (dp[u][w].cost            >dp[u][w-node[u].cost].cost) Dp[u][w].cost=dp[u][w-node[u].cost].cost;    }}} ans.value=0;    ans.cost=0;            For (ll I=1;i<=n, i++) if (Vist[i]) for (ll v=1;v<=w;v++) if (dp[i][v].value>=ans.value)                {if (ans.value<dp[i][v].value) ans.cost=dp[i][v].cost;                else if (dp[i][v].cost<ans.cost) ans.cost=dp[i][v].cost;            Ans.value=dp[i][v].value;    }}int Main () {SSS p; while (scanf ("%d%d%d%d", &n,&m,&w,&x) >0) {for (ll i=1;i<=n;i++) scanf ("%d%d", &amp ;        Node[i].cost,&node[i].value);        Init ();ll V,u,s;            while (m--) {scanf ("%d%d%d", &AMP;V,&AMP;P.U,&AMP;P.S);        Map[v].push_back (P), in[p.u]++;        } tope_oder ();        Solve ();    printf ("%d\n", ans.cost); }}


Zoj3524crazy Shopping (Full backpack + topological sort) Classic

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.