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并发之阻塞队列
    37二叉树的深度+变式题输出最长路径上的所有节点
    36数字在排序数组中出现的次数
    35 两个链表的第一个公共结点
    34 数组中的逆序对+改进低效归并排序
    33 第一个只出现一次的字符+ASCII码
    编程练习赛42+二维动态数组的输入总结
    32 丑数
    在Ubuntu中启动./jmeter-server报错Server failed to start: java.rmi.RemoteException: Cannot start. ranxf is a loopback address.解决方法
    Ubuntu&Linux系统出现文件系统只读Read-only file system 的快速解决方法
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/3505124.html
Copyright © 2011-2022 走看看