当前位置:网站首页>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;
}
边栏推荐
- AcWing 383. Sightseeing problem solution (shortest circuit)
- AcWing 343. 排序 题解(floyd性质实现传递闭包)
- Postman下载安装
- 使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
- What is the Bluetooth chip ble, how to select it, and what is the path of subsequent technology development
- Npoi export Excel2007
- Think about the huge changes caused by variables
- MySQL
- 450-深信服面经1
- How to set priorities in C language? Elaborate on C language priorities
猜你喜欢
数据湖(十二):Spark3.1.2与Iceberg0.12.1整合
Refactoring: improving the design of existing code (Part 1)
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
Refactoring: improving the design of existing code (Part 2)
Dictionaries
定了,就是它!
字典
《架构整洁之道》读书笔记(下)
Kt148a voice chip IC software reference code c language, first-line serial port
SQLite 3.39.0 发布,支持右外连接和全外连接
随机推荐
Gmapping code analysis [easy to understand]
450 Shenxin Mianjing 1
Npoi export Excel2007
Use cheat engine to modify money, life and stars in Kingdom rush
AcWing 341. 最优贸易 题解 (最短路、dp)
AcWing 1129. Heat wave solution (shortest path SPFA)
452-strcpy、strcat、strcmp、strstr、strchr的实现
Set up sentinel mode. Reids and redis leave the sentinel cluster from the node
4274. Suffix expression - binary expression tree
Kt148a voice chip IC software reference code c language, first-line serial port
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
MySQL function
《代码整洁之道》读书笔记
Use IDM to download Baidu online disk files (useful for personal testing) [easy to understand]
Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
《重构:改善既有代码的设计》读书笔记(下)
《架构整洁之道》读书笔记(下)
蓝牙芯片ble是什么,以及该如何选型,后续技术发展的路径是什么
Kt148a voice chip IC user end self replacement voice method, upper computer
多态的理解以及作用