UVA 10308 Roads in the North

  一颗全连通且只有一条路从一个顶点到达另一个顶点,直接深搜,返回时返回最远的支路,且最远的支路加上第二远的支路和总路途最远比较,更新总路途最大,因为以一个点为中心走很多条路,最远的肯定是最大两条路的和,做法类似dp

  输入有点坑,输完最后一组数据直接EOF,处理输入搞了好久,gets返回的是指针,遇到EOF返回NULL,遇空白行字符串第一个字符为NULL结束符,返回指针不为NULL

 1 #include <iostream>
 2 #include <stdio.h>
 3 #include <vector>
 4 #include <algorithm>
 5 #include<cstring>
 6 #define MAX 10010
 7
 8 using namespace std;
 9
10 struct node
11 {
12     vector<int>next,dis;
13 };
14
15 node tree[MAX];
16 int vis[MAX],maxd;
17
18 int dfs(int x)
19 {
20     int max1=0,max2=0;
21     vector <int>dis;
22     vector<int>::iterator i,j,k;
23     for(i=tree[x].next.begin(),j=tree[x].dis.begin();i!=tree[x].next.end();i++,j++)
24         if(!vis[*i])
25         {
26             vis[*i]=1;
27             dis.push_back(dfs(*i)+*j);
28         }
29     if(dis.empty()) return 0;
30     for(k=i=dis.begin();i!=dis.end();i++)
31         if(max1<*i)
32         {
33             max1=*i;
34             k=i;
35         }
36     for(j=dis.begin();j!=dis.end();j++)
37         if(max2<*j&&j!=k) max2=*j;
38     maxd=max(maxd,max1+max2);//最远的和第二远的相加更新最远
39     //printf("maxd=%d max1=%d max2=%d\n",maxd,max1,max2);
40     return max1;//返回最远的
41 }
42
43 void init()
44 {
45     memset(vis,0,sizeof(vis));
46     for(int i=0;i<MAX;i++)
47     {
48         tree[i].next.clear();
49         tree[i].dis.clear();
50     }
51     maxd=-1;
52 }
53 int main()
54 {
55     //freopen("/home/user/桌面/in","r",stdin);
56     int a,b,c;
57     char s[100];
58     init();
59     while(1)
60     {
61         char*p=gets(s);
62         //printf("p=%p\n",p);
63         if(s[0]&&p)
64         {
65             //printf("s[0]=%d\n",s[0]);
66             //printf("1s[0]=%d\n",s[0]);
67             sscanf(s,"%d%d%d",&a,&b,&c);
68             tree[a].next.push_back(b);
69             tree[a].dis.push_back(c);
70             tree[b].next.push_back(a);
71             tree[b].dis.push_back(c);
72         }
73         else
74         {
75             /*printf("cal:s[0]=%d\nmaxd=%d\nvis[1]=%d\n",s[0],maxd,vis[1]);
76             for(int i=1;i<=6;i++)
77                 for(int j=0;tree[i].next.begin()+j!=tree[i].next.end();j++)
78                     printf("%d:%d %d\n",i,tree[i].next[j],tree[i].dis[j]);*/
79             //printf("s[0]=%d\n",s[0]);
80             vis[1]=1;
81             maxd=max(maxd,dfs(1));
82             printf("%d\n",maxd);
83             init();
84             if(p==NULL) break;
85         }
86     }
87     return 0;
88 }

时间: 2024-11-03 21:43:42

UVA 10308 Roads in the North的相关文章

UVa 10308 Roads in the North 树的直径

题目来源:UVa 10308 Roads in the North 题意:求距离最远的2点之间的距离 思路:裸的树的直径 或者树形DP #include <cstdio> #include <cstring> #include <queue> using namespace std; const int maxn = 100010; struct node { int to, w; node(){} node(int to, int w): to(to), w(w) {

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

poj2631 ?Roads in the North(求树的直径)

Roads in the North Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2941   Accepted: 1447 Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such

poj 2631 Roads in the North

题目连接 http://poj.org/problem?id=2631 Roads in the North Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to

POJ 2631 Roads in the North (求树的直径)

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village

题解报告:poj 2631 Roads in the North(最长链)

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village

Roads in the North (树的直径)

Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village twice. Give

UVA10308 Roads in the North 树的最长路径

Description Building and maintaining roads among communities in the far North is an expensive business. With this in mind, the roads are build such that there is only one route from a village to a village that does not pass through some other village

POJ2631 Roads in the North

题解: 树的最长路径,根据树的最长路径的性质,从任一一点bfs得到最远的点的位置,然后再从该点重新bfs一次就可以得到树的直径了 数学证明http://www.cnblogs.com/wuyiqi/archive/2012/04/08/2437424.html 代码: #include<iostream> #include<cstdio> #include<vector> #include<cstring> #include<cmath> #in