Luo gu 1294 master go for a walk
Topic background
Master recently fell in love. It's just a single acacia. "Even if it is a single love, but also a complete relationship," The master has never given up on its pursuit. Today, on this sunny morning, the sun rises slowly from the west. So it found a master, hope in the morning before the start and master together in the Ngau Tau Mountain Walk together. The master of course will not give up this dream opportunity, he has prepared everything.
Title Description
There are n scenic spots on the Ngau Tau Mountain, and there is a total of M in the scenic spots 22. The master of that it, do not like the process of too exciting, so those who do not have the road to the view of the experts will not choose to go. In addition, she does not like to go to the same viewing spot more than once. And the master want to let them together the longest distance (it will not be a master when viewing the scene), the known master shuttle can let them in any one of the viewing spots, but also at any of the end of a view.
Input/output format
Input format:
The first line, two spaces separated by N, M, and M, is the information for each of the trails: two-view attraction number, length.
Output format:
An integer that represents their longest companion distance.
Input/Output sample
Input Sample # #:
4 6
1 2 10
2 3 20
3 4 30
4 1 40
1 3 50
2 4 60
Sample # # of output:
150
Description
For 100% of data: n≤20,m≤50, there will be no more than one trail connection between the points of view 22.
Ideas
Dfs.
Deep search for each node.
Code
1#include <iostream>2 using namespacestd;3 4 Const intMAXN = -;5 6 intG[MAXN][MAXN];7 intVIS[MAXN];8 intN,m,ans;9 Ten voidDfsintUintsum) { One if(Sum>ans) ans=sum; A for(intI=1; i<=n;i++) - if(!vis[i] &&G[u][i]) { -vis[i]=1; theDFS (i,sum+g[u][i]); -vis[i]=0; - } - } + - intMain () { +Ios::sync_with_stdio (false); ACin>>n>>m; at intu,v,w; - for(intI=0; i<m;i++) { -Cin>>u>>v>>W; -g[u][v]=g[v][u]=W; - } - for(intI=1; i<=n;i++) { invis[i]=1; -DFS (I,0); tovis[i]=0; + } -cout<<ans; the return 0; *}
Rokua P1294 to go for a walk