http://codeforces.com/gym/101808/problem/K
Shortest path problems have always been a part of competitive programming, appearing in many contests all around the world. In order to keep that tradition, we created another one:
You are given an undirected connected weighted graph with N nodes and N edges, and you have to answer Q queries. Each query consists of two nodes X and Y, and you have to find the length of the shortest path between nodes X and Y.
Input
The first line contains a single integer T, the number of test cases.
Each test case starts with a line containing two integers N and Q, the number of nodes (and edges) and the number of queries. (3 ≤ N ≤ 105) (1 ≤ Q ≤ 105)
Each of the following N lines contain the description of the edges. The ith line contains 3 space-separated integers ui, vi, and wi. This means that there is an undirected edge between nodes ui and vi, with a weight of wi. (1 ≤ ui, vi ≤ N) (1 ≤ wi ≤ 105)
Then Q lines follow, the ith line contains two integers X and Y. This means that you need to find the shortest path between nodes X and Y. (1 ≤ X, Y ≤ N)
It is guaranteed that the graph contains no self loops or multiple edges.
Output
For each test case, and for each query, print one line containing one integer, the shortest path between X and Y.
Example
Input
1
6 3
1 2 2
1 3 4
2 6 3
3 4 1
3 5 10
3 6 6
1 4
2 5
3 2
Output
5
16
6
题目大意:给一棵n个节点的树和一条额外的边,求树上任意两点间的距离。
思路:用图跑最短路肯定是不行的,时间复杂度太高了。我们考虑不要最后一条边,那不就是求树上任意两点间的距离吗,因此我们有: a n s = d i s [ u ] + d i s [ v ] − 2 ∗ d i s [ L C A ( u , v ) ] ans=dis[u]+dis[v]-2*dis[LCA(u,v)] ans=dis[u]+dis[v]−2∗dis[LCA(u,v)]现在考虑把那条边加上会怎么样,其实无非就是多了两种情况,为了方便叙述,我们设函数: g e t d i s ( u , v ) = d i s [ u ] + d i s [ v ] − 2 ∗ d i s [ L C A ( u , v ) ] getdis(u,v)=dis[u]+dis[v]-2*dis[LCA(u,v)] getdis(u,v)=dis[u]+dis[v]−2∗dis[LCA(u,v)]那么当把第n条边(设这条边的两个点为X、Y 权值为Z)加上时,我们还有另外两种选择: a n s = g e t d i s ( u , X ) + g e t d i s ( v , Y ) + Z ans=getdis(u,X)+getdis(v,Y)+Z ans=getdis(u,X)+getdis(v,Y)+Z a n s = g e t d i s ( u , Y ) + g e t d i s ( v , X ) + Z ans=getdis(u,Y)+getdis(v,X)+Z ans=getdis(u,Y)+getdis(v,X)+Z以上三种情况取最小值即可。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
struct edge
{
int to,nxt;
ll dis;
};
edge Edge[maxn<<1];
int head[maxn];
int fa[maxn];//父节点
int deep[maxn];//深度
ll dist[maxn];
int n,m,cnt=0;
int f[maxn][30];
inline void addedge(int u,int v,ll dis)
{
Edge[++cnt].to=v,Edge[cnt].nxt=head[u],Edge[cnt].dis=dis,head[u]=cnt;
Edge[++cnt].to=u,Edge[cnt].nxt=head[v],Edge[cnt].dis=dis,head[v]=cnt;
}
void dfs(int u,int father)
{
deep[u]=deep[father]+1;
f[u][0]=father;
for(int i=1;i<=20;i++)
f[u][i]=f[f[u][i-1]][i-1];
for(int i=head[u];i;i=Edge[i].nxt)
{
if(Edge[i].to!=father)
{
dist[Edge[i].to]=dist[u]+Edge[i].dis;
dfs(Edge[i].to,u);
}
}
}
inline int skip(int x,int level)
{
for(int i=20;i>=0;i--)
if((1<<i)&level)
x=f[x][i];
return x;
}
inline int LCA(int u,int v)
{
if(deep[u]<deep[v])
swap(u,v);
u=skip(u,deep[u]-deep[v]);
if(u==v)
return u;
for(int i=20;i>=0;i--)
if(f[u][i]!=f[v][i])
u=f[u][i],v=f[v][i];
return f[u][0];
}
inline ll getdis(int u,int v)
{
int grand=LCA(u,v);
return dist[u]+dist[v]-2*dist[grand];
}
int main()
{
int t,u,v,X,Y;
ll dis,ans,Z;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
cnt=0;
memset(head,0,sizeof(head));
for(int i=1;i<n;i++)
{
scanf("%d%d%lld",&u,&v,&dis);
addedge(u,v,dis);
}
scanf("%d%d%lld",&X,&Y,&Z);//第n条边
dfs(1,0);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
ans=getdis(u,v);
ans=min(ans,getdis(u,X)+getdis(v,Y)+Z);
ans=min(ans,getdis(u,Y)+getdis(v,X)+Z);
printf("%lld\n",ans);
}
}
return 0;
}