题目链接: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. CtsVerifier-Bluetooth-LE-SEcure-ClientServer-Test测试pass但是无法选择passbutton

    [问题描述] CtsVerifier-Bluetooth-LE-SEcure-ClientServer-Test测试pass但是无法选择Pass-Button 工具版本:9.0-r11 其他信息: 上 ...

  2. 【集群实战】inotify

    1. inotify简介 Inotify是一种强大的,细粒度的,异步的文件系统事件监控机制(软件). linux内核从2.6.13起,加入了Inotify支持,通过Inotify可以监控文件系统中添加 ...

  3. 【Linux常见命令】ls命令

    ls - list directory contents ls命令用于显示指定工作目录下之内容(列出目前工作目录所含之文件及子目录). 语法:  ls [OPTION]... [FILE]...  l ...

  4. LinearLayout控件

    LinearLayout是线性布局控件,它包含的子控件将以横向或竖向的方式排列,按照相对位置来排列所有的widgets或者其他的containers,超过边界时,某些控件将缺失或消失.因此一个垂直列表 ...

  5. JS编程建议——11:慎重使用伪数组

    建议11:慎重使用伪数组JavaScript没有真正的数组,因此typeof运算符不能辨别数组和对象.伪数组在JavaScript中有很高的易用性,程序员不用给它设置维度,而且永远不用担心产生越界错误 ...

  6. Appium-desktop 元素定位

    1.打开 appium-desktop ,点击 start session 2.打开后,点击屏幕右上角的搜索按钮 3.然后会打开配置页面,在本地服务配置信息同上面写的代码链接配置.填入正确的信息后,点 ...

  7. NetCore项目实战篇03---HTTP Patch 更新数据

    一.什么是HTPP Patch HTTP1.0定义了三种请求方法: GET, POST 和 HEAD方法. HTTP1.1新增了五种请求方法:OPTIONS, PUT, DELETE, TRACE 和 ...

  8. windows远程执行命令总结

    1. 利用Impacket Impacket是一个Python类库,用于对SMB1-3或IPv4 / IPv6 上的TCP.UDP.ICMP.IGMP,ARP,IPv4,IPv6,SMB,MSRPC, ...

  9. 1年之后的拿高工资的资本,Java线程

    只要开启线程,都会开启一块对应的栈内存,然后进行同步执行. -- 谈斌 线程是CPU用来处理程序的资源,线程的执行是抢占式的. 线程开启方式: 创建一个类,继承Thread类.重写 run(), 并在 ...

  10. D. Ehab the Xorcist(纯构造方法)

    \(如果觉得下面难以理解,可以去这里看一种较为简单的解法\):saf \(这个题嘛,首先要明确异或的性质:相同为0,不同为1.\) \(举个例子,我们来构造u=15和v=127的情况\) \(注意到, ...