题目链接: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. 获取系统DPI、系统显示比例等

    using System; using System.Drawing; using System.Runtime.InteropServices; namespace XYDES { public c ...

  2. javascript 控制台调试方法

    console在我们调试js程序的时候是一个非常有效的工具. 日志输出是我们最常用的功能: console.log(); console.info(); console.warn(); console ...

  3. Java反射与注解

    反射 能够分析类能力的程序称为反射(reflective),代码的这种能力称为"自省".反射机制的功能极其强大,反射机制可以用来: 在运行时分析类的能力 在运行时查看对象,例如,编 ...

  4. 以内存级速度实现存储?XPoint正是我们的计划

    随着计算能力虚拟化技术的普及,存储机制在速度上远逊于内存这一劣势开始变得愈发凸显. 这一巨大的访问速度鸿沟一直是各项存储技术想要解决的核心难题:纸带.磁带.磁盘驱动器乃至闪存记忆体等等,而如今最新一代 ...

  5. MySQL 子查询——查询最大值

    子查询指将一个查询语句嵌套在另一个查询语句中.子查询可以在 SELECT.UPDATE 和 DELETE 语句中使用,而且可以进行多层嵌套.在实际开发时,子查询经常出现在 WHERE 子句中.子查询在 ...

  6. 用Navicat建MySQL数据库表,动态改变创建时间和更新时间戳

    1.create_time 记录创建的时间,设默认值为:CURRENT_TIMESATMP 注意:不勾选那个[根据当前时间戳更新] 2.operator_time 更新记录的时间,勾选那个[根据当前时 ...

  7. Navicat12.1系列安装,破解以及破解navicat报错的解决方案

    由于上课的需要,我们必须自己下载并安装 Navicat Premium 12,虽然安装过程很简单,但是安装后的navicat只能试用,并没有永久激活,然而我还想永久使用,所以就各种百度,因为不断地遇到 ...

  8. 线段树 区间合并 F - Sequence operation

    F - Sequence operation 题解:这个题目不是一个特别难的题目,但是呢,写了好久,首先线段树难敲,其次就是bug难找,最后这个代码都被我改的乱七八糟的了,这个有两个地方要注意一下,一 ...

  9. EditPlus编辑java代码 常规配置

  10. python学习之break和continue在for循环中的使用(案例:打印出10以内的偶数,并且只要前三个偶数)

    运行程序,break是整个程序都跳出 continue则表示跳过当前一次循环,然后继续执行循环