当前位置:网站首页>AcWing 341. 最优贸易 题解 (最短路、dp)
AcWing 341. 最优贸易 题解 (最短路、dp)
2022-07-02 18:27:00 【乔大先生】
AcWing 341. 最优贸易
解题思路:先往dp方面想,将n个点视为n个状态的分界点,dp[k]表示以k为分界点,在k之前买进,在k之后卖出,n个状态会有重复的,但肯定不会有漏掉的(满足dp求最值的条件)。之后怎么求n个状态的dp值就成了关键,因为图可能会存在环,所以不能直接用状态转移dp,依然用spfa最短路径求,不过权值由边转移到了点上,但是道理相同。求两种最短路径,一种是能到达x点的最低的买入价,遍历由ht开头的邻接表记录正向边求得,另一种是求能到达x点的最大的卖出价,遍历由hs记录的逆向邻接表求得,求出每个点的两个值之后合计得出dp值,最后遍历找到最大的dp值就是答案
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10, M = 2e6 + 10;
int ht[N], hs[N], e[M], ne[M], w[M], idx;
int n, m;
bool st[N];
int dist[N];
int dmin[N], dmax[N];
int q[N];
void add(int h[], int a, int b){
e[idx] = b;
ne[idx] = h[a];
h[a] = idx ++ ;
}
void spfa(int h[], int dist[], int type){
int hh = 0, tt = 1;
if(type == 0){
memset(dist, 0x3f, sizeof dmin);
dist[1] = w[1];
q[0] = 1;
}
else{
memset(dist, -0x3f, sizeof dmax);
dist[n] = w[n];
q[0] = n;
}
while(hh != tt){
int t = q[hh ++ ];
if(hh == N) hh = 0;
st[t] = false;
for(int i = h[t]; ~i; i = ne[i]){
int j = e[i];
if((type == 0 && dist[j] > min(dist[t], w[j])) || (type == 1 && dist[j] < max(dist[t], w[j]))){
if(type == 0) dist[j] = min(dist[t], w[j]);
else dist[j] = max(dist[t], w[j]);
if(!st[j]){
q[tt ++ ] = j;
if(tt == N) tt = 0;
st[j] = true;
}
}
}
}
}
int main()
{
cin>>n>>m;
for(int i = 1; i <= n; i ++ ){
scanf("%d", &w[i]);
}
memset(ht, -1, sizeof ht);
memset(hs, -1, sizeof hs);
while(m -- ){
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
add(ht, x, y);
add(hs, y, x);
if(z == 2){
add(ht, y, x);
add(hs, x, y);
}
}
spfa(ht, dmin, 0);
spfa(hs, dmax, 1);
int res = 0;
for(int i = 1; i <= n; i ++ ) res = max(res, dmax[i] - dmin[i]); //求n个状态的dp值
cout<<res<<endl;
return 0;
}
边栏推荐
- Golang:[]byte to string
- LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
- [test development] software testing - concept
- Bubble sort array
- End-to-End Object Detection with Transformers(DETR)论文阅读与理解
- QT中的QPropertyAnimation使用和toast案列
- [pytorch learning notes] tensor
- Reduce -- traverse element calculation. The specific calculation formula needs to be passed in and combined with BigDecimal
- C file input operation
- Juypter notebook modify the default open folder and default browser
猜你喜欢
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
ICDE 2023|TKDE Poster Session(CFP)
《重构:改善既有代码的设计》读书笔记(下)
高级性能测试系列《24. 通过jdbc执行sql脚本》
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
[test development] software testing - concept
xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
Talk about the design of red envelope activities in e-commerce system
安装单机redis详细教程
Obligatoire pour les débutants, cliquez sur deux boutons pour passer à un contenu différent
随机推荐
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
【pytorch学习笔记】Tensor
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
[pytorch learning notes] tensor
NPOI导出Excel2007
Develop fixed asset management system, what voice is used to develop fixed asset management system
Usage of ieda refactor
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
开发固定资产管理系统,开发固定资产管理系统用什么语音
AcWing 1137. 选择最佳线路 题解(最短路)
MySQL表历史数据清理总结
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
2022 compilation principle final examination recall Edition
Data dimensionality reduction factor analysis
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
【ERP软件】ERP体系二次开发有哪些危险?
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
线程应用实例