Brief description of the subject:Give a directed acyclic graph, and the weight of each node, find a weight and the largest path (starting arbitrary).
Solution ideas:This question satisfies the principle of no aftereffects and optimization, and can use dynamic programming, but I can't help but write deep search + memory, but it is essentially the same. The code is very short, the logic is also very clear, is the ordinary dfs + memory.
Code example:
#include<iostream>
#include<cstdio>
#include<vector>
using namespace std;
const int maxn = 250;
int w[maxn];
vector<int> G[maxn];
int f[maxn];
int path[maxn];
int most(int p){
if(f[p]) return f[p];
int ans = w[p];
for(int j = 0;j < G[p].size();j++){
int tmp = most(G[p][j]) + w[p];
if(ans < tmp){
path[p] = G[p][j];
ans = tmp;
}
}
return f[p] = ans;
}
void solve(int k){
for(int i = 1;i <= k;i++){
most(i);
}
int mi = -1,pos = 0;
for(int i = 1;i <= k;i++){
if(mi < f[i]){
pos = i;
mi = f[i];
}
}
cout << pos ;
while(path[pos]){
cout << "-" << path[pos];
pos = path[pos];
}
cout << endl;
cout << mi << endl;
}
int main(){
//freopen("123.in","r",stdin);
int k;
scanf("%d",&k);
for(int i = 1;i <= k;i++) scanf("%d",w+i);
int from,to;
while(scanf("%d%d",&from,&to) != EOF && (from || to)){
G[from].push_back(to);
}
solve(k);
return 0;
}
Description There is n cellar on a map (n <= 20), and a certain number of mines are buried in each cellar. At the same time, the connection path between the cellar is given. E.g: Input Output K1 K2...
Topic description There is NNN cellar (N ≤ 20) (N ≤ 15) (n ≤ 20) on a map, and a certain number of mines are buried in each cellar. At the same time, the connection path between the cellar is...
Topic description There is NN cellar (N \ LE 20) (N ≤ 20) on a map, and a certain number of mines are buried in each cellar. At the same time, the connection path between the cellar is given. After...
Description There are N mantles (N <= 200) on a map, and a certain number of mines are buried in each cellar. At the same time, the connection path between the mantles is given, and the paths are a...
@ Topic There are N (n <= 20) a cellar with mine, and the topic gives the number of mines in each cellar, and uses 0, 1 to indicate whether there is a way between the cellar. You can start from any...
Digging mines directly using Windbg One of the previous articles explores how to directly understand the mine, modify the mine area through Windbg. This time we are more direct, see how to win through...
Original title: Portal Code:...
Do you know why? answer: Full points Definition status f [i] f [i] is the maximum value ending with the iii node, f [i] = max \ {f [j] \} + a [i] \ \ (g [j] [i ] = 1) f [i] = max {f [j]} + a [i] (g [j...
Link: ⭐️⭐️ There is a series of paths in a few points. A person can start from a certain point until you go to the end. Each point has a value. Ask the maximum value you can get Because the amount of ...
main idea Given some points and the channels between them, ask for the maximum total point value Thought a simple one d p dp dp Assume f [ i ] f[i] f[i]Indicates the end point is i i iThe maximum valu...