当前位置:网站首页>[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;
}
边栏推荐
- Strict data sheet of new features of SQLite 3.37.0
- Buuctf's different flags and simplerev
- 2022 - 06 - 30 networker Advanced (XIV) Routing Policy Matching Tool [ACL, IP prefix list] and policy tool [Filter Policy]
- 第一章: 舍罕王失算
- Realize user registration and login
- Unittest framework is basically used
- Cross compile opencv with contrib
- Today's work summary and plan: February 14, 2022
- BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply
- PR 2021 quick start tutorial, how to create new projects and basic settings of preferences?
猜你喜欢

HCIA-USG Security Policy
![2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]](/img/b6/5d6b946d8001e2d73c2cadbdce72fc.png)
2022-06-30 advanced network engineering (XIV) routing strategy - matching tools [ACL, IP prefix list], policy tools [filter policy]

I study database at station B (4): DQL

Native table - scroll - merge function

Network security Kali penetration learning how to get started with web penetration how to scan based on nmap

Bright purple crystal meso tetra (4-aminophenyl) porphyrin tapp/tapppt/tappco/tappcd/tappzn/tapppd/tappcu/tappni/tappfe/tappmn metal complex - supplied by Qiyue

BOC protected tryptophan porphyrin compound (TAPP Trp BOC) Pink Solid 162.8mg supply - Qiyue supply

Chapter 1: King Shehan miscalculated

第一章: 舍罕王失算

QT -- qfileinfo file information reading
随机推荐
About unregistered transfer login page
Nerfplusplus parameter format sorting
第一章:简化同码小数和s(d, n)
WPF format datetime in TextBlock- WPF format DateTime in TextBlock?
2022-07-02 advanced network engineering (XV) routing policy - route policy feature, policy based routing, MQC (modular QoS command line)
Merge K ascending linked lists
Chapter 1: recursively find the factorial n of n!
Pecan - route
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
Class loading process
PR FAQ: how to set PR vertical screen sequence?
2022 Xinjiang latest construction eight members (standard members) simulated examination questions and answers
How to improve data security by renting servers in Hong Kong
Common text labels
2022-06-27 advanced network engineering (XII) IS-IS overhead type, overhead calculation, LSP processing mechanism, route revocation, route penetration
BOC protected tryptophan zinc porphyrin (Zn · TAPP Trp BOC) / copper porphyrin (Cu · TAPP Trp BOC) / cobalt porphyrin (cobalt · TAPP Trp BOC) / iron porphyrin (Fe · TAPP Trp BOC) / Qiyue supply
Leetcode daily question solution: 540 A single element in an ordered array
Chapter 1: drinking soft drinks, step tariff calculation, step tariff calculation function, personal income tax, solving square root inequality, simplifying solving square root inequality, solving dem
第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算
HCIA-USG Security Policy