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;
    }
  • 相关阅读:
    封装transform函数(设置和获取transform的属性和属性值)
    layui第三方组件 inputTags 标签输入框
    layui编辑器(layedit)的实现和图片上传功能
    php编写抽奖后台实现抽奖概率计算
    laravel中使用事物
    laravel 使用jwt的基本应用(适于初始jwt)
    layui 下拉框动态添加数据(监听下拉框(select)事件)
    laravel后台账户登录验证(5.5.48版本)
    使用三目运算获取3个数值中最大的数值
    Laravel框架使用融云服务端SDK
  • 原文地址:https://www.cnblogs.com/joyeecheung/p/3505124.html
Copyright © 2011-2022 走看看