UVA10308Roads in the North(dfs)

题目链接

题目大意:给你多条道路,每条道路都连着两个不同的城市,并且给你这条路的距离,现在要求你找出离得最远的两个城市之间的距离。

解题思路:一开始,想用dfs,可是没有想到只需要任意找个节点dfs一次就可以了,还想着如果每个点都找一次,那么肯定行不通。看了题解后才发现只需要找一次就可以了,因为题目给的是一棵树,而且两个节点之间的距离可以通过dfs来得到,只要在过程中记录下最大值就可以。

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>

using namespace std;

const int maxn = 10005;
int vis[maxn];
int ans;

struct node {
    int id, val;
    node (int id = 0, int val = 0) {
        this->id = id;
        this->val = val;
    }
};
vector<node> v[maxn];

int dfs (int r) {

    int num = 0;
    int n = v[r].size();
    vis[r] = 1;
    for (int i = 0; i < n; i++)
        if (!vis[v[r][i].id]) {
            int tmp = dfs(v[r][i].id) + v[r][i].val;
            ans = max (ans, tmp + num);
            num = max (num, tmp);
        }
    vis[r] = 0;
    return num;
}

int main () {

    char str[1000];
    int a, b, val;
    while (true) {

        ans = 0;
        for (int i = 1; i <= maxn - 5; i++)
            v[i].clear();

        while (gets(str) != NULL && str[0] != ‘\0‘) {
            sscanf (str, "%d%d%d", &a, &b, &val);
            v[a].push_back(node(b, val));
            v[b].push_back(node(a, val));
        }

        dfs(1);
        printf ("%d\n", ans);

        if (str[0] != ‘\0‘)
            break;
    }
    return 0;
}
时间: 2024-11-03 22:31:09

UVA10308Roads in the North(dfs)的相关文章

poj 3009 Curling 2.0 (BFS)

题目大意要求把一个冰壶从起点"2"用最少的步数移动到终点"3" 其中0为移动区域,1为石头区域,冰壶一旦想着某个方向运动就不会停止,也不会改变方向(想想冰壶在冰上滑动),除非冰壶撞到石头1 或者 到达终点 3 冰壶撞到石头后,冰壶会停在石头前面,此时(静止状态)才允许改变冰壶的运动方向,而该块石头会破裂,石头所在的区域由1变为0. 也就是说,冰壶撞到石头后,并不会取代石头的位置. 终点是一个摩擦力很大的区域,冰壶若到达终点3,就会停止在终点的位置不再移动. 要先明确

UVA - 10308 - Roads in the North (DFS)

题目传送:UVA - 10308 思路:就是树的遍历,DFS即可,注意输入 AC代码: #include <cstdio> #include <cstring> #include <iostream> #include <string> #include <sstream> #include <algorithm> #include <cmath> #include <queue> #include <s

HDU 1045--Fire Net【DFS 经典】

Fire Net Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 7912    Accepted Submission(s): 4505 Problem Description Suppose that we have a square city with straight streets. A map of a city is a

HDU 4770 Lights Against Dudely 暴力枚举+dfs

又一发吐血ac,,,再次明白了用函数(代码重用)和思路清晰的重要性. 11779687 2014-10-02 20:57:53 Accepted 4770 0MS 496K 2976 B G++ czy Lights Against Dudely Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1360    Accepted Subm

hdu 5024 Wang Xifeng&#39;s Little Plot (dfs+暴力)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 194    Accepted Submission(s): 131 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

NYOJ 587 blockhouses 【DFS】

blockhouses 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle th

Martian Mining_DP&amp;&amp;DFS

Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 2567   Accepted: 1595 Description The NASA Space Center, Houston, is less than 200 miles from San Antonio, Texas (the site of the ACM Finals this year). This is the place where the astronau

HDU 1045 Fire Net(DFS)

Fire Net Problem Description Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings t

hdu1035 Robot Motion (DFS)

Robot Motion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 8180    Accepted Submission(s): 3771 Problem Description A robot has been programmed to follow the instructions in its path. Instruc