标签:des style blog http color java 使用 os
先上题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4936 Accepted Submission(s): 1866
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #include <cmath> 5 #define MAX 40002 6 using namespace std; 7 8 typedef struct{ 9 int from,to,next,l; 10 }Edge; 11 12 Edge e[MAX<<1]; 13 int p[MAX],fa[MAX],tot; //fa:保存当前点的父亲是谁 14 int r[MAX<<1],all; //r保存先序遍历的序列 15 int d[MAX]; //d保存每个点的深度 16 int dp[MAX<<1][30],len[MAX]; //len保存当前点回到父亲节点的路劲长度 17 int vis[MAX]; //保存第一次出现在r[]里面的位置 18 19 inline void add(int u,int v,int l){ 20 e[tot].from=u; e[tot].to=v; e[tot].l=l; e[tot].next=p[u]; p[u]=tot++; 21 } 22 23 void dfs(int u,int par,int dep,int l){ 24 d[u]=dep; 25 fa[u]=par; 26 vis[u]=all; 27 r[all++]=u; 28 len[u]=l; 29 for(int i=p[u];i!=-1;i=e[i].next){ 30 if(vis[e[i].to]==-1){ 31 dfs(e[i].to,u,dep+1,e[i].l); 32 r[all++]=u; 33 } 34 } 35 } 36 37 void rmq(){ 38 for(int i=0;i<all;i++) dp[i][0]=d[r[i]]; 39 for(int j=1;(1<<j)<=all;j++){ 40 for(int i=0;i+(1<<j)-1<all;i++){ 41 dp[i][j]=min(dp[i][j-1],dp[i+(1<<(j-1))][j-1]); 42 } 43 } 44 } 45 46 int query(int x,int y){ 47 x = vis[x]; 48 y = vis[y]; 49 if(x>y) swap(x,y); 50 int minn,k; 51 k = log(y-x+1.0) / log(2.0); 52 minn=min(dp[x][k],dp[y-(1<<k)+1][k]); 53 return minn; 54 } 55 56 int dist(int x,int pxy){ 57 int sum=0; 58 while(d[x]!=pxy){ 59 sum+=len[x]; 60 x = fa[x]; 61 } 62 return sum; 63 } 64 65 int main() 66 { 67 int t,n,m,x,y,l,pxy,sum; 68 //freopen("data.txt","r",stdin); 69 scanf("%d",&t); 70 while(t--){ 71 scanf("%d %d",&n,&m); 72 tot=0; 73 memset(p,-1,sizeof(p)); 74 for(int i=1;i<n;i++){ 75 scanf("%d %d %d",&x,&y,&l); 76 add(x,y,l); 77 add(y,x,l); 78 } 79 all=0; 80 memset(vis,-1,sizeof(vis)); 81 dfs(1,1,1,0); 82 rmq(); 83 while(m--){ 84 scanf("%d %d",&x,&y); 85 pxy = query(x,y); 86 sum = dist(x,pxy) + dist(y,pxy); 87 printf("%d\n",sum); 88 } 89 } 90 return 0; 91 }
HDU - 2586 - How far away ?,布布扣,bubuko.com
标签:des style blog http color java 使用 os
原文地址:http://www.cnblogs.com/sineatos/p/3879474.html