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;
    }
  • 相关阅读:
    解决跨域问题的后端解决方案
    java-反射
    验证接口实现类对于接口中所有方法是否都要重写?
    完成Triangle时遇到的问题
    刚知道的一种算法
    完成ATM编程练习题时遇到的问题
    完成Account编程练习遇到的问题
    完成StopWatch编程练习题时遇到的一些问题
    Window环境下Java备份和恢复Mysql数据
    IDEA导入项目不下载依赖问题
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/3505124.html
Copyright © 2011-2022 走看看