sicily 1024 Magic Island

题意:求无向图路径中的最大带权值.

解法:深搜

 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 }                                 

 

转载于:https://www.cnblogs.com/renaissance/p/3512938.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值