题意:求无向图路径中的最大带权值.
解法:深搜
1 // Problem#: 9859 2 // Submission#: 2661875 3 // The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License 4 // URI: http://creativecommons.org/licenses/by-nc-sa/3.0/ 5 // All Copyright reserved by Informatic Lab of Sun Yat-sen University 6 #include<iostream> 7 #include<vector> 8 #include<stack> 9 #include<memory.h> 10 using namespace std; 11 12 struct Road{ 13 int From; 14 int Dest; 15 int Dist; 16 Road(int f,int de,int di){ 17 From = f; 18 Dest = de; 19 Dist = di; 20 } 21 }; 22 vector<Road> roads[10001]; 23 bool visited[10001]; 24 25 int longest; 26 void DSF(int k,int cur){ 27 if(cur > longest){ 28 longest = cur; 29 } 30 visited[k] = true; 31 for(int i=0;i<roads[k].size();i++){ 32 int dest = roads[k][i].Dest; 33 int dist = roads[k][i].Dist; 34 if(visited[dest]) 35 continue; 36 DSF(dest,cur+dist); 37 } 38 //clear visited mark 39 visited[k] = false; 40 } 41 int main(){ 42 int N,K; 43 while(cin>>N>>K){ 44 int from,dest,dist; 45 longest = 0; 46 memset(roads,0,sizeof(roads)); 47 memset(visited,false,sizeof(visited)); 48 //input 49 for(int i=0;i<N-1;i++){ 50 cin >> from >> dest >> dist; 51 Road rF = Road(from,dest,dist); 52 Road rD = Road(dest,from,dist); 53 roads[from].push_back(rF); 54 roads[dest].push_back(rD); 55 } 56 DSF(K,longest); 57 cout << longest <<endl; 58 } 59 return 0; 60 }