zoukankan      html  css  js  c++  java
  • sicily 1024. Magic Island

    Constraints

    Time Limit: 1 secs, Memory Limit: 32 MB

    Description

    There are N cities and N-1 roads in Magic-Island. You can go from one city to any other. One road only connects two cities. One day, The king of magic-island want to visit the island from the capital. No road is visited twice. Do you know the longest distance the king can go.

    Input

    There are several test cases in the input
    A test case starts with two numbers N and K. (1<=N<=10000, 1<=K<=N). The cities is denoted from 1 to N. K is the capital.

    The next N-1 lines each contain three numbers X, Y, D, meaning that there is a road between city-X and city-Y and the distance of the road is D. D is a positive integer which is not bigger than 1000.
    Input will be ended by the end of file.

    Output

    One number per line for each test case, the longest distance the king can go.

    Sample Input

    3 1
    1 2 10
    1 3 20
    

    Sample Output

    20

    以前做的题,加了点注释

    #include<cstdio>
    #include<vector>
    using namespace std;
    #define MAX 10001
    struct road {
        int id;  // id of the road
        int end; // another end
        int len;
        road(int i, int e, int l) {
            id = i, end = e; len = l;
        }
    };
    
    bool visited[MAX];  // has road id been visited
                        // can't use attr since the road is bidirenctional,
                        // whereas visiting is one-direnctional => unnecessary search
    int maxLen;
    vector<road> roads[MAX];
    
    void dfs(int start, int total = 0) {
        // for each road connected to start
        for (int i = 0; i < roads[start].size(); i++) {
            // if it hasn't been visited
            if (!visited[roads[start][i].id]) {
                // visit it
                visited[roads[start][i].id] = true;
                total += roads[start][i].len;
                 
                // upadate
                if (maxLen < total) 
                    maxLen = total;
     
                 // start from its other end
                dfs(roads[start][i].end, total);
                
                // [IMPORTANT] backtracking
                total -= roads[start][i].len;
                visited[roads[start][i].id] = false;
            }
        }
    }
    
    int main() {
        int start, end, len;
        int n, k;
        while(scanf("%d%d", &n, &k) != EOF) {
            // initialize
            for (int i = 0; i < MAX; i++) {
                roads[i].clear();
                visited[i] = false;
            }
    
            for (int i = 1; i < n; i++) {
                scanf("%d%d%d", &start, &end, &len);
                // bidirenctional
                roads[start].push_back(road(i, end, len));
                roads[end].push_back(road(i, start, len));
            }
            
            maxLen = 0;
            dfs(k);
            printf("%d
    ", maxLen);
        }
    
        return 0;
    }
  • 相关阅读:
    HTML特效代码大全
    PHP网站加网站访问量统计
    定时显示隐藏
    加入收藏 设为首页
    IP和归属地
    手机站的拨打电话和发短信
    Shell运算
    Shell命令替换与变量替换
    $* 和 $@ 的区别
    Shell特殊变量列表
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/3505124.html
Copyright © 2011-2022 走看看