zoj 3195 Design the city

来源:互联网 发布:家长控制电脑软件 编辑:程序博客网 时间:2024/05/16 04:24
/*Design the cityTime Limit: 1 Second      Memory Limit: 32768 KBCerror is the mayor of city HangZhou. As you may know, the traffic system of this city is so terrible, that there are traffic jams everywhere. Now, Cerror finds out that the main reason of them is the poor design of the roads distribution, and he want to change this situation.In order to achieve this project, he divide the city up to N regions which can be viewed as separate points. He thinks that the best design is the one that connect all region with shortest road, and he is asking you to check some of his designs.Now, he gives you an acyclic graph representing his road design, you need to find out the shortest path to connect some group of three regions.InputThe input contains multiple test cases! In each case, the first line contian a interger N (1 < N < 50000), indicating the number of regions, which are indexed from 0 to N-1. In each of the following N-1 lines, there are three interger Ai, Bi, Li (1 < Li < 100) indicating there's a road with length Li between region Ai and region Bi. Then an interger Q (1 < Q < 70000), the number of group of regions you need to check. Then in each of the following Q lines, there are three interger Xi, Yi, Zi, indicating the indices of the three regions to be checked.Process to the end of file.OutputQ lines for each test case. In each line output an interger indicating the minimum length of path to connect the three regions.Output a blank line between each test cases.Sample Input40 1 10 2 10 3 121 2 30 1 250 1 10 2 11 3 11 4 120 1 21 0 3Sample Output3222*//*ps:本题求得是,联通三个点的最短距离。由于图的结构是一个图,所以对于任意两点间,有且只有一条路径。所以我们想到求三个点中任意两点间的距离,然后结果除以2即可。*/#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>#include <cmath>using namespace std;typedef long long LL;const int INF = 0x3f3f3f3f;const int Tmaxn = 50000+10;const int Qmaxn = 70000+10;int dist[Tmaxn],vist[Tmaxn],fat[Tmaxn],head_tree[Tmaxn],head_query[Tmaxn];int N,M,cnt_tree,cnt_query;int res[Qmaxn][4];struct tree{int to,w,next;}Tree[Tmaxn<<1];struct query{int to,next;int id,num;}Query[Qmaxn*6];void Init(){cnt_query = cnt_tree = 0;for(int i = 0; i < N; i++){dist[i] = vist[i] = 0;head_tree[i] = head_query[i] = -1;fat[i] = i;}}void ADD_tree(int u,int v,int w){Tree[cnt_tree].to = v;Tree[cnt_tree].w = w;Tree[cnt_tree].next = head_tree[u];head_tree[u] = cnt_tree++;}void ADD_query(int u,int v,int id,int num){Query[cnt_query].to = v;Query[cnt_query].id = id;Query[cnt_query].num = num;Query[cnt_query].next = head_query[u];head_query[u] = cnt_query++;}int Find(int x){if(x != fat[x])x = Find(fat[x]);return x;}void Tarjan(int u,int father){for(int i = head_tree[u]; ~i; i = Tree[i].next){int v = Tree[i].to;if(!vist[v] && v!=father){dist[v] = dist[u]+Tree[i].w;Tarjan(v,u);fat[v] = u;}}vist[u] = 1;for(int i = head_query[u]; ~i; i = Query[i].next){int v = Query[i].to;if(vist[v]){int temp = Find(v);res[Query[i].id][Query[i].num] = dist[u]+dist[v]-2*dist[temp];}}}int main(){int u,v,w;int cas = 0;while(~scanf("%d",&N)){cas++;Init();for(int i = 1; i < N; i++){scanf("%d %d %d",&u,&v,&w);ADD_tree(u,v,w);ADD_tree(v,u,w);}scanf("%d",&M);for(int i = 0; i < M; i++){scanf("%d %d %d",&u,&v,&w);ADD_query(u,v,i,0);ADD_query(v,u,i,0);ADD_query(v,w,i,1);ADD_query(w,v,i,1);ADD_query(w,u,i,2);ADD_query(u,w,i,2);}Tarjan(0,-1);if(cas>1)puts("");for(int i = 0; i < M; i++)printf("%d\n",(res[i][0]+res[i][1]+res[i][2])/2);}return 0;}

原创粉丝点击