hdoj_2586How far away ? && poj_1986Distance Queries

来源:互联网 发布:怎么查找淘宝评论过的 编辑:程序博客网 时间:2024/03/29 05:37

How far away ?

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2931    Accepted Submission(s): 1092


Problem Description
There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B"? Usually it hard to answer. But luckily int this village the answer is always unique, since the roads are built in the way that there is a unique simple path("simple" means you can't visit a place twice) between every two houses. Yout task is to answer all these curious people.
 

Input
First line is a single integer T(T<=10), indicating the number of test cases.
  For each test case,in the first line there are two numbers n(2<=n<=40000) and m (1<=m<=200),the number of houses and the number of queries. The following n-1 lines each consisting three numbers i,j,k, separated bu a single space, meaning that there is a road connecting house i and house j,with length k(0<k<=40000).The houses are labeled from 1 to n.
  Next m lines each has distinct integers i and j, you areato answer the distance between house i and house j.
 

Output
For each test case,output m lines. Each line represents the answer of the query. Output a bland line after each test case.
 

Sample Input
23 21 2 103 1 151 22 32 21 2 1001 22 1
 

Sample Output
1025100
100
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#pragma warning(disable : 4996)const int MAXN = 40002;typedef struct Edge{int v;int w;int next;}Edge;int cnt, edge_head[MAXN], ask_head[MAXN];int father[MAXN], dist[MAXN], ans[201];bool visited[MAXN];Edge edge[2 * MAXN], ask[404];int find(int x){if (x != father[x]){father[x] = find(father[x]);}return father[x];}void add_edge(int x, int y, int z){edge[cnt].v = y;edge[cnt].w = z;edge[cnt].next = edge_head[x];edge_head[x] = cnt++;}void add_ask(int x, int y, int cast){ask[cnt].v = y;ask[cnt].w = cast;ask[cnt].next = ask_head[x];ask_head[x] = cnt++;}void Tarjan(int k){visited[k] = true;for (int i = ask_head[k]; i != 0; i = ask[i].next){if (visited[ask[i].v]){ans[ask[i].w] = dist[ask[i].v] + dist[k] - 2 * dist[find(ask[i].v)];}}for (int i = edge_head[k]; i != 0; i = edge[i].next){if (!visited[edge[i].v]){dist[edge[i].v] = dist[k] + edge[i].w;Tarjan(edge[i].v);edge[i].v = find(edge[i].v);father[edge[i].v] = k;}}}int main(){freopen("in.txt", "r", stdin);int i, m, n;int x, y, z;int t;scanf("%d", &t);while (t--){scanf("%d %d", &n, &m);for (i = 1; i <= n; i++){edge_head[i] = ask_head[i] = 0;father[i] = i;visited[i] = false;}cnt = 1;for (i = 1; i < n; i++){scanf("%d%d%d", &x, &y, &z);add_edge(x, y, z);add_edge(y, x, z);}cnt = 1;for (i = 1; i <= m; i++){scanf("%d%d", &x, &y);add_ask(x, y, i);add_ask(y, x, i);}dist[1] = 0;Tarjan(1);for (i = 1; i <= m; i++)printf("%d\n", ans[i]);}return 0;}

Distance Queries
Time Limit: 2000MS Memory Limit: 30000KTotal Submissions: 7456 Accepted: 2619Case Time Limit: 1000MS

Description

Farmer John's cows refused to run in his marathon since he chose a path much too long for their leisurely lifestyle. He therefore wants to find a path of a more reasonable length. The input to this problem consists of the same input as in "Navigation Nightmare",followed by a line containing a single integer K, followed by K "distance queries". Each distance query is a line of input containing two integers, giving the numbers of two farms between which FJ is interested in computing distance (measured in the length of the roads along the path between the two farms). Please answer FJ's distance queries as quickly as possible! 

Input

* Lines 1..1+M: Same format as "Navigation Nightmare" 

* Line 2+M: A single integer, K. 1 <= K <= 10,000 

* Lines 3+M..2+M+K: Each line corresponds to a distance query and contains the indices of two farms. 

Output

* Lines 1..K: For each distance query, output on a single line an integer giving the appropriate distance. 

Sample Input

7 61 6 13 E6 3 9 E3 5 7 S4 1 3 N2 4 20 W4 7 2 S31 61 42 6

Sample Output

13336

#include <iostream>#include <cstdio>#include <cstring>using namespace std;#pragma warning(disable : 4996)const int MAXN = 10000005;typedef struct Edge{int v;int w;int next;}Edge;int cnt, edge_head[MAXN], ask_head[MAXN];int father[MAXN], dist[MAXN], ans[MAXN];bool visited[MAXN];Edge edge[2 * MAXN], ask[MAXN];int find(int x){if (x != father[x]){father[x] = find(father[x]);}return father[x];}void add_edge(int x, int y, int z){edge[cnt].v = y;edge[cnt].w = z;edge[cnt].next = edge_head[x];edge_head[x] = cnt++;}void add_ask(int x, int y, int cast){ask[cnt].v = y;ask[cnt].w = cast;ask[cnt].next = ask_head[x];ask_head[x] = cnt++;}void Tarjan(int k){visited[k] = true;for (int i = ask_head[k]; i != 0; i = ask[i].next){if (visited[ask[i].v]){ans[ask[i].w] = dist[ask[i].v] + dist[k] - 2 * dist[find(ask[i].v)];}}for (int i = edge_head[k]; i != 0; i = edge[i].next){if (!visited[edge[i].v]){dist[edge[i].v] = dist[k] + edge[i].w;Tarjan(edge[i].v);edge[i].v = find(edge[i].v);father[edge[i].v] = k;}}}int main(){freopen("in.txt", "r", stdin);int i, m, n;int x, y, z;int q;scanf("%d %d", &n, &m);for (i = 1; i <= n; i++){edge_head[i] = ask_head[i] = 0;father[i] = i;visited[i] = false;}cnt = 1;for (i = 1; i <= m; i++){scanf("%d %d %d %*c", &x, &y, &z);add_edge(x, y, z);add_edge(y, x, z);}scanf("%d", &q);cnt = 1;for (i = 1; i <= q; i++){scanf("%d%d", &x, &y);add_ask(x, y, i);add_ask(y, x, i);}dist[1] = 0;Tarjan(1);for (i = 1; i <= q; i++)printf("%d\n", ans[i]);return 0;}


原创粉丝点击