Atcoder 070 D Transit Tree Path

题目链接http://abc070.contest.atcoder.jp/tasks/abc070_d

题目大意:给一棵树,一个节点K和Q次询问x,y,问x倒y经过节点k的距离。

解题思路:以K为根深搜标记距离即可。

加边的时候加两条边!!! 

数组大小开两倍!!!

爆int用ll!!!

代码:

 1 const int inf = 0x3f3f3f3f;
 2 const int maxn = 5e5 + 5;
 3 struct edge{
 4     int to, next;
 5     ll val;
 6 };
 7 edge edges[maxn];
 8 int tot, head[maxn], n;
 9 ll dis[maxn];
10 short vis[maxn];
11 
12 void init(){
13     memset(head, -1, sizeof(head));
14     memset(vis, 0, sizeof(vis));
15     tot = 0;
16 }
17 void addEdge(int u, int v, ll w){
18     edges[tot].to = v;
19     edges[tot].val = w;
20     edges[tot].next = head[u];
21     head[u] = tot++;
22 }
23 
24 void dfs(int u, ll val){
25     vis[u] = 1; dis[u] = val;
26     for(int i = head[u]; i != -1; i = edges[i].next){
27         int v = edges[i].to;
28         if(!vis[v]) dfs(v, val + edges[i].val);
29     }
30 }
31 
32 int main(){
33     init();
34     scanf("%d", &n);
35     for(int i = 1; i < n; i++) {
36         int u, v, w;
37         scanf("%d %d %d", &u, &v, &w);
38         addEdge(u, v, w);
39         addEdge(v, u, w);
40     }
41     int q, k;
42     scanf("%d %d", &q, &k);
43     dfs(k, 0);
44     while(q--){
45         int x, y;
46         scanf("%d %d", &x, &y);
47         printf("%lld\n", dis[x] + dis[y]);
48     }
49 }

题目:

D - Transit Tree Path


Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

You are given a tree with N vertices.
Here, a tree is a kind of graph, and more specifically, a connected undirected graph with N−1 edges, where N is the number of its vertices.
The i-th edge (1≤iN−1) connects Vertices ai and bi, and has a length of ci.

You are also given Q queries and an integer K. In the j-th query (1≤jQ):

  • find the length of the shortest path from Vertex xj and Vertex yj via Vertex K.

Constraints

  • 3≤N≤105
  • 1≤ai,biN(1≤iN−1)
  • 1≤ci≤109(1≤iN−1)
  • The given graph is a tree.
  • 1≤Q≤105
  • 1≤KN
  • 1≤xj,yjN(1≤jQ)
  • xjyj(1≤jQ)
  • xjK,yjK(1≤jQ)

Input

Input is given from Standard Input in the following format:

N  
a1 b1 c1  
:  
aN−1 bN−1 cN−1
Q K
x1 y1
:  
xQ yQ

Output

Print the responses to the queries in Q lines.
In the j-th line j(1≤jQ), print the response to the j-th query.


Sample Input 1

Copy
5
1 2 1
1 3 1
2 4 1
3 5 1
3 1
2 4
2 3
4 5

Sample Output 1

Copy
3
2
4

The shortest paths for the three queries are as follows:

  • Query 1: Vertex 2 → Vertex 1 → Vertex 2 → Vertex 4 : Length 1+1+1=3
  • Query 2: Vertex 2 → Vertex 1 → Vertex 3 : Length 1+1=2
  • Query 3: Vertex 4 → Vertex 2 → Vertex 1 → Vertex 3 → Vertex 5 : Length 1+1+1+1=4

Sample Input 2

Copy
7
1 2 1
1 3 3
1 4 5
1 5 7
1 6 9
1 7 11
3 2
1 3
4 5
6 7

Sample Output 2

Copy
5
14
22

The path for each query must pass Vertex K=2.


Sample Input 3

Copy
10
1 2 1000000000
2 3 1000000000
3 4 1000000000
4 5 1000000000
5 6 1000000000
6 7 1000000000
7 8 1000000000
8 9 1000000000
9 10 1000000000
1 1
9 10

Sample Output 3

Copy
17000000000
posted @ 2017-08-16 09:19  EricJeffrey  阅读(257)  评论(0编辑  收藏  举报