当前位置:网站首页>[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;
}
边栏推荐
- The 15 year old interviewer will teach you four unique skills that you must pass the interview
- 01 - QT OpenGL display OpenGL window
- FPGA 学习笔记:Vivado 2019.1 工程创建
- BUUCTF
- Luogu-p1107 [bjwc2008] Lei Tao's kitten
- Part 27 supplement (27) buttons of QML basic elements
- 第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算
- 3. Data binding
- QT -- qfileinfo file information reading
- Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
猜你喜欢

FAQs for datawhale learning!

2022-06-27 网工进阶(十二)IS-IS-开销类型、开销计算、LSP的处理机制、路由撤销、路由渗透

Flume learning notes

kubernetes集群搭建efk日志收集平台

Chapter 1: King Shehan miscalculated

Phpstudy set LAN access

Basic principle of LSM tree

FPGA 学习笔记:Vivado 2019.1 工程创建

NFT without IPFs and completely on the chain?

The 15 year old interviewer will teach you four unique skills that you must pass the interview
随机推荐
NFT without IPFs and completely on the chain?
Zhang Fei hardware 90 day learning notes - personal records on day 2, please see my personal profile / homepage for the complete
Nacos usage of micro services
Chapter 1: recursively find the factorial n of n!
FAQs for datawhale learning!
Typora charges, WTF? Still need support
[Yu Yue education] basic reference materials of manufacturing technology of Shanghai Jiaotong University
Part 27 supplement (27) buttons of QML basic elements
Teach you how to quickly recover data by deleting recycle bin files by mistake
6. Data agent object Defineproperty method
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
Xctf attack and defense world crypto master advanced area olddriver
Detailed explanation of shuttle unity interworking principle
第一章:求所有阶乘和数,大奖赛现场统分程序设计,三位阶乘和数,图形点扫描,递归求n的阶乘n!,求n的阶乘n!,舍罕王失算
Next spread
Chapter 1: King Shehan miscalculated
第一章:简化同码小数和s(d, n)
Don't be afraid of no foundation. Zero foundation doesn't need any technology to reinstall the computer system
I study database at station B (4): DQL
Phpstudy set LAN access