GYM101635E Ingredients
题目链接: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的更多相关文章
- 【leetcode】1276. Number of Burgers with No Waste of Ingredients
题目如下: Given two integers tomatoSlices and cheeseSlices. The ingredients of different burgers are as ...
- Exploring Architectural Ingredients of Adversarially Robust Deep Neural Networks
目录 概 主要内容 深度 宽度 代码 Huang H., Wang Y., Erfani S., Gu Q., Bailey J. and Ma X. Exploring architectural ...
- LeetCode 5276. 不浪费原料的汉堡制作方案 Number of Burgers with No Waste of Ingredients
地址 https://leetcode-cn.com/problems/number-of-burgers-with-no-waste-of-ingredients/ 目描述圣诞活动预热开始啦,汉堡店 ...
- E - Ingredients 拓扑排序+01背包
题源:https://codeforces.com/gym/101635/attachments 题意: n行,每行给定字符串s1,s2,s3代表一些菜谱名.s2和s3是煮成是的必要条件,然后给出c和 ...
- (转) [it-ebooks]电子书列表
[it-ebooks]电子书列表 [2014]: Learning Objective-C by Developing iPhone Games || Leverage Xcode and Obj ...
- 《HeadFirst SQL》笔记
规范化 0 约束 1 原子性 2 第一范式 1NF 3 数据模式 4 依赖 5 联接查询 6 交叉联接(AKA 笛卡尔联接,叉积) 7 内联接 8 子查询 9 外联接 10 自联接 11 集合 12 ...
- angular学习笔记(二十八-附1)-$resource中的资源的方法
通过$resource获取到的资源,或者是通过$resource实例化的资源,资源本身就拥有了一些方法,$save,$delete,$remove,可以直接调用来保存该资源: 比如有一个$resour ...
- Stanford Prof. Li Feifei写给她学生的一封信
De-mystifying Good Research and Good Papers By Fei-Fei Li, 2009.03.01 Please remember this: 1000+ co ...
- 给你的应用“一只”智慧的眼睛 —— Barcode常识普及以及识别信息处理
在“如何用MediaCapture解决二维码扫描问题”这篇文章中,我们通过“成像”.“截图”与“识别”三个步骤介绍了使用MediaCapture扫码的主要过程及注意事项.本文主要针对“识别”的过程,对 ...
随机推荐
- AndroidStudio提高编译速度的建议
1.使用最新的Android gradle插件 Google tools team一直致力于提高android studio的编译速度,使用最新的gradle插件可以搞编译速度 在Android Gr ...
- centos分配IP脚本--写的第一个shell脚本
IDC小菜鸟一枚,非科班出身.常常有客户的centos服务器需要分配15个IP甚至30个IP.每次需要手动分配十分麻烦,于是花了一天时间学了shell脚本,写了这个脚本. #!/bin/bash re ...
- 阿里巴巴年薪800k大数据全栈工程师成长记
大数据全栈工程师一词,最早出现于Facebook工程师Calos Bueno的一篇文章 - Full Stack (需fanqiang).他把全栈工程师定义为对性能影响有着深入理解的技术通才.自那以后 ...
- 01-复杂度2 Maximum Subsequence Sum
01-复杂度2 Maximum Subsequence Sum (25分) 时间限制:200ms 内存限制:64MB 代码长度限制:16kB 判题程序:系统默认 作者:陈越 单位:浙江大学 htt ...
- 使用BottomNavigationView+ViewPager+Fragment的底部导航栏
2019独角兽企业重金招聘Python工程师标准>>> 使用BottomNavigationView做底部工具栏,使用ViewPager做页面切换,使用Fragment完成每个页面的 ...
- 图论--二分图最佳完美匹配(KM模板)
#include <iostream> #include <cstring> #include <cstdio> using namespace std; cons ...
- vue js手机端滑到某一个位置时固定位置显示
1.HTML <div id="searchBar"><ul class="items_filter" :class="search ...
- VSCode 安装 React 项目
1 下载nodejs 安装 (此时npm 和 node环境都已经装好) 2 安装淘宝镜像 npm install -g cnpm --registry=https://registry.npm.tao ...
- windows远程执行命令总结
1. 利用Impacket Impacket是一个Python类库,用于对SMB1-3或IPv4 / IPv6 上的TCP.UDP.ICMP.IGMP,ARP,IPv4,IPv6,SMB,MSRPC, ...
- 无线脉冲水表LoRaWAN方案芯片ASR6500S
无线脉冲水表LoRaWAN方案 物联网是新一代信息技术的重要组成部分,也是"信息化"时代的重要发展阶段,在物联网飞速发展的今天,只有多技术融合的物联网解决方案才能够在不同的应用场景 ...