首页 > 代码库 > ZOJ3524:Crazy Shopping(拓扑排序+完全背包)

ZOJ3524:Crazy Shopping(拓扑排序+完全背包)

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.

TH_CuteNitori.jpg

Kawashiro Nitori is a very shy kappa (a type of water sprite that live in rivers) and she lives on Youkai MountainYoukai 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, XiYi 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 1
1 1
2 3
3 4
4 5
1 2 5
1 3 4
2 4 4
3 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;
}


ZOJ3524:Crazy Shopping(拓扑排序+完全背包)