POJ1986(LCA应用:求两结点之间距离)
2016-01-30 23:35
253 查看
Distance Queries
DescriptionFarmer 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
Time Limit: 2000MS | Memory Limit: 30000K | |
Total Submissions: 11304 | Accepted: 3985 | |
Case Time Limit: 1000MS |
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 离线:将所有查询输入完毕后再统一输出结果。 在线:查询一个输出一个。 dfs+并查集,离线
#include"cstdio" #include"cstring" #include"vector" using namespace std; typedef pair<int,int> P; const int MAXN=100005; int V,E; vector<P> G[MAXN]; vector<P> que[MAXN]; int par[MAXN]; int fnd(int x) { if(par[x]==x) return x; return par[x]=fnd(par[x]); } int vis[MAXN]; int ans[MAXN]; int d[MAXN]; void dfs(int u,int fa) { par[u]=u; for(int i=0;i<que[u].size();i++) { P no=que[u][i]; int v=no.first; if(vis[v]) ans[no.second]=d[u]+d[v]-2*d[fnd(v)]; } vis[u]=1; for(int i=0;i<G[u].size();i++) { P now=G[u][i]; if(now.first==fa) continue; d[now.first]=d[u]+now.second; dfs(now.first,u); par[now.first]=u; } } int main() { while(scanf("%d%d",&V,&E)!=EOF&&V) { for(int i=1;i<=V;i++) { G[i].clear(); que[i].clear(); } memset(vis,0,sizeof(vis)); memset(ans,0,sizeof(ans)); memset(d,0,sizeof(d)); for(int i=0;i<E;i++) { int u,v,cost; scanf("%d %d %d %*c",&u,&v,&cost); G[u].push_back(P(v,cost)); G[v].push_back(P(u,cost)); } int Q; scanf("%d",&Q); for(int i=1;i<=Q;i++) { int u,v; scanf("%d%d",&u,&v); que[u].push_back(P(v,i)); que[v].push_back(P(u,i)); } dfs(1,-1); for(int i=1;i<=Q;i++) { printf("%d\n",ans[i]); } } return 0; }
模板:RMQ求LCA在线算法(稀疏表实现RMQ)
#include"cstdio" #include"cstring" #include"vector" #include"cmath" #include"algorithm" using namespace std; const int MAXN=50005; typedef pair<int,int> P; vector<P> G[MAXN]; int V,E; int depth[MAXN*2]; int vs[MAXN]; int pos[MAXN]; int d[MAXN]; int cnt; int dep; void dfs(int u,int fa) { int cdep=++dep; depth[++cnt]=cdep; vs[cdep]=u; pos[u]=cnt; for(int i=0;i<G[u].size();i++) { P now=G[u][i]; if(now.first==fa) continue; d[now.first]=d[u]+now.second; dfs(now.first,u); depth[++cnt]=cdep; } } int dp[MAXN*2][20]; void init_rmq(int n) { for(int i=1;i<=n;i++) dp[i][0]=depth[i]; int m=floor(log(n*1.0)/log(2.0)); for(int j=1;j<=m;j++) for(int i=1;i<=n-(1<<j)+1;i++) dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); } int rmq(int a,int b) { int k=log((b-a+1)*1.0)/log(2.0); return min(dp[a][k],dp[b-(1<<k)+1][k]); } int LCA(int u,int v) { if(pos[u]>pos[v]) swap(u,v); int k=rmq(pos[u],pos[v]); return vs[k]; } int main() { while(scanf("%d%d",&V,&E)!=EOF) { for(int i=1;i<=V;i++) G[i].clear(); cnt=0; dep=0; memset(d,0,sizeof(d)); for(int i=0;i<E;i++) { int u,v,cost; scanf("%d%d%d %*c",&u,&v,&cost); G[u].push_back(P(v,cost)); G[v].push_back(P(u,cost)); } dfs(1,-1); init_rmq(cnt); int Q; scanf("%d",&Q); while(Q--) { int u,v; scanf("%d%d",&u,&v); printf("%d\n",d[u]+d[v]-2*d[LCA(u,v)]); } } return 0; }
相关文章推荐
- mysql-XtraBackup备份
- Sqlite3 批量导入数据
- Nginx+Varnish
- 1041. 考试座位号(15)
- 使用yum源安装CDH Hadoop集群
- 【Android】【笔记】《Android 入门经典》part 5 程序调试
- 蓝桥杯 - 最小乘积(基本型)
- C++基本概念——赋值&&初始化
- iOS 安全攻防系列(三):sqlite3数据库
- 开发环境解决 kafka Failed to send messages after 3 tries
- spring 零散知识点
- php 实现简单爬虫
- 使用好雨云的10大理由!
- 安卓工程版本控制哪些文件可以忽略
- 【Android】【笔记】《Android 入门经典》part 4 Android布局管理
- BZOJ 1064
- 新工作 Day24 周六
- 【Android】【笔记】《Android 入门经典》part 3 安卓开发组件
- Java判断文本文件编码格式以及读取
- 基于SocketChannel的21点小游戏