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 ;
}