Crazy Shopping(拓扑排序+完全背包)
Crazy Shopping(拓扑排序+完全背包)
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 1
1 1
2 3
3 4
4 5
1 2 5
1 3 4
2 4 4
3 4 5
Sample Output
0
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4439
题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念品重量为cost[i],价值为val[i]。n座山形成一张有m条边的有向图,某山道某某山都有距离dist[i]。主角xx从st点出发,背着个容量为M的背包,想要收集最多的价值。但是主角体弱多病要顾及身体,每次背着重量为wi从某山走到某某山就要耗费dist[i]*wi的能量。最后能价值最多时最少的能量耗费为多少?
解题思路:看上去像神题,但仔细分析就是一个拓扑排序+完全背包,不过细节着实有点蛋痛。我最开始是想先再每个点做一次完全背包,这样转移的时候直接转移就好了。但是这样似乎很难实现。
设dp[i][j]到达i点背包里装容量为j的最大价值,power[i][j]表示价值最大时的最小耗费。按上一段说的一开始就转移的话,那么dp[i][j]都会被更新,此时power[i][j]应该是0,因为不知道前面跑了几万几千里。但是这样并不靠谱,先不说从st能不能到i,就说能到达的时候,我们怎么得到一个和dp[i][j]一样的值,那么此时power应该更新为多少?还有dp[i][j]要怎么更新?
上面是我的一次失败的尝试,但对后面的分析也有帮助,我只要先进行拓扑排序,然后利用拓扑序向后转移,转移到下一个点就做一次完全背包,一个点可能从很多歌点转移来,优先更新dp[i][j]然后更新power[i][j],转移得到的dp[i][j]和power[i][j]都是从前序节点转移而来,如果.而每次转移的时候还必须对下一个节点进行标记,表示能否从st点而来。
具体的转移方程和完全背包很像,只是在价值一样的时候要依据power进行转移,实现见代码。
测试数据:
4 4 10 1
1 1
2 3
3 4
4 5
1 2 5
1 3 4
2 4 4
3 4 5
4 4 10 1
3 5
2 2
3 3
1 1
1 2 5
1 3 10
2 4 4
3 4 5
4 4 15 1
4 7
2 3
3 3
1 1
1 2 5
1 3 10
2 4 0
3 4 5
4 4 0 1
4 7
2 3
3 3
1 1
1 2 5
1 3 10
2 4 0
3 4 5
4 4 15 4
4 7
2 3
3 3
1 1
1 2 5
1 3 10
2 4 0
3 4 5
4 4 15 2
4 7
2 3
3 3
1 1
1 2 5
1 3 10
2 4 0
3 4 5
4 3 15 1
2 3
3 3
2 1
1 1
1 3 0
2 3 5
3 4 0
4 3 15 1
2 3
3 3
2 1
1 1
1 3 0
2 3 5
3 4 10
Output:
15 0
16 81
25 60
0 0
15 0
22 0
22 0
22 140
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<vector>
using namespace std;
#define MIN 710
#define MAX 2100
#define mem(a,x) memset((a),(x),sizeof((a)))
int n,m,road,x;
int cost[MIN],val[MIN],flag[MIN];
int st[MIN],top,cnt[MIN],real[MIN],Index;
int dp[MIN][MAX],power[MIN][MAX],ans,dist;
struct node
{
int v,len;
} cur; vector<node> mmap[MIN]; void tuopu()
{
for(int i=; i<=n; i++)
{
if(cnt[i]==)
st[++top]=i;
while(top!=)
{
int v=st[top--];
real[++Index]=v;
int size = mmap[v].size();
for(int j=; j<size; j++)
{
cur=mmap[v][j];
cnt[cur.v]--;
if(cnt[cur.v]==)
st[++top]=cur.v;
}
}
}
}
void Solve_AC()
{ int i,j,k,s,v,size,tp; for (j = ; j <= m; ++j)
{
//相当于初始化
power[x][j] = ;
if (j >= cost[x])
dp[x][j] = max(dp[x][j],dp[x][j-cost[x]]+val[x]);
if (dp[x][j] > ans)
ans = dp[x][j],dist = ;
//printf("%d %lld ans%lld\n",j,dp[x][j],ans);
} flag[x] = ;
for (i = ; i <= n; ++i)
{ v = real[i];
if (flag[v] == ) continue; //flag为0 ,表示不可达
size = mmap[v].size();
for (s = ; s < size; ++s)
{ cur = mmap[v][s];
tp = cur.v,flag[tp] = ; //可达,tp为下一个节点号
for (j = ; j <= m; ++j)
{ if (dp[tp][j] < dp[v][j]) //优先根据dp[tp][j]进行转移
{ dp[tp][j] = dp[v][j];
power[tp][j] = power[v][j] + cur.len * j;
}
else if (dp[tp][j] == dp[v][j]) //当dp[tp][j]和dp[v][j]相等才根据power[i][j]转移
{ if (power[tp][j] == -) //第一次到达tp点
power[tp][j] = power[v][j]+cur.len * j;
else
power[tp][j] = min(power[tp][j],power[v][j] + cur.len * j);
}
//没有这个if就会出现后面的耗费比前面多,但实际获得的价值都一样
if (j && dp[tp][j] == dp[tp][j-])
power[tp][j] = min(power[tp][j],power[tp][j-]);
} for (j = cost[tp]; j <= m; ++j)
{
//完全背包
if (dp[tp][j] < dp[tp][j-cost[tp]]+val[tp])
{ dp[tp][j] = dp[tp][j-cost[tp]] + val[tp];
power[tp][j] = power[tp][j-cost[tp]];
}
else if(dp[tp][j] == dp[tp][j-cost[tp]]+val[tp])
power[tp][j] = min(power[tp][j],power[tp][j-cost[tp]]);
} for (j = ; j <= m; ++j)
{
//更新答案
if (dp[tp][j] > ans)
ans = dp[tp][j],dist = power[tp][j];
else if (dp[tp][j] == ans)
dist = min(dist,power[tp][j]);
}
//printf("cur %d:\n",cur.v),Debug_InPut();
}
}
} int main()
{
int i,j,k,a,b,c;
while(scanf("%d%d%d%d",&n,&road,&m,&x)!=EOF)
{
Index=;
top=ans=dist=;
mem(dp,);
mem(cnt,);
mem(flag,);
mem(power,-);
for(i=; i<=n; i++)
{
mmap[i].clear();
}
for(i=; i<=n; i++)
{
scanf("%d%d",&cost[i],&val[i]);
}
for(i=; i<=road ; i++)
{
scanf("%d%d%d",&a,&b,&c);
cnt[b]++;
cur.v=b;
cur.len=c;
mmap[a].push_back(cur);
}
tuopu();
Solve_AC();
printf("%d\n",dist);
}
return ;
}
Crazy Shopping(拓扑排序+完全背包)的更多相关文章
- zoj 3524(拓扑排序+多重背包)(好题)
http://blog.csdn.net/woshi250hua/article/details/7824773 题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念 ...
- ZOJ-3524 拓扑排序+完全背包(好题)
题意:在一个DAG上,主角初始有W钱起点在s点,每个点有一个代价wi和价值vi,主角从起点走到某一点不能回头走,一路上可以买东西(一个点的东西可以买无限次),且体力消耗为身上负重*路径长度.主角可以在 ...
- E - Ingredients 拓扑排序+01背包
题源:https://codeforces.com/gym/101635/attachments 题意: n行,每行给定字符串s1,s2,s3代表一些菜谱名.s2和s3是煮成是的必要条件,然后给出c和 ...
- ZOJ 3524 Crazy Shopping
Crazy Shopping Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...
- hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- 有向图和拓扑排序Java实现
package practice; import java.util.ArrayDeque; import java.util.Iterator; import java.util.Stack; pu ...
- HDU 4324 Triangle LOVE (拓扑排序)
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Tot ...
- HDU4324 Triangle LOVE【拓扑排序】
Triangle LOVE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) To ...
- 算法与数据结构(七) AOV网的拓扑排序
今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...
随机推荐
- 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑
[源码下载] 背水一战 Windows 10 (61) - 控件(媒体类): InkCanvas 涂鸦编辑 作者:webabcd 介绍背水一战 Windows 10 之 控件(媒体类) InkCanv ...
- 【转】javascript深入理解js闭包
原文:http://www.jb51.net/article/24101.htm 闭包(closure)是Javascript语言的一个难点,也是它的特色,很多高级应用都要依靠闭包实现. 一.变量的作 ...
- 【BZOJ2001】 [Hnoi2010]City 城市建设
BZOJ2001 [Hnoi2010]City 城市建设 Solution 我们考虑一下这个东西怎么求解? 思考无果...... 咦? 好像可以离线cdq,每一次判断一下如果这条边如果不选就直接删除, ...
- C# 中 DataTable转换成IList
在用C#作开发的时候经常要把DataTable转换成IList:操作DataTable比较麻烦,把DataTable转换成IList,以对象实体作为IList的元素,操作起来就非常方便. 注意:实体的 ...
- 微信小程序之模版的使用(template)
WXML提供模板(template),可以在模板中定义代码片段,然后在不同的地方调用. 分为两部分,定义模板和使用模板 (1).定义模板:使用 name 属性,作为模板的名字.然后在<templ ...
- 安卓APP简单后端的搭建
写在前面: 此教程没有用到后端框架.只是单纯用servlet做一个例子,如果是学框架可以不用往下看了 本文适合哪些人:懂java的,会写android单机程序,懂得用HTTPClient等发送请求解析 ...
- 不用函数库求一个数的平方根 (java版)
一.题目 编写程序求一个整数的平方根,精确到小数点后三位 二.解法 1) 采用 牛顿迭代法. 2)牛顿迭代法简介 假设方程 在 附近有一个根,那么用以下迭代式子: ...
- 图片训练:使用卷积神经网络(CNN)识别手写数字
这篇文章中,我们将使用CNN构建一个Tensorflow.js模型来分辨手写的数字.首先,我们通过使之“查看”数以千计的数字图片以及他们对应的标识来训练分辨器.然后我们再通过此模型从未“见到”过的测试 ...
- Android的Fragment的第一种声明方式
Android的Frangment的第一种声明方式 实际效果图如下: 项目结构图如下: fragment1: package com.demo.fragementfirst; import andro ...
- Spring Boot 静态资源映射与上传文件路由配置
默认静态资源映射目录 默认映射路径 在平常的 web 开发中,避免不了需要访问静态资源,如常规的样式,JS,图片,上传文件等;Spring Boot 默认配置对静态资源映射提供了如下路径的映射 /st ...