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. 用JS中的cookie实现商品的浏览记录

    最近在做一个购物车效果,为了实现商品的浏览记录效果可是让我百般周折,避免以后忘记特写此随笔与大家共享,希望博友们看后有所收获. 第一步:在一个公用的js文件下getCookie(“liulan”),c ...

  2. Unity经验之谈

    1.全屏与非全屏之间的切换 if (Input.GetMouseButtonDown(1)) { Screen.fullScreen = !Screen.fullScreen; } 2.Camera适 ...

  3. Python中的list,tuple,dict和set

    List list的创建与检索 Python内置的一种数据类型是列表:list.list是一种有序的集合,可以随时添加和删除其中的元素. 构造list非常简单,直接用 [ ] 把list的所有元素都括 ...

  4. 9 hbase源码系列(九)StoreFile存储格式

    hbase源码系列(九)StoreFile存储格式    从这一章开始要讲Region Server这块的了,但是在讲Region Server这块之前得讲一下StoreFile,否则后面的不好讲下去 ...

  5. ImageLoader的简单分析(二)

    在<ImageLoader的简单分析>这篇博客中对IImageLoader三大组件的创建过程以及三者之间的关系做了说明.同一时候文章的最后也简单的说明了一下ImageLoader是怎么通过 ...

  6. java 自己定义异常,记录日志简单说明!留着以后真接复制

    log4j 相关配制说明:http://blog.csdn.net/liangrui1988/article/details/17435139 自己定义异常 package org.rui.Excep ...

  7. Codeforces Educational Codeforces Round 8 A. Tennis Tournament

    大致题意: 网球比赛,n个參赛者,每场比赛每位选手b瓶水+裁判1瓶水,所有比赛每一个參赛者p条毛巾 每一轮比赛有2^k个人參加比赛(k为2^k<=n中k的最大值),下一轮晋级人数是本轮每场比赛的 ...

  8. 9.java 操作mongodb插入、读取、修改以及删除基础

    1 package mongodb; import java.net.UnknownHostException; import java.util.ArrayList; import java.uti ...

  9. C#中如何获得两个日期之间的天数差

    DateTime d1; DateTime d2; //自己去赋值吧 int days = (d1 - d2).Days;//天数差 label1.Text = "2012-1-1 15:3 ...

  10. XML结构,写到TreeView树上

    http://blog.csdn.net/ztzi321/article/details/44077563