ZOJ3524:Crazy Shopping(拓撲排序+完全背包),zoj3524shopping

來源:互聯網
上載者:User

ZOJ3524:Crazy Shopping(拓撲排序+完全背包),zoj3524shopping

Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C.P), Kawashiro Nitori decides to 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 are unable to be close to the mountain. But because of the financial crisis, something have changed. For example, Youkai Mountain becomes available for tourists.

On the mountain there are N tourist 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. Nitori also knows that each 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 M roads connect some pairs of tourist attractions, and each road has a length L. However, because Youkai Mountain is 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.

Nitori has one bag and the maximal load is W kilogram. When there are K kilogram things in Nitori's bag, she needs to cost K units 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 energy of Nitori when the value is maximal.

Notice: Nitori can buy souvenir at tourist attraction X, 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 numbers N (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 and X (1 <= X <= N) - the starting point ofNitori.

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

Next, there are 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

題意:
有一個有向非循環圖,圖上有n個點,每個地點有一種佔用V空間,價值為W的物品,現在一個人從某點出發,如果背包有C重量的物品,走過路程為K,會消耗C*K的體力,那麼在背包容量一定的情況下,我要得到最大的價值需要消耗的最少體力是多少

思路:
先進行一次拓撲排序,根據其排序後的特點,我們只需要按照這個順序進行完全背包就可以了


#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()//拓撲排序{    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++)//初始化    {        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++)//先把x位置的情況與y位置最優的情況比較,選最優的先轉移到y位置            {                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++)//進行完全背包            {                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++)//尋找答案            {                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",&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;}


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.