How far away ?
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 21046 Accepted Submission(s): 8280
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.
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
手残起了三个个全局的变量名,结果在递归的时候被调用了,接下来就是起了两个m1和m2的结构体数组,不小心打错了m1又是无限wa,以后取变量还是要谨慎。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#define ll long long
using namespace std;
void mydis(int a[], int n){
printf("总数为%d个\n",n);
for(int i = 1; i <= n; i++) cout<<a[i]<<", ";
cout<<endl<<"------------------"<<endl;
}
const int mx = 8e4+10; //要两倍才够
struct no{
int u,to,va,next;
}m1[mx];
struct no2{
int u,to,next,dd;
}m2[mx];
int he[mx],qu[mx],fa[mx],dis[mx]; //瞎取了一个重复的变量名 ,递归的时候被用到了
int n,m,tol;
bool vis[mx];
void init(){
tol = 0;
dis[1] = 0;
memset(he,-1,sizeof(he));
memset(qu,-1,sizeof(qu));
memset(vis,0,sizeof(vis));
}
void add(int u, int to, int w){
m1[tol].to = to;
m1[tol].va = w;
m1[tol].next = he[u];
he[u] = tol++;
}
void add2(int u, int to){
m2[tol].u = u;
m2[tol].to = to;
m2[tol].next = qu[u];
qu[u] = tol++;
}
int find(int x){
if(x == fa[x])
return x;
else
return fa[x] = find(fa[x]);
}
void tarjan(int u){
vis[u] = 1;
fa[u] = u;
for(int i = he[u]; i != -1; i = m1[i].next){
int to = m1[i].to, w = m1[i].va;
if(!vis[to]){
dis[to] = dis[u] + w;
tarjan(to);
fa[to] = u;
}
}
for(int i = qu[u]; i != -1; i = m2[i].next){
int to = m2[i].to, dd = m2[i].dd; //这里m2写成了m1,wa到怀疑人生,下次变量名还是不要瞎取了
if(vis[to]){
m2[i].dd = m2[i^1].dd = find(to);
}
}
}
int main(){
int T,a,b,c;
scanf("%d",&T);
while(T--){
init();
/* memset(he,-1,sizeof(he));
memset(qu,-1,sizeof(qu));
tol = 0;*/
scanf("%d%d",&n,&m);
for(int i = 1; i < n; i++){
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
tol = 0;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
add2(a,b);
add2(b,a);
}
tarjan(1);
// mydis(dis,n);
for(int i = 0; i < m; i++){
int u = m2[2*i].u, v = m2[2*i].to, w = m2[2*i].dd;
printf("%d\n",dis[u]+dis[v]-2*dis[w]);
}
}
return 0;
}