当前位置:网站首页>[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
dfsSome things need to be recorded to flash back , What needs to be traced back in this topic iscntvalue ( The number of paths is recorded ) - g [ i ] [ j ] g[i][j] g[i][j] representative
iTojWhether 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
cntWe 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;
}
边栏推荐
- PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
- Class loading process
- Professional interpretation | how to become an SQL developer
- Chapter 1: King Shehan miscalculated
- The simplicity of laravel
- Basic principle of LSM tree
- I study database at station B (4): DQL
- Phpstudy set LAN access
- How to improve data security by renting servers in Hong Kong
- 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
猜你喜欢

第二章:求a,b的最大公约与最小公倍数经典求解,求a,b的最大公约与最小公倍数常规求解,求n个正整数的的最大公约与最小公倍数

第二章:4位卡普雷卡数,搜索偶数位卡普雷卡数,搜索n位2段和平方数,m位不含0的巧妙平方数,指定数字组成没有重复数字的7位平方数,求指定区间内的勾股数组,求指定区间内的倒立勾股数组

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

Octopus online ecological chain tour Atocha protocol received near grant worth $50000

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

Acquisition and transmission of parameters in automatic testing of JMeter interface

Chapter 1: sum of three factorials, graph point scanning

2022-06-25 advanced network engineering (XI) IS-IS synchronization process of three tables (neighbor table, routing table, link state database table), LSP, cSNP, psnp, LSP
![Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles](/img/70/6fd00146418e5d481e951d51428990.png)
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles

Teach you how to quickly recover data by deleting recycle bin files by mistake
随机推荐
Zhang Fei hardware 90 day learning notes - personal record on day 5. Please see my personal profile / homepage for the complete record
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
5. MVVM model
Merge K ascending linked lists
Buuctf's different flags and simplerev
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
FPGA 学习笔记:Vivado 2019.1 工程创建
Use of aggregate functions
Meso tetra [P - (p-n-carbazole benzylidene imino)] phenylporphyrin (tcipp) /eu (tcipp) [pc( α- 2-oc8h17) 4] and euh (tcipp) [pc (a-2-oc8h17) 4] supplied by Qiyue
Chapter 1: sum of three factorials, graph point scanning
Acquisition and transmission of parameters in automatic testing of JMeter interface
第一章:求同吗小数和s(d, n)
Zhang Fei hardware 90 day learning notes - personal record of day 3, please see my personal profile / homepage for the complete
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
How to improve data security by renting servers in Hong Kong
Common text labels
JMeter connection database
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue
Today's work summary and plan: February 14, 2022