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 of Nitori.
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 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.
#include <iostream>
#include <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
vector<pair<int,int> > g[610];
const int maxn = 10010;
ll w[maxn];
ll v[maxn];
int n,m,C,st;
bool vis[650];
struct Node
{
ll val,cost;
} dp[605][2010];
int dfs(int now)
{
vis[now] = true;
for(int i=0;i<g[now].size();i++)
{
int u = g[now][i].first;
if(vis[u]) continue;
dfs(u);
}
for(int c=C;c>=0;c--)
{
dp[now][c].val =0, dp[now][c].cost = 0;
for(int i=0;i<g[now].size();i++)
{
int u = g[now][i].first;
int l = g[now][i].second;
if(dp[now][c].val < dp[u][c].val || (dp[now][c].val == dp[u][c].val && dp[now][c].cost > c*l + dp[u][c].cost))
{
dp[now][c].val = dp[u][c].val;
dp[now][c].cost = c*l + dp[u][c].cost;
}
}
if(c + w[now] <= C)
{
if(dp[now][c].val < dp[now][c+w[now]].val+v[now])
{
dp[now][c].val = dp[now][c+w[now]].val + v[now];
dp[now][c].cost = dp[now][c+w[now]].cost;
}
if(dp[now][c].val == dp[now][c+w[now]].val + v[now] && dp[now][c].cost > dp[now][c+w[now]].cost)
{
dp[now][c].cost = dp[now][c+w[now]].cost;
}
}
}
}
int main()
{
while(~scanf("%d%d%d%d",&n,&m,&C,&st))
{
for(int i=1;i<=n;i++)
{
scanf("%lld%lld",&w[i],&v[i]);
}
int a,b,c;
for(int i=0;i<n+2;i++)
g[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
g[a].push_back(make_pair(b,c));
}
int x = 0;
memset(vis,0,sizeof(vis));
memset(dp,0,sizeof(dp));
dfs(st);
printf("%lld\n",dp[st][0].cost);
}
return 0;
}