• 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;
    }
  • 相关阅读:
    CCF CSP 201503-1 图像旋转
    CCF CSP 201403-1 相反数
    CCF CSP 201312-1 出现次数最多的数
    CCF CSP 201703-3 Markdown
    CCF CSP 201709-3 JSON查询
    CCF CSP 201709-2 公共钥匙盒
    CCF CSP 201709-1 打酱油
    CCF CSP 201604-4 游戏
    CCF CSP 201604-1 折点计数
    CCF CSP 201612-1 中间数
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/3505124.html
Copyright © 2020-2023  润新知