zoj 3524(拓扑排序+多重背包)(好题)
http://blog.csdn.net/woshi250hua/article/details/7824773
题目大意:从前有n座山,山里都有一座庙,庙里都有一个老和尚,老和尚专送纪念品,每个纪念品重量为cost[i],价值为val[i]。n座山形成一张有m条边的有向图,某山道某某山都有距离dist[i]。主角xx从st点出发,背着个容量为M的背包,想要收集最多的价值。但是主角体弱多病要顾及身体,每次背着重量为wi从某山走到某某山就要耗费dist[i]*wi的能量。最后能价值最多时最少的能量耗费为多少?
写下我的理解吧
首先我们要找到所有从x出发的通路,这个通过拓扑排序和标记来做到。
然后在这些通路中找到价值最大的:
在每个点通过之前传递的dp信息,用多重背包找出当前点价值最大,价值相等时找出精力最小
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <cctype>
#include <vector>
#include <iterator>
#include <set>
#include <map>
#include <sstream>
using namespace std; #define mem(a,b) memset(a,b,sizeof(a))
#define pf printf
#define sf scanf
#define spf sprintf
#define pb push_back
#define debug printf("!\n")
#define MAXN 1010
#define MAX(a,b) a>b?a:b
#define blank pf("\n")
#define LL long long
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pqueue priority_queue
#define INF 0x3f3f3f3f struct node
{
int v,len;
}cur; vector<node> mmap[]; int n,m,w,x; int tw[],tv[],cnt[]; int st[],result[],flag[]; long long dp[][],power[][],ans,dist; void ptopo()
{
for(int i =;i<=n;i++) pf("%d ",result[i]);
blank;
} void Debug_InPut() { for (int i = ; i <= n; ++i)
for (int j = ; j <= w; ++j)
pf("(%lld %lld)%c",dp[i][j],power[i][j],j==w?'\n':' ');
} void init()
{
ans = ;
dist = ;
memset(dp,,sizeof(dp));
memset(cnt,,sizeof(cnt));
memset(flag,,sizeof(flag));
memset(power,-,sizeof(power));
for(int i = ; i <= n; ++i)
mmap[i].clear();
} void topo()
{
int i,j;
int tot = -,index=;
for(i=;i<=n;i++)
{
if(cnt[i]==) st[++tot] = i;
//pf("%d ",st[tot]);
}
//blank;
while(tot>=)
{
int xx = st[tot--];
result[++index] = xx;
int s = mmap[xx].size();
//pf("tot%d\n",tot);
for(j=;j<s;j++)
{
cur = mmap[xx][j];
int y = cur.v;
//pf("xx%d y%d ",xx,y);
cnt[y]--;
if(cnt[y]==)
{
st[++tot] = y;
//pf("tot%d\n",tot);
//pf("y%d ",y);
}
}
}
//ptopo();
} void solve()
{
int i,j,k;
for(i=;i<=w;i++)
{
power[x][i] = ;
if(i>=tw[x])
{
dp[x][i] = max(dp[x][i],dp[x][i-tw[x]]+tv[x]);
}
if (dp[x][i] > ans)
ans = dp[x][i],dist = ;
} flag[x] = ;
for(i=;i<=n;i++)
{
int xx = result[i];
if(flag[xx]==) continue;
int s = mmap[xx].size();
for(j=;j<s;j++)
{
cur = mmap[xx][j];
int tp = cur.v;
flag[tp] = ;
for(k=;k<=w;k++)
{
if(dp[tp][k] < dp[xx][k])
{
dp[tp][k] = dp[xx][k];
power[tp][k] = power[xx][k] + cur.len*k;
}
else if(dp[tp][k] == dp[xx][k])
{
if(power[tp][k] == -)
{
power[tp][k] = power[xx][k]+cur.len * k;
}
else
power[tp][k] = min(power[xx][k] + cur.len*k ,power[tp][k]);
}
} for (k = tw[tp]; k <= w; ++k) {
//完全背包
if (dp[tp][k] < dp[tp][k-tw[tp]]+tv[tp]) {
dp[tp][k] = dp[tp][k-tw[tp]] + tv[tp];
power[tp][k] = power[tp][k-tw[tp]];
}
else if(dp[tp][k] == dp[tp][k-tw[tp]]+tv[tp])
power[tp][k] = min(power[tp][k],power[tp][k-tw[tp]]);
} for (k = ; k <= w; ++k) {
//更新答案
if (dp[tp][k] > ans)
ans = dp[tp][k],dist = power[tp][k];
else if (dp[tp][k] == ans)
dist = min(dist,power[tp][k]);
}
//pf("cur %d:\n",cur.v),Debug_InPut();
}
}
} int main()
{
int i,j;
while(~sf("%d%d%d%d",&n,&m,&w,&x))
{
init(); for(i=;i<=n;i++) sf("%d%d",&tw[i],&tv[i]); for(i=;i<m;i++)
{
int x,y,l;
sf("%d%d%d",&x,&y,&l);
cur.v = y;
cur.len = l;
cnt[y]++;
mmap[x].pb(cur);
}
topo();
solve();
//pf("ans %lld %lld\n",ans,dist);
printf("%lld\n",dist);
}
return ;
}
zoj 3524(拓扑排序+多重背包)(好题)的更多相关文章
- Crazy Shopping(拓扑排序+完全背包)
Crazy Shopping(拓扑排序+完全背包) Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C. ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
- HDU 2191 珍惜现在,感恩生活(多重背包模板题)
多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...
- nyoj 546——Divideing Jewels——————【dp、多重背包板子题】
Divideing Jewels 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Mary and Rose own a collection of jewells. ...
- ZOJ-3524 拓扑排序+完全背包(好题)
题意:在一个DAG上,主角初始有W钱起点在s点,每个点有一个代价wi和价值vi,主角从起点走到某一点不能回头走,一路上可以买东西(一个点的东西可以买无限次),且体力消耗为身上负重*路径长度.主角可以在 ...
- ZOJ 4124 拓扑排序+思维dfs
ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...
- E - Ingredients 拓扑排序+01背包
题源:https://codeforces.com/gym/101635/attachments 题意: n行,每行给定字符串s1,s2,s3代表一些菜谱名.s2和s3是煮成是的必要条件,然后给出c和 ...
- HDU 3342 -- Legal or Not【裸拓扑排序 &&水题 && 邻接表实现】
Legal or Not Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题
Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...
随机推荐
- 淘宝内部分享:MySQL & MariaDB性能优化 【转】
MySQL· 5.7优化·Metadata Lock子系统的优化 背景 引入MDL锁的目的,最初是为了解决著名的bug#989,在MySQL 5.1及之前的版本,事务执行过程中并不维护涉及到的所有表的 ...
- redis删除指定前缀的缓存
redis作为缓存服务器为MySQL数据库提供较高的防御性,对于一些数据的查询可以直接从缓存中可以进行查询. 但是,某些情况下,我们需要清除缓存. 以下场景: 公司经常做活动,每个活动都存在大量的数据 ...
- xshell连接centos虚拟机的几点注意
我家用电脑使用联通的宽带,使用virtualbox装了centos6,连接方式使用NAT网络,还有一个是网络地址转换(NAT),不清楚区别是什么,使用xshell连接 当使用cd /etc/sysco ...
- js中一切都是对象
<script> function cat(){} var cat = new cat(); console.log(cat.constructor) console.log(typeof ...
- I01-通过查询资料库方式来监控Informatica调度情况
--登陆INFA资料库,运行下面的SQL --想要更加个性化查询的话注意看SQL倒数第二第三行的备注 SELECT RUN_DATE, START_TIME , END_TIME, FOLIDER , ...
- java中引用
java中引用分为,强,弱,虚,软 (1)强引用 使用最普遍的引用.如果一个对象具有强引用,它绝对不会被gc回收.如果内存空间不足了,gc宁愿抛出OutOfMemoryError,也不是会回收具有强引 ...
- ssh协议git利用ss代理
前言 不知道ss为何物的绕道 求帐号的绕道 这里只是亲测 ssh协议下的git, 如何判断是什么协议出门左拐 判断是否需要代理 我遇到的问题是: ssh_exchange_identification ...
- urllib和urllib3
urllib库 urllib 是一个用来处理网络请求的python标准库,它包含4个模块. urllib.request---请求模块,用于发起网络请求 urllib.parse---解析模块,用于解 ...
- POJ_3264 Balanced Lineup 【线段树 + 区间查询】
一.题面 POJ3264 二.分析 典型的线段树的题,没有更新只有查询. 查询的时候需要注意的是,在判断区间是完全属于右子树还是左子树时,要根据建树的情况来选择,不然会出错.具体看代码 三.AC代码 ...
- maven 可执行jar maven-shade-plugin
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> ...