poj-1986 Distance Queries(lca+ST+dfs)
2016-04-11 18:46
381 查看
题目链接: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: 11531 | Accepted: 4068 | |
Case Time Limit: 1000MS |
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 题意: 给一棵树;问两点之间的距离; 思路: lca的模板题,一开始莫名其妙的wa了,看了好久才发现是一个地方写反了;后来又tle,把cin改成scanf就好了;看来还是不能用cin; AC代码:
/*2014300227 1986 Accepted 13984K 485MS G++ 2077B*/ #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const int N=4e4+4; typedef long long ll; const double PI=acos(-1.0); int n,m,cnt,head ,vis ,a[2*N],dep ,first ,dis ,num,dp[2*N][30],p ; struct Edge { int to,next,val; }; Edge edge[2*N]; void add_edge(int s,int e,int va) { edge[cnt].to=e; edge[cnt].next=head[s]; edge[cnt].val=va; head[s]=cnt++; } int dfs(int x,int deep) { vis[x]=1; first[x]=num; a[num++]=x; dep[x]=deep; for(int i=head[x];i!=-1;i=edge[i].next) { int y=edge[i].to; if(!vis[y]) { dis[y]=dis[x]+edge[i].val; dfs(y,deep+1); a[num++]=x; } } } int RMQ() { for(int i=0;i<num;i++) { dp[i][0]=a[i]; } for(int j=1;(1<<j)<=num;j++) { for(int i=0;i+(1<<j)-1<num;i++) { if(dep[dp[i][j-1]]<dep[dp[i+(1<<(j-1))][j-1]])dp[i][j]=dp[i][j-1]; else dp[i][j]=dp[i+(1<<(j-1))][j-1]; } } } int query(int l,int r) { int temp=(int)(log((r-l+1)*1.0)/log(2.0)); if(dep[dp[l][temp]]<dep[dp[r-(1<<temp)+1][temp]])return dp[l][temp]; else return dp[r-(1<<temp)+1][temp]; } int main() { while(scanf("%d%d",&n,&m)!=EOF) { cnt=0; num=0; for(int i=0;i<=n;i++) { vis[i]=0; head[i]=-1; } int u,v,w; char s[10]; for(int i=0;i<m;i++) { scanf("%d%d%d%s",&u,&v,&w,s); //cin>>u>>v>>w>>s; add_edge(u,v,w); add_edge(v,u,w); } dis[1]=0; dfs(1,0); RMQ(); int q,fx,fy; scanf("%d",&q); while(q--) { scanf("%d%d",&fx,&fy); int lca; if(first[fx]<first[fy]) { lca=query(first[fx],first[fy]); } else lca=query(first[fy],first[fx]); printf("%d\n",dis[fx]+dis[fy]-2*dis[lca]); } } return 0; }
相关文章推荐
- UITableView 滑动cell显示不全
- UIImagePikerController 浅析
- iOS彩票项目--第四天,新特性界面搭建,UICollectionViewController的初次使用
- java并发:AbstractQueuedSynchronizer的介绍和原理分析
- 前端工具HBuilder安装Sass插件
- iOS xib下设置UILabel等控件的边框
- easy-ui的datetimebox回显不能清除的问题
- uitableview处理section的不悬浮,禁止section停留的方法
- Android BluetoothAdapter详解
- hdoj1242 Rescue
- lintcode: Distinct Subsequences
- iOS UITableViewCell重用问题
- Build fast jar 打包,增加配置文件
- IOS开发之—— UITextView禁止Emoji输入
- 状态栏UIStatusBar(时间 电量那一栏)
- UITableView一些心得
- ios的控件UIScrollView的位置定位---------逐渐积累
- UITableView-(单元格的自定义方法)
- iOS开发-UIPickerView 简单介绍
- 百度编辑器 多图片批量上传 顺序混乱问题 ueditor