博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU2586 How far away ?
阅读量:4136 次
发布时间:2019-05-25

本文共 3294 字,大约阅读时间需要 10 分钟。

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
23 21 2 103 1 151 22 32 21 2 1001 22 1
 

Sample Output
1025100100

Tarjan算法:

/*//最近公共祖先题目描述:给定一棵带权树,求u,v两节点的最短长度算法思想:找到u,v两点的最近公共祖先lca(v)若dis[u]表示根节点到u的长度,则res=dis[u]+dis[v]-2*dis[lca(v)]*/#include 
#include
#define NN 40002 // number of house#define MM 202 // number of queryusing namespace std;typedef struct node{ int v;//节点 int d;//v节点与nxt相连的权值 struct node *nxt;}NODE;NODE *Link1[NN];//邻接表,Link1[u]表示与u相连所有节点链NODE edg1[NN * 2]; // 树中的边NODE *Link2[NN];NODE edg2[NN * 2]; // 询问的点对int idx1, idx2, N, M;int res[MM][3]; // 记录结果,res[i][0]: u res[i][1]: v res[i][2]: lca(u, v)int fat[NN];//并查集表int vis[NN];//记录访问点int dis[NN];//根到i节点的长度//前插法void Add(int u, int v, int d, NODE edg[], NODE *Link[], int &idx){ edg[idx].v = v; edg[idx].d = d; edg[idx].nxt = Link[u]; Link[u] = edg + idx++; edg[idx].v = u; edg[idx].d = d; edg[idx].nxt = Link[v]; Link[v] = edg + idx++;}int find(int x){ // 并查集路径压缩 if(x != fat[x]){ return fat[x] = find(fat[x]); } return x;}void Tarjan(int u){ vis[u] = 1; fat[u] = u; for (NODE *p = Link2[u]; p; p = p->nxt){ if(vis[p->v]){ res[p->d][2] = find(p->v); // 存的是最近公共祖先结点 } } for (NODE *p = Link1[u]; p; p = p->nxt){ if(!vis[p->v]){ dis[p->v] = dis[u] + p->d; Tarjan(p->v); fat[p->v] = u; } }}int main() { freopen("C:\\in.txt","r",stdin); int T, i, u, v, d; scanf("%d", &T); while(T--){ scanf("%d%d", &N, &M); idx1 = 0; memset(Link1, 0, sizeof(Link1)); for (i = 1; i < N; i++){ scanf("%d%d%d", &u, &v, &d); Add(u, v, d, edg1, Link1, idx1); } idx2 = 0; memset(Link2, 0, sizeof(Link2)); for (i = 1; i <= M; i++){ scanf("%d%d", &u, &v); Add(u, v, i, edg2, Link2, idx2); res[i][0] = u; res[i][1] = v; } memset(vis, 0, sizeof(vis)); dis[1] = 0; Tarjan(1); for (i = 1; i <= M; i++){ printf("%d\n", dis[res[i][0]] + dis[res[i][1]] - 2 * dis[res[i][2]]); } } return 0;}

转载地址:http://zdvvi.baihongyu.com/

你可能感兴趣的文章
Error: An App ID with identifier "*****" is not avaliable. Please enter a different string.
查看>>
X-code beta 开发iWatch项目,运行没有错误,但是某些操作一点就崩,而且找不错误的原因场景一
查看>>
Xcode 报错: Extra argument in call
查看>>
iTunes Connect 上传APP报错: Communication error. please use diagnostic mode to check connectivity.
查看>>
#import <Cocoa/Cocoa.h> 报错 Lexical or Preprocessor Issue 'Cocoa/Cocoa.h' file not found
查看>>
`MQTTClient (~> 0.2.6)` required by `Podfile`
查看>>
X-Code 报错 ld: library not found for -lAFNetworking
查看>>
Bitcode
查看>>
If you want to see the backtrace, please set CG_CONTEXT_SHOW_BACKTRACE environmental variable.
查看>>
3.5 YOLO9000: Better,Faster,Stronger(YOLO9000:更好,更快,更强)
查看>>
iOS菜鸟学习--如何避免两个按钮同时响应
查看>>
How to access the keys in dictionary in object-c
查看>>
iOS菜鸟学习—— NSSortDescriptor的使用
查看>>
hdu 3787 hdoj 3787
查看>>
hdu 3790 hdoj 3790
查看>>
hdu 3789 hdoj 3789
查看>>
hdu 3788 hdoj 3788
查看>>
zju 1003 zoj 1003
查看>>
zju 1004 zoj 1004
查看>>
zju 1005 zoj 1005
查看>>