poj 1986 Distance Queries 【LCA转RMQ 裸题】【求两点最短距离】
2015-08-10 10:40
423 查看
Distance Queries
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
Sample Output
Hint
Farms 2 and 6 are 20+3+13=36 apart.
LCA的学习 到一段落了,下面要复习学过的图论了。
题意:给你N个点和M条边以及边的距离(边的方向不影响做题),有Q次查询,查询两点最短距离。
纯裸题,只当练敲自己的模版了。 LCA转RMQ 看这里:点我
AC代码:
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 10510 | Accepted: 3717 | |
Case Time Limit: 1000MS |
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 6 1 6 13 E 6 3 9 E 3 5 7 S 4 1 3 N 2 4 20 W 4 7 2 S 3 1 6 1 4 2 6
Sample Output
13 3 36
Hint
Farms 2 and 6 are 20+3+13=36 apart.
LCA的学习 到一段落了,下面要复习学过的图论了。
题意:给你N个点和M条边以及边的距离(边的方向不影响做题),有Q次查询,查询两点最短距离。
纯裸题,只当练敲自己的模版了。 LCA转RMQ 看这里:点我
AC代码:
#include <cstdio> #include <cstring> #include <algorithm> #define MAXN 100000+100 using namespace std; struct Edge { int from, to, val, next; }; Edge edge[MAXN<<1]; int head[MAXN], edgenum; int vs[MAXN<<1], depth[MAXN<<1]; int id[MAXN]; int dist[MAXN]; int dfs_clock; int N, M; void init() { edgenum = 0; memset(head, -1, sizeof(head)); } void addEdge(int u, int v, int w) { Edge E = {u, v, w, head[u]}; edge[edgenum] = E; head[u] = edgenum++; } void getMap() { int a, b, c; char op[5]; while(M--) { scanf("%d%d%d%s", &a, &b, &c, op); addEdge(a, b, c); addEdge(b, a, c); } } void DFS(int u, int fa, int d) { id[u] = dfs_clock; vs[dfs_clock] = u; depth[dfs_clock++] = d; for(int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; if(v == fa) continue; dist[v] = dist[u] + edge[i].val; DFS(v, u, d+1); vs[dfs_clock] = u; depth[dfs_clock++] = d; } } void find_depth() { dfs_clock = 1; memset(vs, 0, sizeof(vs)); memset(depth, 0, sizeof(depth)); memset(id, 0, sizeof(id)); memset(dist, 0, sizeof(dist)); DFS(1, -1, 0); } int dp[MAXN<<1][30]; void RMQ_init(int NN) { for(int i = 1; i <= NN; i++) dp[i][0] = i; for(int j = 1; (1<<j) <= NN; j++) { for(int i = 1; i + (1<<j) - 1 <= NN; i++) { int a = dp[i][j-1]; int b = dp[i + (1<<(j-1))][j-1]; if(depth[a] < depth[b]) dp[i][j] = a; else dp[i][j] = b; } } } int query(int L, int R) { int k = 0; while(1 << (k+1) <= R-L+1) k++; int a = dp[L][k]; int b = dp[R - (1<<k) + 1][k]; if(depth[a] < depth[b]) return a; else return b; } int LCA(int u, int v) { int x = id[u]; int y = id[v]; if(x < y) return vs[query(x, y)]; else return vs[query(y, x)]; } void solve() { int Q, a, b; scanf("%d", &Q); while(Q--) { scanf("%d%d", &a, &b); printf("%d\n", dist[a] + dist[b] - 2 * (dist[LCA(a, b)])); } } int main() { while(scanf("%d%d", &N, &M) != EOF) { init(); getMap(); find_depth(); RMQ_init(dfs_clock - 1); solve(); } return 0; }
相关文章推荐
- poj 1458 Common Subsequence (LCS(最长公共子序列))
- HDOJ--1159--Common Subsequence(lcs算法)
- 几本不错的开源书(to be continued)
- [UIPopoverController dealloc] reached while popover is still visible
- Common Subsequence
- [ios] UI TableView
- 什么是队列(Queue)?
- easyui中带checkbox框的tree
- POJ 1458:Common Subsequence【最长子序列】
- POJ1679 The Unique MST(kru)
- Common Subsequence
- 最长公共子序列(Longest common subsequence)
- UIScrollView - 点击跳转
- 【Android UI】TabHos之基础实现
- UIScrollView
- poj 1458 Common Subsequence【LCS】
- Buildroot用户指南
- 【代码优化】当许多构造函数的参数,请考虑使用builder模式
- 【Android UI】ViewFlipper实现广告轮播图
- poj 1458 Common Subsequence【LCS】