当前位置:网站首页>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;
}
边栏推荐
- 搭建主从模式集群redis
- VBScript详解(一)
- MySQL
- 《重构:改善既有代码的设计》读书笔记(下)
- AcWing 1128. 信使 题解(最短路—Floyd)
- Use cheat engine to modify money, life and stars in Kingdom rush
- zabbix5客户端安装和配置
- Reading notes of "the way to clean structure" (Part 2)
- Watchful pioneer world outlook Architecture - (how does a good game come from)
- 《架构整洁之道》读书笔记(下)
猜你喜欢
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
Implementation of online shopping mall system based on SSM
Design and implementation of ks004 based on SSH address book system
Common problems and description of kt148a voice chip IC development
Dictionaries
mysql函数
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
Watchful pioneer world outlook Architecture - (how does a good game come from)
SQLite 3.39.0 发布,支持右外连接和全外连接
自動生成VGG圖像注釋文件
随机推荐
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
KT148A语音芯片ic的用户端自己更换语音的方法,上位机
良心总结!Jupyter Notebook 从小白到高手,保姆教程来了!
函数高阶-柯里化实现
Common problems and description of kt148a voice chip IC development
[ERP software] what are the dangers of the secondary development of ERP system?
NMF-matlab
安装单机redis详细教程
Py's interpret: a detailed introduction to interpret, installation, and case application
嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件
Istio1.12:安装和快速入门
453-atoi函数的实现
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
AcWing 383. Sightseeing problem solution (shortest circuit)
Build a master-slave mode cluster redis
Kt148a voice chip instructions, hardware, protocols, common problems, and reference codes
MySQL table historical data cleaning summary
Implementation of 453 ATOI function
451 implementation of memcpy, memmove and memset
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config