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(拓扑排序+多重背包)(好题)的更多相关文章

  1. Crazy Shopping(拓扑排序+完全背包)

    Crazy Shopping(拓扑排序+完全背包) Because of the 90th anniversary of the Coherent & Cute Patchouli (C.C. ...

  2. hdu 2191 珍惜现在,感恩生活 多重背包入门题

    背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...

  3. HDU 2191 珍惜现在,感恩生活(多重背包模板题)

    多重背包模板题 #include<iostream> #include<cstring> #include<algorithm> using namespace s ...

  4. nyoj 546——Divideing Jewels——————【dp、多重背包板子题】

    Divideing Jewels 时间限制:1000 ms  |  内存限制:65535 KB 难度:4   描述 Mary and Rose own a collection of jewells. ...

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

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

  6. ZOJ 4124 拓扑排序+思维dfs

    ZOJ - 4124Median 题目大意:有n个元素,给出m对a>b的关系,问哪个元素可能是第(n+1)/2个元素,可能的元素位置相应输出1,反之输出0 省赛都过去两周了,现在才补这题,这题感 ...

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

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

  8. HDU 3342 -- Legal or Not【裸拓扑排序 &amp;&amp;水题 &amp;&amp; 邻接表实现】

    Legal or Not Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  9. 第十二届湖南省赛 (B - 有向无环图 )(拓扑排序+思维)好题

    Bobo 有一个 n 个点,m 条边的有向无环图(即对于任意点 v,不存在从点 v 开始.点 v 结束的路径). 为了方便,点用 1,2,…,n 编号. 设 count(x,y) 表示点 x 到点 y ...

随机推荐

  1. 五,Smarty模板技术/引擎——自定义函数机制

    自建函数是smarty提供的函数,不允许修改,只能被调用: 自定义函数是自己编写函数,注册成为smarty的函数,之后可以被调用: 示例:使用smarty自定义函数的机制,编写一个函数myfun1,通 ...

  2. [Swift实际操作]八、实用进阶-(4)通过protocol在两个对象中进行消息传递

    本文将演示如何借助协议,实现视图控制器对象和其内部的自定义视图对象之间的数据传递. 首先创建一个自定义视图对象.在项目名称文件夹点击鼠标右键New File ->Cocoa Touch Clas ...

  3. CentOS加入Windows域

    CentOS加入Windows域,为减少操作已经提前关掉了selinux,防火墙.并且更改了主机名 yum install  nss-pam-ldapd -y 第一步:更改主机名为linux.itxd ...

  4. 前端-chromeF12 谷歌开发者工具详解 Network篇

    开发者工具初步介绍 chrome开发者工具最常用的四个功能模块: Elements:主要用来查看前面界面的html的Dom结构,和修改css的样式.css可以即时修改,即使显示.大大方便了开发者调试页 ...

  5. php获取随机字符串的几种方法

    方法一:shuffle函数(打乱数组)和mt_rand函数(生成随机数,比rand速度快四倍) /** * 获得随机字符串 * @param $len 需要的长度 * @param $special ...

  6. php中签名公钥、私钥(SHA1withRSA签名)以及AES(AES/ECB/PKCS5Padding)加密解密详解

    由于http请求是无状态,所以我们不知道请求方到底是谁.于是就诞生了签名,接收方和请求方协商一种签名方式进行验证,来取得互相信任,进行下一步业务逻辑交流. 其中签名用得很多的就是公钥私钥,用私钥签名, ...

  7. [web]深入理解Session和Cookie

    一.理解Cookie 由于http是一种无状态的协议,当用户的一次访问结束后,后端的服务器就无法知道下一次来访问的请求是不是上一次的用户了.那么Cookie的作用就是用户通过http访问一个服务器时, ...

  8. docker搭建tomcat环境

    1.拉取镜像 docker pull tomcat 2.运行容器 docker run --name tomcat -p : -v /data/tomcat/test:/usr/local/tomca ...

  9. 51 Nod 1050 dp

    1050 循环数组最大子段和 1 秒 131,072 KB 10 分 2 级题   N个整数组成的循环序列a[1],a[2],a[3],…,a[n],求该序列如a[i]+a[i+1]+…+a[j]的连 ...

  10. SpringBoot 启动的时候提示 Field *** in *** required a bean named 'entityManagerFactory' that could not be found.

    错误截图 后面发现原来和入口类代码有关. //@SpringBootApplication(scanBasePackages = {"org.jzc.odata.cboard",& ...