当前位置:网站首页>[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;
}
边栏推荐
- Xctf attack and defense world crypto advanced area best_ rsa
- Chapter 1: extend the same code decimal sum s (D, n)
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- IPv6 experiment
- JMeter connection database
- Phpstudy set LAN access
- Typora charges, WTF? Still need support
- WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
- Titles can only be retrieved in PHP via curl - header only retrieval in PHP via curl
- Today's work summary and plan: February 14, 2022
猜你喜欢

I study database at station B (4): DQL

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

Basic principle of LSM tree

Gym welcomes the first complete environmental document, which makes it easier to get started with intensive learning!

FAQs for datawhale learning!

2022 Xinjiang latest road transportation safety officer simulation examination questions and answers

Chapter 1: simplify the same code decimal sum s (D, n)

Buuctf's different flags and simplerev

2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
随机推荐
Professional interpretation | how to become an SQL developer
Kubernetes cluster builds efk log collection platform
Cesiumjs 2022 ^ source code interpretation [7] - Analysis of the request and loading process of 3dfiles
04 -- QT OpenGL two sets of shaders draw two triangles
Native table - scroll - merge function
第一章:递归求n的阶乘n!
Common text labels
What is the content of game modeling
05 -- QT OpenGL draw cube uniform
Microsoft: the 12th generation core processor needs to be upgraded to win11 to give full play to its maximum performance
Part 27 supplement (27) buttons of QML basic elements
Xctf attack and defense world crypto advanced area best_ rsa
Today's work summary and plan: February 14, 2022
Day10 ---- 强制登录, token刷新与jwt禁用
QT -- qfile file read / write operation
Web Security (VIII) what is CSRF attack? Why can token prevent csdf attacks?
Detailed and not wordy. Share the win10 tutorial of computer reinstallation system
Zhang Fei hardware 90 day learning notes - personal record on day 6. Please see my personal profile / homepage for the complete record
BUUCTF
Xctf attack and defense world crypto master advanced area olddriver