Crazy Shopping

Time Limit: 3000ms
Memory Limit: 65536KB

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 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 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, 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

Hint

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

 

Author

DAI, Longao
 
解题:。。拓扑排序后进行完全背包。。。
 
 #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
*/

ZOJ 3524 Crazy Shopping的更多相关文章

  1. Crazy Shopping(拓扑排序+完全背包)

    Crazy Shopping(拓扑排序+完全背包) Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C. ...

  2. zoj 3524(拓扑排序+多重背包)(好题)

    http://blog.csdn.net/woshi250hua/article/details/7824773 题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念 ...

  3. DP专题·三(01背包+完全背包)

    1.hdu 2126 Buy the souvenirs 题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案. 思路:01背包+记录方案. #include<iostr ...

  4. zoj 1730 / poj 1455 Crazy Tea Party

    这阵子都没怎么写代码,由于开学,忙于各种琐碎的事情,现在静下来了开始跟着暑假的节奏刷题了. 这道题一开是没看清题目-在寝室刷题就是效率不高... 后来才知道,题目意思是,一个环形序列,1minute可 ...

  5. ZOJ Problem Set - 1730 Crazy Tea Party

    #include<cstdio> int main(){ int T,n; scanf("%d",&T); while(T--){ scanf("%d ...

  6. 【转载】图论 500题——主要为hdu/poj/zoj

    转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...

  7. ZOJ 3435 Ideal Puzzle Bobble

    ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds      Memory Limit: 65536 KB Have yo ...

  8. ZOJ People Counting

    第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ  3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...

  9. ZOJ 3686 A Simple Tree Problem

    A Simple Tree Problem Time Limit: 3 Seconds      Memory Limit: 65536 KB Given a rooted tree, each no ...

随机推荐

  1. bzoj1922 [SDOI2010]大陆争霸 分层图

    问题描述 幻想历8012年5月12日深夜,斯普林·布拉泽降下神谕:“Trust me, earn eternal life.”克里斯军团士气大增.作为克里斯军团的主帅,你决定利用这一机会发动奇袭,一举 ...

  2. LNMP安装部署开源IP管理工具phpipam

    1.数据库 mariadb 安装 //依赖安装 yum install -y apr* autoconf automake bison bzip2 bzip2* compat* \ cpp curl ...

  3. tt

    Oracle报错处理 1.oem启动报错 解决方案:

  4. 【SICP练习】152 练习4.8

    练习4-8 原文 Exercise 4.8. "Named let" is a variant of let that has the form (let <var> ...

  5. 129.C++面试一百题(1-51)

  6. 开启Windows 7远程桌面功能的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 本设置方法同样适用用Vista和Windows Server 2008. 1.依次点击"开始"菜单 ...

  7. C++中的namespace详解

    原文链接:http://blog.csdn.net/yao_zhuang/article/details/1853625 namespace中文意思是命名空间或者叫名字空间,传统的C++只有一个全局的 ...

  8. .net开源CMS

    提起开源cms,大家第一想到的是php的cms,因为php开源的最早,也最为用户和站长们认可,随着各大cms系统的功能的不断完善和各式各样的开源cms的出现,.net和java的高端的cms系统也逐渐 ...

  9. 【Uva 1631】Locker

    [Link]: [Description] 有一个n(n≤1000)位密码锁,每位都是0-9,可以循环旋转.每次可以让1-3个相邻 数字同时往上或者往下转一格.例如,567890->567901 ...

  10. PatentTips - Virtual translation lookaside buffer

    BACKGROUND OF THE INVENTION A conventional virtual-machine monitor (VM monitor) typically runs on a ...