標籤:acm algorithm zoj lca tarjan
Design the city
| Time Limit: 1000MS |
|
Memory Limit: 32768KB |
|
64bit IO Format: %lld & %llu |
SubmitStatus
Description
Cerror 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.
Input
The 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.
Output
Q 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 Input
40 1 10 2 10 3 121 2 30 1 250 1 10 2 11 3 11 4 120 1 21 0 3
Sample Output
3222
題意:在一棵樹上,查詢u -> v -> w的最短距離。
分析:LCA+tarjan離線演算法,模板題。以前經常做的是兩點的最短距離,現在換成三點其實是一樣的,求出兩兩之間的最短距離之和,然後除2即可。易得。
題目連結:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=15731
代碼清單:
//#pragma comment(linker, "/STACK:102400000,102400000")#include<set>#include<map>#include<cmath>#include<queue>#include<stack>#include<ctime>#include<string>#include<cstdio>#include<cstring>#include<cctype>#include<cstdlib>#include<iostream>#include<algorithm>using namespace std;typedef long long ll;typedef unsigned int uint;typedef unsigned long long ull;const int maxn = 50000 + 5;const int maxv = 50000 + 5;const int maxq = 70000 + 5;struct Edge{ int u,v,w; Edge(){} Edge(int u,int v,int w){ this -> u = u; this -> v = v; this -> w = w; }}edge[maxq];struct Q{ int v,id,next; }quary[6*maxq];struct e{ int v,dis,next; }graph[2*maxn];int n,m,q;int a,b,c;int father[maxn];bool vis[maxn];int ans[3*maxq];int color[maxn];int depth[maxn];int nume,numq;int heade[maxn];int headq[maxn];int cases=0;void init(){ for(int i=0;i<maxn;i++) father[i]=i; memset(ans,-1,sizeof(ans)); memset(color,0,sizeof(color)); memset(depth,0,sizeof(depth)); memset(vis,false,sizeof(vis)); memset(graph,0,sizeof(graph)); memset(quary,0,sizeof(quary)); memset(heade,-1,sizeof(heade)); memset(headq,-1,sizeof(headq)); nume=numq=0;}void add_E(int u,int v,int dis){ graph[nume].v=v; graph[nume].dis=dis; graph[nume].next=heade[u]; heade[u]=nume++;}void add_Q(int u,int v,int id){ quary[numq].v=v; quary[numq].id=id; quary[numq].next=headq[u]; headq[u]=numq++;}void input(){ for(int i=1;i<n;i++){ scanf("%d%d%d",&a,&b,&c); add_E(a,b,c); add_E(b,a,c); } scanf("%d",&q); for(int i=1;i<=q;i++){ scanf("%d%d%d",&a,&b,&c); edge[i]=Edge(a,b,c); add_Q(a,b,(i-1)*3+1); add_Q(b,a,(i-1)*3+1); add_Q(a,c,(i-1)*3+2); add_Q(c,a,(i-1)*3+2); add_Q(b,c,(i-1)*3+3); add_Q(c,b,(i-1)*3+3); }}int Find(int x){ return x!=father[x] ? father[x]=Find(father[x]) : father[x]; }void tarjan(int u){ color[u]=1; vis[u]=true; for(int i=headq[u];i!=-1;i=quary[i].next){ int ID=quary[i].id; if(ans[ID]!=-1) continue; int v=quary[i].v; if(color[v]==0) continue; if(color[v]==1) ans[ID]=depth[u]-depth[v]; if(color[v]==2) ans[ID]=depth[u]+depth[v]-2*depth[Find(v)]; } for(int i=heade[u];i!=-1;i=graph[i].next){ int vv=graph[i].v; int dis=graph[i].dis; if(!vis[vv]){ depth[vv]=depth[u]+dis; tarjan(vv); color[vv]=2; father[vv]=u; } }}void solve(){ tarjan(0); if(cases) printf("\n"); for(int i=1;i<=q;i++){ printf("%d\n",(ans[(i-1)*3+1]+ans[(i-1)*3+2]+ans[(i-1)*3+3])/2); } cases++;}int main(){ while(scanf("%d",&n)!=EOF){ init(); input(); solve(); } return 0;}
著作權聲明:本文為博主原創文章,未經博主允許不得轉載。
ZOJ_3195_Design the city(LCA+tarjan)