当前位置:网站首页>AcWing 341. Optimal trade solution (shortest path, DP)
AcWing 341. Optimal trade solution (shortest path, DP)
2022-07-02 19:43:00 【Mr. Qiao Da】
AcWing 341. The best trade
Their thinking : The first dp Aspects , take n A point is regarded as n The dividing point of two states ,dp[k] Said to k It's the dividing point , stay k Bought before , stay k Sell later ,n There will be repetition in each state , But there must be nothing missing ( Satisfy dp The condition of finding the best value ). How to ask after n A state of dp Value becomes the key , Because a graph may have rings , So you can't use state transfer directly dp, Still use spfa Find the shortest path , But the weight is transferred from edge to point , But the truth is the same . Find two shortest paths , One is to be able to reach x The lowest buying price of points , Traversal by ht The beginning adjacency table record is evaluated by the positive side , The other is seeking to reach x The maximum selling price of points , Traversal by hs The recorded inverse adjacency table is obtained , Calculate the two values of each point and then sum them up to dp value , Finally, traverse to find the largest dp The value is the answer
#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]); // seek n A state of dp value
cout<<res<<endl;
return 0;
}
边栏推荐
- Implementation of online shopping mall system based on SSM
- End to end object detection with transformers (Detr) paper reading and understanding
- 中缀表达式转换为后缀表达式(C语言代码+详解)
- 股票证券公司排名,有安全保障吗
- MySQL
- LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
- Infix expression is converted to suffix expression (C language code + detailed explanation)
- AcWing 343. 排序 题解(floyd性质实现传递闭包)
- How to set priorities in C language? Elaborate on C language priorities
- AcWing 1129. Heat wave solution (shortest path SPFA)
猜你喜欢
Common problems and description of kt148a voice chip IC development
Implementation of online shopping mall system based on SSM
Shardingsphere jdbc5.1.2 about select last_ INSERT_ ID () I found that there was still a routing problem
Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!
How to avoid duplicate data in gaobingfa?
Dictionaries
基于SSM实现网上购物商城系统
自動生成VGG圖像注釋文件
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
搭建哨兵模式reids、redis从节点脱离哨兵集群
随机推荐
《重构:改善既有代码的设计》读书笔记(上)
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
JS如何取整数
Kt148a voice chip IC user end self replacement voice method, upper computer
Py's interpret: a detailed introduction to interpret, installation, and case application
AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
Automatic reading of simple books
《架构整洁之道》读书笔记(下)
AcWing 1125. 牛的旅行 题解(最短路、直径)
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
Istio1.12: installation and quick start
Typescript 之 快速入门
多端小程序开发有什么好处?覆盖百度小程序抖音小程序微信小程序开发,抢占多平台流量红利
Codeforces Round #802 (Div. 2) 纯补题
AcWing 383. 观光 题解(最短路)
AcWing 181. Turnaround game solution (search ida* search)
Postman download and installation
AcWing 1131. 拯救大兵瑞恩 题解(最短路)
453-atoi函数的实现
Horizontal ultra vires and vertical ultra vires [easy to understand]