题目链接:https://vjudge.net/problem/Gym-101635E

题目大意:

  给定一个有 \(N\) 条边的有向无环图(有多个起点),每条边都有其费用和收益,现要从一个或多个起点出发,以某一个或多个点为终点(一个点不能多次作为终点;如果有多个方案能到达同一个点,则选择总费用最少的),问在使得总费用不超过 \(B\) 的大前提下,能得到的最大收益(如果有多个得到最大收益的方法,则选择使得费用最少的)。输出最大收益及其对应的总费用。

  (建议仔细地读一下 \(Important Notes\))

知识点:  DP、最短路

解题思路:

  先用 \(Dijkstra\) 跑出从各个起点到各个点的最小费用及其对应的收益(如果有多个费用最小的方案,则选择收益最大的那个),再用 \(dp\) 计算出在各个总费用下所能得到的最大收益。

AC代码:

 #include <bits/stdc++.h>
using namespace std;
const int maxm = +, maxn = +, inf=0x3f3f3f3f; struct Edge{
int to,nxt,cost,prestige;
}edge[maxm];
int head[maxm],tot;
void init(){
memset(head,-,sizeof(head));
tot=;
}
void addedge(int u,int v,int cost,int pres){
edge[tot].to=v;
edge[tot].nxt=head[u];
edge[tot].prestige=pres;
edge[tot].cost=cost;
head[u]=tot++;
} int min_cost[maxn],max_pres[maxn];
bool noroot[maxn];
map<string,int> ind; void dijkstra(int s){
queue<int> que;
que.push(s);
min_cost[s]=max_pres[s]=;
while(!que.empty()){
int v=que.front();
que.pop();
for(int i=head[v];i!=-;i=edge[i].nxt){
int to=edge[i].to;
if(min_cost[to]>min_cost[v]+edge[i].cost){
min_cost[to]=min_cost[v]+edge[i].cost;
max_pres[to]=max_pres[v]+edge[i].prestige;
que.push(to);
}
else if(min_cost[to]==min_cost[v]+edge[i].cost&&max_pres[to]<max_pres[v]+edge[i].prestige){
max_pres[to]=max_pres[v]+edge[i].prestige;
que.push(to);
}
}
}
}
int dp[maxn];
int main(){
std::ios::sync_with_stdio(false); //如果没有加速输入输出的话会T
int B,N,cos,pre,u,v;
int cnt=;
string to,from,tmp;
cin>>B>>N;
init();
for(int i=;i<N;i++){
cin>>to>>from>>tmp>>cos>>pre;
if(!ind[to])
ind[to]=cnt++;
v=ind[to];
if(!ind[from])
ind[from]=cnt++;
u=ind[from];
addedge(u,v,cos,pre);
noroot[v]=true;
}
for(int i=;i<cnt;i++)
min_cost[i]=inf,max_pres[i]=;
for(int i=;i<cnt;i++){
if(!noroot[i])
dijkstra(i);
}
dp[]=;
for(int i=;i<cnt;i++){
for(int j=B;j>=min_cost[i];j--)
dp[j]=max(dp[j],dp[j-min_cost[i]]+max_pres[i]);
}
int ans1=,ans2=;
for(int i=;i<=B;i++){
if(dp[i]>ans1)
ans1=dp[i],ans2=i;
}
cout<<ans1<<endl;
cout<<ans2<<endl; return ;
}

GYM101635E Ingredients的更多相关文章

  1. 【leetcode】1276. Number of Burgers with No Waste of Ingredients

    题目如下: Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as ...

  2. Exploring Architectural Ingredients of Adversarially Robust Deep Neural Networks

    目录 概 主要内容 深度 宽度 代码 Huang H., Wang Y., Erfani S., Gu Q., Bailey J. and Ma X. Exploring architectural ...

  3. LeetCode 5276. 不浪费原料的汉堡制作方案 Number of Burgers with No Waste of Ingredients

    地址 https://leetcode-cn.com/problems/number-of-burgers-with-no-waste-of-ingredients/ 目描述圣诞活动预热开始啦,汉堡店 ...

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

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

  5. (转) [it-ebooks]电子书列表

    [it-ebooks]电子书列表   [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...

  6. 《HeadFirst SQL》笔记

    规范化 0 约束 1 原子性 2 第一范式 1NF 3 数据模式 4 依赖 5 联接查询 6 交叉联接(AKA 笛卡尔联接,叉积) 7 内联接 8 子查询 9 外联接 10 自联接 11 集合 12 ...

  7. angular学习笔记(二十八-附1)-$resource中的资源的方法

    通过$resource获取到的资源,或者是通过$resource实例化的资源,资源本身就拥有了一些方法,$save,$delete,$remove,可以直接调用来保存该资源: 比如有一个$resour ...

  8. Stanford Prof. Li Feifei写给她学生的一封信

    De-mystifying Good Research and Good Papers By Fei-Fei Li, 2009.03.01 Please remember this: 1000+ co ...

  9. 给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理

    在“如何用MediaCapture解决二维码扫描问题”这篇文章中,我们通过“成像”.“截图”与“识别”三个步骤介绍了使用MediaCapture扫码的主要过程及注意事项.本文主要针对“识别”的过程,对 ...

随机推荐

  1. certutil 导入 CA 证书

    2019独角兽企业重金招聘Python工程师标准>>> 在linux下使用GoAgent客户端的时候,需要导入CA.cer证书. 安装证书管理工具 apt-get install l ...

  2. MySQL用另一张表的字段值Update本表

    SQL示例: UPDATE TABLE1 a, TABLE2 b SET a.field1 = b. field1 [, a.field2 = b.field2, ...] WHERE a.connn ...

  3. 历史上的今天mysql数据库包含详情分类以及图片

    历史上的今天mysql数据库包含详情分类以及图片 https://item.taobao.com/item.htm?spm=a2oq0.12575281.0.0.50111debo71iaJ& ...

  4. CodeForces - 140A New Year Table (几何题)当时没想出来-----补题

    A. New Year Table time limit per test2 seconds memory limit per test256 megabytes inputstandard inpu ...

  5. P2290 [HNOI2004]树的计数(bzoj1211)

    洛谷P2290 [HNOI2004]树的计数 bzoj1211 [HNOI2004]树的计数 Description 一个有\(n\)个结点的树,设它的结点分别为\(v_1,v_2,\cdots, v ...

  6. 跟哥一起学python(2)- 运行第一个python程序&环境搭建

    本节的任务,是完成我们的第一个python程序,并搭建好学习python的环境.  建议通过视频来学习本节内容: 查看本节视频 再次看看上一节提到的那张图,看看作为高级编程语言,我们如何编程. 首先, ...

  7. win7 64位系统使用vs2010编译OSG3.2.1

    首先我想说的是,osg是有二进制安装包的:http://openscenegraph.alphapixel.com/osg/downloads/free-openscenegraph-binary-d ...

  8. 僵尸进程(zombie process)

    首先了解一下linux中进程的5大状态: R Running or runnable (on run queue)S Interruptible sleep (waiting for an event ...

  9. 一文带你深入了解 Lambda 表达式和方法引用

    前言 尽管目前很多公司已经使用 Java8 作为项目开发语言,但是仍然有一部分开发者只是将其设置到 pom 文件中,并未真正开始使用.而项目中如果有8新特性的写法,例如λ表达式.也只是 Idea Al ...

  10. 这是一篇每个人都能读懂的最小生成树文章(Kruskal)

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题的第19篇文章,我们一起来看看最小生成树. 我们先不讲算法的原理,也不讲一些七七八八的概念,因为对于初学者来说,看到 ...