J - Design the city
Crawling in process...
Crawling failed
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu
System Crawler (2013-05-30)
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
4
0 1 1
0 2 1
0 3 1
2
1 2 3
0 1 2
5
0 1 1
0 2 1
1 3 1
1 4 1
2
0 1 2
1 0 3
Sample Output
3
2
2
2
思路:就是个模板的水LCA,依次求出两两点间的最短路加和除2就是答案,最短路用LCA求。
失误点:虽然只有n个元素,求bit时也要求到bit[n],我居然求到bit[n-1],看了半天也没看出问题,居然把如此简单LCA给写搓了,好伤心,sad,给个调试的搓代码
提醒下自己。
#include<iostream>
#include<cstring>
#include<cstdio>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define clr(f,z) memset(f,z,sizeof(f))
#define ll(x) (1<<x)
using namespace std;
const int mm=2e5+9;
int rmq[mm][25];
int N,head[mm],dfs_clock,to[mm],vis[mm],dis[mm];
int bit[mm],edge;
class Edge
{
public:int v,next,w;
}e[mm];
void data()
{
clr(head,-1);edge=0;
}
void add(int u,int v,int w)
{
e[edge].v=v;e[edge].w=w;e[edge].next=head[u];head[u]=edge++;
}
void dfs(int u,int dep)
{ int v;
dis[u]=dep;
to[dfs_clock]=u;
vis[u]=dfs_clock++;
for(int i=head[u];~i;i=e[i].next)
{
v=e[i].v;
if(vis[v]==-1)
{
dfs(v,dep+e[i].w);
to[dfs_clock++]=u;
}
}
}
void initRMQ()
{ bit[0]=-1;
int n=dfs_clock;
//FOR(i,1,n-1)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];不包括n错了。
FOR(i,1,n)bit[i]=(i&(i-1))==0?bit[i-1]+1:bit[i-1];
FOR(i,0,n-1)rmq[i][0]=dis[ to[i] ];
/*FOR(i,0,n-1)
{
printf("qw=%d %d %d %d\n",i,to[i],dis[to[i]],rmq[i][0]);
}*/
//puts("+++++++++++++++");
//printf("bit=%d %d %d %d\n",n,bit[2],bit[n],ll(1));
FOR(i,1,bit[n])
for(int j=0;j+ll(i)-1<n;++j)
{rmq[j][i]=min(rmq[j][i-1],rmq[j+ll(i-1)][i-1]);
//printf("rmq %d %d %d\n",j,i,rmq[j][i]);
}
}
void find_bcc()
{
clr(vis,-1);
dfs_clock=0;
dfs(0,0);
initRMQ();
/* FOR(i,0,N-1)
printf("dis=%d %d\n",i,dis[i]);
FOR(i,0,N-1)
printf("h=%d %d\n",i,vis[i]);
FOR(i,0,dfs_clock-1)
printf("to=%d %d\n",i,to[i]);
*/
}
int RMQ(int l,int r)
{ //printf("t=%d %d %d %d ",to[l],to[r],l,r);
if(l>r)l^=r,r^=l,l^=r;
int t=bit[r-l+1];
r-=ll(t)-1;
int z=min(rmq[l][t],rmq[r][t]);
// printf("%d\n",z);
return z;
}
int main()
{ int a,b,c,cas=0;
while(~scanf("%d",&N))
{ if(cas)printf("\n");++cas;
data();
FOR(i,2,N)
scanf("%d%d%d",&a,&b,&c),add(a,b,c),add(b,a,c);
find_bcc();
int Q;
scanf("%d",&Q);
while(Q--)
{
scanf("%d%d%d",&a,&b,&c);
int ans=0;
/*ans+=dis[a]+dis[b]-2*RMQ(a,b);
ans+=dis[a]+dis[c]-2*RMQ(a,c);
ans+=sia[b]+dis[c]-2*RMQ(b,c);
*/ans=dis[a]+dis[b]+dis[c]-RMQ(vis[a],vis[b])-RMQ(vis[b],vis[c])-RMQ(vis[a],vis[c]);
printf("%d\n",ans);
}
}
}