UVA - 10308 - Roads in the North (DFS)

來源:互聯網
上載者:User

標籤:acm   uva   gcd   


題目傳送:UVA - 10308


思路:就是樹的遍曆,DFS即可,注意輸入


AC代碼:

#include <cstdio>#include <cstring>#include <iostream>#include <string>#include <sstream>#include <algorithm>#include <cmath>#include <queue>#include <stack>#include <vector>#include <map>#include <set>#include <deque>#include <cctype>#define LL long long#define INF 0x7fffffffusing namespace std;struct node {int e, w;node(int _e, int _w) : e(_e), w(_w) { }}; const int maxn = 10010;vector<node> G[maxn];int ans;int dfs(int u, int fa) {int maxx = 0;//maxx表示當前結點到葉子結點的最大權值int anss;//anss表示當前結點到葉子結點的權值 int len = G[u].size();for(int i = 0; i < len; i ++) {if(G[u][i].e != fa) {anss = dfs(G[u][i].e, u) + G[u][i].w;ans = max(ans, anss + maxx);//更新答案 maxx = max(anss, maxx);//更新最大權值 }}return maxx;}int main() {string s;    while (!cin.eof()) {    for(int i = 0; i < maxn; i ++)G[i].clear();        getline(cin, s);        while (s.length() > 0 && !cin.eof())        {            stringstream ss;            ss << s;            int u, v, w;            ss >> u >> v >> w;            G[u].push_back(node(v, w));            G[v].push_back(node(u, w));            getline(cin, s);        }        ans = 0;dfs(1, -1);printf("%d\n", ans);    }return 0;}





UVA - 10308 - Roads in the North (DFS)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.