dfs + memory search, find the longest path between any two points

C、Coolest Ski Route

The meaning of problems: n points, m edges consisting of a directed graph, find the longest path between any two points

dfs memory search

#include<iostream>
#include<string.h>
#include<string>
#include<algorithm> #include<math.h> #include<string> #include<string.h> #include<vector> #include<utility> #include<map> #include<queue> #include<set> #define mx 0x3f3f3f3f #define ll long long using namespace std; int dis[1005],way[1005][1005],vis[1005],flag[1005]; int n,m; int dfs(int x) { if(vis[x]==1) return dis[x]; for(int i=1;i<=n;i++) { if(way[x][i]!=0) dis[x]=max(dis[x],dfs(i)+way[x][i]);//dis[x]表示以x为起点能走的最长路径是dis[x]  } vis[x]=1; return dis[x]; } int main() { cin>>n>>m; for(int i=0;i<m;i++) { int x,y,z; cin>>x>>y>>z; flag[y]=1;//标记终点 if(way[x][y]!=0) way[x][y]=max(way[x][y],z); else way[x][y]=z; } int ans=0; for(int i=. 1; I <= n-; I ++) // Enumeration search start point { IF (In Flag [I] == 0) // given is a directed graph, there can only search ans = start from the starting point to FIG max (ans , DFS (I));} COUT ANS << << endl; return 0 ;} / * . 5 2 15 2. 5. 1. 1. 4. 3. 17 12 is 2. 4. 5. 11. 4. 9. 6. 6. 1 2 2 2 2. 5. 4. 3. 3 132562124 * /

Guess you like

Origin www.cnblogs.com/-citywall123/p/11364108.html