比赛 |
ZLXOI2015Day2 |
评测结果 |
AAAAAAAAAA |
题目名称 |
ZLX的陨落 |
最终得分 |
100 |
用户昵称 |
ifAandBisC |
运行时间 |
0.889 s |
代码语言 |
C++ |
内存使用 |
60.04 MiB |
提交时间 |
2015-10-30 20:28:57 |
显示代码纯文本
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<cmath>
#define iii 1010101
using namespace std;
int n;
int dis[iii],f[iii],ans[iii];
bool vis[iii],flag[iii];
vector<int> head[iii];
vector<int> val [iii];
vector<int> Qus[iii];
vector<int> num[iii];
int find (int x){if (x!=f[x])return f[x]=find(f[x]);return x;}
void add (int a,int b,int c)
{
head[a].push_back(b);
val [a].push_back(c);
}
void Lca (int T)
{
vis[T]=true;
int len=head[T].size();
for (int i=0;i<len;i++)
if (!vis[head[T][i]])
{
dis[head[T][i]]=dis[T]+val[T][i];
Lca (head[T][i]);
flag[head[T][i]]=true;
f[head[T][i]]=T;
}
len=Qus[T].size();
for (int i=0;i<len;i++)
if (flag[Qus[T][i]])
{
int C=find(Qus[T][i]);
ans[num[T][i]]=dis[Qus[T][i]]+dis[T]-2*dis[C];
}
}
int main ()
{
freopen ("ThefallingofZLX.in","r",stdin);
freopen ("ThefallingofZLX.out","w",stdout);
memset (vis, 0,sizeof(vis));
memset (flag,0,sizeof(flag));
int m,q,a,b,c,t=0x7fffffff;
scanf ("%d%d",&n,&m);
for (int i=1;i<=m;i++)
{
scanf ("%d%d%d",&a,&b,&c);
add (a,b,c);
add (b,a,c);
t=min(t,min(a,b));
}
scanf ("%d",&q);
for (int i=1;i<=q;i++)
{
scanf ("%d%d",&a,&b);
Qus[a].push_back(b);
Qus[b].push_back(a);
num[a].push_back(i);
num[b].push_back(i);
}
for (int i=1;i<=n;i++) f[i]=i;
dis[t]=0;
Lca (t);
for (int i=1;i<=q;i++)
printf ("%d\n",ans[i]);
return 0;
}