当前位置:网站首页>[raid] [simple DP] mine excavation
[raid] [simple DP] mine excavation
2022-07-03 19:48:00 【Code chess】
link :
️https://www.luogu.com.cn/problem/P2196️
There are a series of paths in several points , One can walk from a certain point , Until the end , Each point has a value , Ask the maximum value you can get
Because of the small amount of data , So we can conduct a violent search
1️⃣ Violent search
- You can start at each point : So in
dfs
Some things need to be recorded to flash back , What needs to be traced back in this topic iscnt
value ( The number of paths is recorded ) - g [ i ] [ j ] g[i][j] g[i][j] representative
i
Toj
Whether it can reach v
: An array of record pathssum
: Represents the sum of the median values of the path ending with the current point- Pay attention to the starting point
cnt
We should also go back
Violent search
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
bool g[21][21],vis[30];
int a[25];
int p[25];
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
memset(g, false, sizeof(g));
int n;
cin >> n;
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 2; i <= n; i++)
{
for(int j = i; j <= n; j++)
{
int x;
cin >> x;
g[i - 1][j] = x;
}
}
int ans = 0;
vector<int> v;
int cnt = 0;
function<void(int, int)> dfs = [&](int x, int sum) -> void
{
for(int i = x + 1; i <= n; i++)
{
if(g[x][i])
{
p[++ cnt] = i;
dfs(i, sum + a[i]);
cnt --;
}
}
if(sum > ans)
{
ans = sum;
v.clear();
for(int i = 1; i <= cnt; i++) v.push_back(p[i]);
}
};
for(int i = 1; i <= n; i++)
{
p[++ cnt] = i;
dfs(i, a[i]);
cnt --;
}
for(int i = 0; i < v.size(); i++)
cout << v[i] << " \n"[i == v.size() - 1];
cout << ans << "\n";
return 0;
}
2️⃣DP
State means :
f [ i ] f[i] f[i]: With i
Is the maximum value of the sum in the ending path
State shift :
f [ i ] = m a x ( f [ j ] + a [ i ] , f [ i ] ) = m a x ( f [ j ] ) + a [ i ] f[i] = max(f[j] + a[i],f[i]) = max(f[j]) + a[i] f[i]=max(f[j]+a[i],f[i])=max(f[j])+a[i]
Because the path needs to be recorded , You need to set a pre
The precursor node in the array storage path , Finally, use recursive output path .
And also notice that f[i]
Initialization of an array , Because sometimes you can choose only one point , If it is not initialized, an error will occur .
last
: The end node of the path in the optimal result
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int>a(n + 1),f(n + 1);
vector g(n + 1, vector(n + 1, 0));
for(int i = 1; i <= n; i++) cin >> a[i];
for(int i = 1; i < n; i++)
for(int j = i + 1; j <= n; j++)
{
int x;
cin >> x;
g[i][j] = x;
}
vector<int> pre(n + 1, 0);
int res = 0;
int last = 0;
for(int i = 1; i <= n; i++) f[i] = a[i];
for(int i = 2; i <= n; i++)
{
for(int j = 1; j < i; j++)
{
if(g[j][i] && f[j] + a[i] > f[i])
{
f[i] = f[j] + a[i];
pre[i] = j;
}
}
if(f[i] > res)
{
last = i;
res = f[i];
}
}
function<void(int)> print = [&](int x) -> void
{
if(pre[x] == 0)
{
cout << x ;
return;
}
print(pre[x]);
cout << " " << x;
};
print(last);
cout << "\n" << res << "\n";
return 0;
}
边栏推荐
- BOC protected alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC / alanine zinc porphyrin Zn · TAPP ala BOC supplied by Qiyu
- Class loading process
- P3402 persistent and searchable
- Nerfplusplus parameter format sorting
- Rd file name conflict when extending a S4 method of some other package
- Basic principle of LSM tree
- 6. Data agent object Defineproperty method
- PR FAQ: how to set PR vertical screen sequence?
- Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
- The space of C disk is insufficient, and the computer becomes stuck. Quickly expand the capacity of C disk to make the system more smooth
猜你喜欢
Part 28 supplement (XXVIII) busyindicator (waiting for elements)
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]
第二章:基于分解的求水仙花数,基于组合的求水仙花数, 兰德尔数,求[x,y]内的守形数,探求n位守形数,递推探索n位逐位整除数
第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算
Point cloud data denoising
kubernetes集群搭建efk日志收集平台
Chapter 2: 4-digit Kaplan number, search even digit Kaplan number, search n-digit 2-segment sum square number, m-digit ingenious square number without 0, specify the number to form a 7-digit square nu
Use of aggregate functions
04 -- QT OpenGL two sets of shaders draw two triangles
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
随机推荐
Find a line in a file and remove it
Summary of learning materials and notes of Zhang Fei's actual combat electronics 1-31
Basic principle of LSM tree
5- (4-nitrophenyl) - 10,15,20-triphenylporphyrin ntpph2/ntppzn/ntppmn/ntppfe/ntppni/ntppcu/ntppcd/ntppco and other metal complexes
Ae/pr/fcpx super visual effects plug-in package fxfactory
2022-07-02 网工进阶(十五)路由策略-Route-Policy特性、策略路由(Policy-Based Routing)、MQC(模块化QoS命令行)
Micro service knowledge sorting - three pieces of micro Service Technology
BOC protected amino acid porphyrins TAPP ala BOC, TAPP Phe BOC, TAPP Trp BOC, Zn · TAPP ala BOC, Zn · TAPP Phe BOC, Zn · TAPP Trp BOC Qiyue
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
What is the content of game modeling
Make a simple text logo with DW
Leetcode daily question solution: 540 A single element in an ordered array
05 -- QT OpenGL draw cube uniform
Network security Kali penetration learning how to get started with web penetration how to scan based on nmap
Point cloud data denoising
Buuctf's different flags and simplerev
Next spread
Use of aggregate functions