ZOJ 3524 Crazy Shopping
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
*/
ZOJ 3524 Crazy Shopping的更多相关文章
- Crazy Shopping(拓扑排序+完全背包)
Crazy Shopping(拓扑排序+完全背包) Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C. ...
- zoj 3524(拓扑排序+多重背包)(好题)
http://blog.csdn.net/woshi250hua/article/details/7824773 题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念 ...
- DP专题·三(01背包+完全背包)
1.hdu 2126 Buy the souvenirs 题意:给出若干个纪念品的价格,求在能购买的纪念品的数目最大的情况下的购买方案. 思路:01背包+记录方案. #include<iostr ...
- zoj 1730 / poj 1455 Crazy Tea Party
这阵子都没怎么写代码,由于开学,忙于各种琐碎的事情,现在静下来了开始跟着暑假的节奏刷题了. 这道题一开是没看清题目-在寝室刷题就是效率不高... 后来才知道,题目意思是,一个环形序列,1minute可 ...
- ZOJ Problem Set - 1730 Crazy Tea Party
#include<cstdio> int main(){ int T,n; scanf("%d",&T); while(T--){ scanf("%d ...
- 【转载】图论 500题——主要为hdu/poj/zoj
转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并 ...
- ZOJ 3435 Ideal Puzzle Bobble
ZOJ Problem Set - 3435 Ideal Puzzle Bobble Time Limit: 2 Seconds Memory Limit: 65536 KB Have yo ...
- ZOJ People Counting
第十三届浙江省大学生程序设计竞赛 I 题, 一道模拟题. ZOJ 3944http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=394 ...
- ZOJ 3686 A Simple Tree Problem
A Simple Tree Problem Time Limit: 3 Seconds Memory Limit: 65536 KB Given a rooted tree, each no ...
随机推荐
- Redis数据持久化的两种方式RDB和AOF
由于Redis的数据都存放在内存中,如果没有配置持久化,redis重启后数据就全丢失了,于是需要开启redis的持久化功能,将数据保存到磁 盘上,当redis重启后,可以从磁盘中恢复数据.redis提 ...
- HTML标签和文档结构
HTML标签与文档结构 HTML作为一门标记语言,是通过各种各样的标签来标记网页内容的.我们学习HTML主要就是学习的HTML标签. 那什么是标签呢? #1.在HTML中规定标签使用英文的的尖括号即` ...
- HNU 12886 Cracking the Safe 二十四点的判断
经典的一个题,今天竟然写跪了…… 题意: 给你4个数字,让你判断是否能通过四则运算和括号,凑成24点. 思路: 暴力枚举运算顺序和运算符. 代码: #include <iostream> ...
- hdu 1423 最长公共递增子序列 LCIS
最长公共上升子序列(LCIS)的O(n^2)算法 预备知识:动态规划的基本思想,LCS,LIS. 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列). 首先我们可以看到,这个问题具有相 ...
- 石子合并 (区间DP)
一.试题在一个园形操场的四周摆放N堆石子(N≤100),现要将石子有次序地合并成一堆.规定每次仅仅能选相邻的两堆合并成新的一堆,并将新的一堆的石子数.记为该次合并的得分.编一程序.由文件读入堆数N及每 ...
- 剑指Offer面试题27(Java版):二叉搜索树与双向链表
题目:输入一颗二叉搜索树,将该二叉搜索树转换成一个排序的双向链表.要求不能创建新的结点.仅仅能调整树中结点指针的指向. 比方例如以下图中的二叉搜索树.则输出转换之后的排序双向链表为: 在二叉树中,每一 ...
- To new is C++; To malloc is C; To mix them is sin (混淆C++中的new和C中的malloc是一种犯罪)
Introduction One of the most common questions that get asked during interviews for C++ programmers i ...
- C++中explicit关键字作用
explicit是c++中不太常用的一个关键字,其作用是用于修饰构造函数,告诉编译器定义对象时不做隐式转换. 举例说明: include <iostream> include <st ...
- Huawei配置两台交换机堆叠示例
配置两台交换机堆叠示例(先配置后连线方式,推荐) 一.基本概念 在堆叠中,有以下一些基本概念,如图1所示.图1 堆叠基本概念示意图 1. 角色堆叠中的单台交换机称为成员交换机,按照功能不同可以分为以下 ...
- DELL T110 II 系统安装总结
DELL T110 II 系统安装总结 1.RAID制作:https://jingyan.baidu.com/article/a3aad71ac4ce98b1fb0096bc.html 2.系统安装 ...