Crazy Shopping
This problem will be judged on ZJU. Original ID: 3524
64-bit integer IO format: %lld Java class name: Main
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 Mountainbecomes 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 valueTVi.
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 theTWi 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 1
1 1
2 3
3 4
4 5
1 2 5
1 3 4
2 4 4
3 4 5
Sample Output
0
Hint
It's no hard to know that Nitori can buy all things at tourist attraction 2, so she cost 0 unit energy.
Author
#include <bits/stdc++.h>
using namespace std;
const int maxn = ;
struct arc {
int to,w,next;
arc(int x = ,int y = ,int z = -) {
to = x;
w = y;
next = z;
}
} e[];
int head[maxn],ind[maxn],tot,n,m,w,x;
int dp[maxn][],energy[maxn][];
int cw[maxn],cv[maxn];
bool done[maxn];
vector<int>sorted;
queue<int>q;
void add(int u,int v,int w) {
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
}
void topSort() {
while(!q.empty()) q.pop();
for(int i = ; i <= n; ++i)
if(!ind[i]) q.push(i);
while(!q.empty()) {
int u = q.front();
q.pop();
sorted.push_back(u);
for(int i = head[u]; ~i; i = e[i].next)
if(--ind[e[i].to] == ) q.push(e[i].to);
}
}
void solve() {
for(int i = ; i <= w; ++i) {
energy[x][i] = ;
if(i >= cw[x])
dp[x][i] = max(dp[x][i],dp[x][i - cw[x]] + cv[x]);
}
int maxV = dp[x][w],minW = ;
done[x] = true; for(int i = ; i < sorted.size(); ++i) {
if(!done[sorted[i]]) continue;
int u = sorted[i]; for(int j = head[u]; ~j; j = e[j].next) {
int v = e[j].to;
done[v] = true;
for(int k = ; k <= w; ++k) {
if(dp[v][k] < dp[u][k]) {
dp[v][k] = dp[u][k];
energy[v][k] = energy[u][k] + e[j].w*k;
} else if(dp[v][k] == dp[u][k]){
if(energy[v][k] == -) energy[v][k] = energy[u][k] + e[j].w*k;
else energy[v][k] = min(energy[v][k],energy[u][k] + e[j].w*k);
}
} for(int k = cw[v]; k <= w; ++k)
if(dp[v][k] < dp[v][k - cw[v]] + cv[v]){
dp[v][k] = dp[v][k - cw[v]] + cv[v];
energy[v][k] = energy[v][k - cw[v]];
}else if(dp[v][k] == dp[v][k - cw[v]] + cv[v])
energy[v][k] = min(energy[v][k],energy[v][k - cw[v]]); for(int k = ; k <= w; ++k)
if(dp[v][k] > maxV || dp[v][k] == maxV && energy[v][k] < minW){
maxV = dp[v][k];
minW = energy[v][k];
}
}
}
printf("%d\n",minW);
}
int main() {
int u,v,c;
while(~scanf("%d %d %d %d",&n,&m,&w,&x)) {
memset(head,-,sizeof head);
memset(ind,,sizeof ind);
memset(done,false,sizeof done);
sorted.clear();
memset(energy,-,sizeof energy);
memset(dp,,sizeof dp);
for(int i = ; i <= n; ++i)
scanf("%d %d",cw+i,cv+i);
for(int i = tot = ; i < m; ++i) {
scanf("%d %d %d",&u,&v,&c);
++ind[v];
add(u,v,c);
}
topSort();
solve();
}
return ;
}
/*
4 4 10 1
3 5
2 2
3 3
1 1
1 2 5
1 3 10
2 4 4
3 4 5
*/