当前位置:网站首页>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;
}
边栏推荐
- Mobile robot path planning: artificial potential field method [easy to understand]
- Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
- AcWing 1134. Shortest circuit counting problem solution (shortest circuit)
- AcWing 383. 观光 题解(最短路)
- Golang并发编程——goroutine、channel、sync
- 良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!
- 简书自动阅读
- JASMINER X4 1U深度拆解,揭开高效省电背后的秘密
- Golang:[]byte to string
- 《代碼整潔之道》讀書筆記
猜你喜欢
Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode
Génération automatique de fichiers d'annotation d'images vgg
程序猿入门攻略(十二)——数据的存储
AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
End to end object detection with transformers (Detr) paper reading and understanding
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
KT148A语音芯片ic的硬件设计注意事项
Educational codeforces round 129 (rated for Div. 2) supplementary problem solution
Conscience summary! Jupyter notebook from Xiaobai to master, the nanny tutorial is coming!
随机推荐
What is the MySQL backup suffix_ MySQL backup restore
MySQL
Dictionaries
Yes, that's it!
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure
NMF-matlab
Bubble sort array
Automatically generate VGg image annotation file
Getting started with typescript
字典
《重构:改善既有代码的设计》读书笔记(下)
Implementation of online shopping mall system based on SSM
基于SSM实现网上购物商城系统
《架构整洁之道》读书笔记(下)
JS如何取整数
Understanding and function of polymorphism
自動生成VGG圖像注釋文件
Cuckoo filter
AcWing 1137. 选择最佳线路 题解(最短路)
搭建主从模式集群redis