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 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 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, 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

题目链接: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(拓扑排序+完全背包)的更多相关文章

  1. zoj 3524(拓扑排序+多重背包)(好题)

    http://blog.csdn.net/woshi250hua/article/details/7824773 题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念 ...

  2. ZOJ-3524 拓扑排序+完全背包(好题)

    题意:在一个DAG上,主角初始有W钱起点在s点,每个点有一个代价wi和价值vi,主角从起点走到某一点不能回头走,一路上可以买东西(一个点的东西可以买无限次),且体力消耗为身上负重*路径长度.主角可以在 ...

  3. E - Ingredients 拓扑排序+01背包

    题源:https://codeforces.com/gym/101635/attachments 题意: n行,每行给定字符串s1,s2,s3代表一些菜谱名.s2和s3是煮成是的必要条件,然后给出c和 ...

  4. ZOJ 3524 Crazy Shopping

    Crazy Shopping Time Limit: 3000ms Memory Limit: 65536KB This problem will be judged on ZJU. Original ...

  5. hdoj 4324 Triangle LOVE【拓扑排序判断是否存在环】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  6. 有向图和拓扑排序Java实现

    package practice; import java.util.ArrayDeque; import java.util.Iterator; import java.util.Stack; pu ...

  7. HDU 4324 Triangle LOVE (拓扑排序)

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tot ...

  8. HDU4324 Triangle LOVE【拓扑排序】

    Triangle LOVE Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  9. 算法与数据结构(七) AOV网的拓扑排序

    今天博客的内容依然与图有关,今天博客的主题是关于拓扑排序的.拓扑排序是基于AOV网的,关于AOV网的概念,我想引用下方这句话来介绍: AOV网:在现代化管理中,人们常用有向图来描述和分析一项工程的计划 ...

随机推荐

  1. Python中super()的用法

    参考链接:https://www.cnblogs.com/shengulong/p/7892266.html super 是用来解决多重继承问题的,直接用类名调用父类方法在使用单继承的时候没问题,但是 ...

  2. C# byte array 跟 string 互转

    用 System.Text.Encoding.Default.GetString() 转换时,byte array 中大于 127 的数据转 string 时会出问题. 把这里的 Default 换成 ...

  3. GoLang学习之变量定义和初始化

    变量命名原则 go语言的变量名有字母数字和下划线组成,首字母不能为数字,但是字母不仅仅只限于英文字母,所有的UTF-8字符都是可以的. 变量声明和初始化方式 使用var关键字 var a int = ...

  4. underscore.js源码研究(4)

    概述 很早就想研究underscore源码了,虽然underscore.js这个库有些过时了,但是我还是想学习一下库的架构,函数式编程以及常用方法的编写这些方面的内容,又恰好没什么其它要研究的了,所以 ...

  5. Unable to preventDefault inside passive event listener due to target being treated as passive?

    使用滚动时候,新版google浏览器,会弹出如下的警告. 解决方法,可以加上* { touch-action: none; } 这句样式去掉. 其原因:https://developers.googl ...

  6. Swift5 语言参考(九) 泛型和参数

    本章介绍泛型类型,函数和初始值设定项的参数和参数.声明泛型类型,函数,下标或初始化程序时,可以指定泛型类型,函数或初始化程序可以使用的类型参数.当创建泛型类型的实例或调用泛型函数或初始化程序时,这些类 ...

  7. JavaScript(JS)简介

    历史背景介绍 (Brendan Eich)在其Netscape Navigator 2.0产品中开发出一套livescript的脚本语言.Sun和Netscape共同完成.后改名叫Javascript ...

  8. [Spring]@Autowired,@Required,@Qualifier注解

    @Required注解 @Required注解用于setter方法,表明这个属性是必要的,不可少的,必须注入值 假设有个测试类,里面有name和password两个属性 我给两个属性的setter方法 ...

  9. VirtualBox下Ubuntu虚拟机共享文件夹、自动挂载相关配置

    VirtualBox Ubuntu 共享文件夹的自动挂载: 一些基本的操作步骤: 首先,我们想要实现VirtualBox虚拟机与windows系统之间的通信,我们必须也应该正确的安装虚拟机系统. 其次 ...

  10. dotnetcore+vue+elementUI 前后端分离 三(前端篇)

    说明: 本项目使用了 mysql employees数据库,使用了vue + axois + element UI 2.0 ,演示了 单页程序 架构 ,vue router 的使用,axois 使用, ...