Example 6: Digging mines (recursive solution)

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;
}

 

Intelligent Recommendation

SSLOJ1071 digging mines

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...

P2196 digging mines [DFS]

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...

P2196 digging mines

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...

#155-[The longest road] digging mines

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...

Luo Valley [2196] digging mines

@ 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...

More Recommendation

Digging mines directly using Windbg

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...

Luo Valley P2196 digging mines

Original title: Portal Code:...

P2196 Digging mines - Dynamic Planning *

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...

[Search] [Simple DP] Digging mines

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 ...

[Dynamic Planning] Luogu P2196 Digging Mines

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...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top