ZOJ3524:Crazy Shopping(拓扑排序+完全背包)
来源:互联网 发布:过山车大亨1 mac 编辑:程序博客网 时间:2024/06/01 09:42
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 ofNitori.
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 11 12 33 44 51 2 51 3 42 4 43 4 5
Sample Output
0
题意:
有一个有向无环图,图上有n个点,每个地点有一种占用V空间,价值为W的物品,现在一个人从某点出发,如果背包有C重量的物品,走过路程为K,会消耗C*K的体力,那么在背包容量一定的情况下,我要得到最大的价值需要消耗的最少体力是多少
思路:
先进行一次拓扑排序,根据其排序后的特点,我们只需要按照这个顺序进行完全背包就可以了
#include <stdio.h>#include <algorithm>#include <string.h>#include <vector>#include <math.h>using namespace std;struct node{ int next,dis;};vector<node> a[605];int n,m,sum,st;int v[605],w[605],root[605];int dp[605][2005],tp[605],tem[605],top,len;int power[605][2005],vis[605];int max_val,min_pow;void topoo()//拓扑排序{ int i,j; len = top = 0; for(i = 1; i<=n; i++) { if(!root[i]) tem[++top] = i; } while(top) { int k = tem[top--]; tp[++len] = k; int end = a[k].size(); for(i = 0; i<end; i++) { node ss = a[k][i]; root[ss.next]--; if(!root[ss.next]) tem[++top] = ss.next; } }}void solve(){ max_val = 0; int i,j,k; vis[st] = 1; for(i = 0; i<=sum; i++)//初始化 { power[st][i] = 0; if(i>=v[st]) dp[st][i] = max(dp[st][i],dp[st][i-v[st]]+w[st]); } max_val = dp[st][sum]; min_pow = 0; for(i = 1; i<=n; i++) { int x = tp[i]; if(!vis[x]) continue; int l = a[x].size(); for(j = 0; j<l; j++) { node sk = a[x][j]; int y = sk.next; vis[y] = 1; for(k = 0; k<=sum; k++)//先把x位置的情况与y位置最优的情况比较,选最优的先转移到y位置 { if(dp[x][k]>dp[y][k]) { dp[y][k] = dp[x][k]; power[y][k] = power[x][k]+sk.dis*k; } else if(dp[x][k]==dp[y][k]) { if(power[y][k] == -1) power[y][k] = power[x][k]+sk.dis*k; else power[y][k] = min(power[y][k],power[x][k]+sk.dis*k); } } for(k = v[y]; k<=sum; k++)//进行完全背包 { if(dp[y][k]<dp[y][k-v[y]]+w[y]) { dp[y][k] = dp[y][k-v[y]]+w[y]; power[y][k] = power[y][k-v[y]]; } else if(dp[y][k]==dp[y][k-v[y]]+w[y]) { power[y][k] = min(power[y][k],power[y][k-v[y]]); } } for(k = 0; k<=sum; k++)//寻找答案 { if(dp[y][k]>max_val || (dp[y][k]==max_val && power[y][k]<min_pow)) { max_val = dp[y][k]; min_pow = power[y][k]; } } } }}int main(){ int i,j,k; while(~scanf("%d%d%d%d",&n,&m,&sum,&st)) { for(i = 1; i<=n; i++) scanf("%d%d",&v[i],&w[i]); memset(root,0,sizeof(root)); memset(dp,0,sizeof(dp)); memset(power,-1,sizeof(power)); memset(vis,0,sizeof(vis)); int x,y,z; for(i = 0; i<=n; i++) a[i].clear(); for(i = 0; i<m; i++) { scanf("%d%d%d",&x,&y,&z); root[y]++; node ss; ss.next = y; ss.dis = z; a[x].push_back(ss); } topoo(); solve(); printf("%d\n",min_pow); } return 0;}
- ZOJ3524:Crazy Shopping(拓扑排序+完全背包)
- Crazy Shopping(拓扑排序+完全背包)
- 【zoj3524】【Crazy Shopping】
- zoj 3524 Crazy Shopping 拓扑排序+完全背包 ★★★☆
- ZOJ3524Crazy Shopping(完全背包+拓扑排序)经典
- ZOJ 3524 Crazy Shopping(拓扑+DP)
- poj 1170 Shopping Offers 完全背包
- hdu 5325 Crazy Bobo 拓扑排序
- Hdu 5325 Crazy Bobo (拓扑排序)
- Codeforces 284E Coin Troubles【思维+拓扑排序+完全背包】好题!
- 多维完全背包 USACO 3.3.2 Shopping Offers
- poj 1170 Shopping Offers-动态规划-完全背包
- POJ 1170 Shopping Offers 状态压缩dp+完全背包
- 商店购物 Shopping Offers(完全背包问题+结构体使用)
- 【Codeforces 283 C. Coin Troubles】+ 拓扑序 + 完全背包
- POJ 1170 Shopping Offers 6进制压缩状态+完全背包
- hdu 5325 Crazy Bobo (拓扑排序代替树的遍历解决爆栈)
- 多校第三场 1010 hdu 5325 Crazy Bobo(拓扑排序+树形dp)
- Boost 库 Enable_shared_from_this 实现原理分析
- 学习笔记3-Bootstrap CSS 【转载整理->原创】
- Python模块学习 ---- zlib 数据压缩
- 开发swift软件的快速方法--1024swift
- linux下make命令
- ZOJ3524:Crazy Shopping(拓扑排序+完全背包)
- boost::shared_ptr的使用方法
- Substring with Concatenation of All Words
- (一)U-Boot启动过程--详细版的完全分析
- 【JS】简易倒计时
- python的tarfile模块实例 python把文件夹压缩成tar格式文件的例子
- plan1
- 从C开始_结构体_把结构信息传递到函数中1
- C++5 数组