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. TIME定时器

    一.定时器分类 STM32F1 系列中,除了互联型的产品,共有 8 个定时器,分为基本定时器,通用定时器和高级定时器.基本定时器 TIM6 和 TIM7 是一个 16 位的只能向上计数的定时器,只能定 ...

  2. 题解 CF1037D 【Valid BFS?】

    不管怎么说,这都不是道紫题吧... 这里采用的思想有点类似轻重链剖分. 我们按照每个节点在序列里面出现的顺序,把每一个节点连出去的边都排一个序. 这样(如果序列没错)肯定会按照序列的方式遍历完全图. ...

  3. Java基础学习总结(15)——java读取properties文件总结

    一.java读取properties文件总结 在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResource ...

  4. mysql通过DATE_FORMAT将错误数据恢复

    因为如今新开发项目,同事造数据的时候,将时间类型格式造成"20150708".可是实际希望的数据格式是:"2015-07-08" . 数据库使用的是mysql. ...

  5. 动态网页爬取样例(WebCollector+selenium+phantomjs)

    目标:动态网页爬取 说明:这里的动态网页指几种可能:1)须要用户交互,如常见的登录操作:2)网页通过JS / AJAX动态生成.如一个html里有<div id="test" ...

  6. js实现小时钟,js中Date对象的使用?

    介绍一下js中Date对象的使用 dateObj = new Date() dateObj = new Date(dateValue) dateObj = new Date(year,month,da ...

  7. vim 基础学习之文件跳转

    1. ''-当前文件上次跳转之前的位置2. '.-当前文件上次修改的位置,只要是发生了可能导致变化的命令操作就会被标记,哪怕实际结果没有变化3. '^-当前文件上次插入的位置,只要是发生了插入操作命令 ...

  8. DispatcherServlet 前置控制器

    1.DispatcherServlet作用 DispatcherServlet是前端控制器设计模式的实现,提供Spring Web MVC的集中访问点,而且负责职责的分派,而且与Spring IoC容 ...

  9. 打开文件对话框在xp和win7上的实现文件任意多选

    作者:朱金灿 来源:http://blog.csdn.net/clever101 在xp系统上进行文件多选,实际上其文件字符串数组的缓冲区是有限,并不能支持选择任意多个文件,为此以前我还写过一篇文章: ...

  10. SQL SERVER 新增表、新增字段、修改字段 判断表是否存在

    // 新增之前判断表是否存在 IF NOT EXISTS (SELECT NAME FROM SYSOBJECTS WHERE ID = OBJECT_ID('tb_MessageHistory')) ...