麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

HDU 2586 How far away ?(map+lca【暴力水】)

2019-11-11 06:01:23
字體:
來源:轉載
供稿:網友

題目鏈接:http://acm.hdu.edu.cn/showPRoblem.php?pid=2586

How far away ?

Time Limit: 2000/1000 MS (java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14305 Accepted Submission(s): 5409

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 2 3 2 1 2 10 3 1 15 1 2 2 3

2 2 1 2 100 1 2 2 1

Sample Output 10 25 100 100

Source ECJTU 2009 Spring Contest

【中文題意】有t組數據,每組數據一個n,一個m,n代表點的個數,m代表詢問的次數。 下面n-1行每行輸入三個整數i,j,k,意思為,從i到j有一條長度為k的雙向邊。 下面m行每行兩個整數u,v;讓你輸出從u到v的最短距離。 【思路分析】運用lca的思想,以1為樹根DFS建立樹,記錄每個結點的深度和父結點。 用map記錄彼此之間的距離。 求的時候直接用暴力求LCA的方法求出距離即可【貌似沒有向我這么做的】。 【AC代碼】

#include<cstdio>#include<iostream>#include<cstring>#include<cmath>#include<queue>#include<stack>#include<map>#include<algorithm>using namespace std;int t,n,q;vector<int>G[50005];#define root 1int parent[50005];int depth[50005];map<int,map<int,int> >m;void dfs(int v,int p,int d){ parent[v]=p; depth[v]=d; for(int i=0; i<G[v].size(); i++) { if(G[v][i]!=p) { dfs(G[v][i],v,d+1); } }}void init(){ dfs(root,-1,0);}int lca(int u,int v){ int sum=0; while(depth[u]>depth[v]) { sum+=m[u][parent[u]]; u=parent[u]; } while(depth[v]>depth[u]) { sum+=m[v][parent[v]]; v=parent[v]; } while(u!=v) { sum+=m[v][parent[v]]; sum+=m[u][parent[u]]; u=parent[u]; v=parent[v]; } return sum;}int main(){ scanf("%d",&t); while(t--) { m.clear(); for(int i=0;i<n;i++) { G[i].clear(); } scanf("%d%d",&n,&q); int u,v,cost; for(int i=0; i<n-1; i++) { scanf("%d%d%d",&u,&v,&cost); G[u].push_back(v); G[v].push_back(u); m[u][v]=cost; m[v][u]=cost; //printf("%d**/n",m[u][v]); } init(); for(int i=1; i<=q; i++) { scanf("%d%d",&u,&v); printf("%d/n",lca(u,v)); } } return 0;}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产妞干网| 毛片小网站 | 亚洲精品久久久久久下一站 | 久久99精品久久久久久久久久久久 | 免费网站看毛片 | 久久精品黄 | 毛片一区二区三区四区 | 久久av喷吹av高潮av懂色 | 成人不卡在线观看 | 精品国产一区二区三区免费 | 91 在线| 欧美激情天堂 | 一级毛片在线免费观看 | 亚洲自拍第一 | 欧美成人一级 | 精品中文字幕久久久久四十五十骆 | 中文字幕网站在线 | 国产 一区 精品 | 国产色91 | 欧美精品| 羞羞答答视频 | www.99热视频 | 青青草最新网址 | 日韩美女电影 | 一级国产精品一级国产精品片 | av大全在线免费观看 | av影院在线播放 | 中文字幕网在线 | 国产成年人网站 | 国产一区二区三区高清 | 黄色国产在线观看 | 91久久99热青草国产 | 亚洲精品一区中文字幕 | 欧美爱爱视频免费看 | 欧美一级电影网站 | 毛片观看网址 | 国产在线1区 | 国产精品99精品 | 成人网在线观看 | 黄色毛片18 | 久久久久免费精品国产小说色大师 |