http://hihocoder.com/problemset/problem/1515
题意:
思路:
带权并查集的简单题,计算的时候利用向量法则即可。
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = +; int n,m,q;
int p[maxn],r[maxn]; int finds(int x)
{
if(p[x]==x) return x;
int tmp = p[x];
p[x] = finds(p[x]);
r[x] = r[x] + r[tmp];
return p[x];
} void unions(int a, int b, int x, int y, int s)
{
p[x] = y;
r[x] = s+r[b]-r[a];
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d%d",&n,&m,&q);
for(int i=;i<=n;i++) {p[i]=i;r[i]=;}
while(m--)
{
int a,b,s;
scanf("%d%d%d",&a,&b,&s);
int x = finds(a);
int y = finds(b);
unions(a,b,x,y,s);
}
while(q--)
{
int a,b;
scanf("%d%d",&a,&b);
int x = finds(a);
int y = finds(b);
if(x!=y) puts("-1");
else printf("%d\n",r[a]-r[b]);
}
return ;
}