一定要坚持呀!
day1 打卡
这道题我先根据样例算了一下结果,知道了题意是n个点相连有m条路径,和向量差不多,带方向的相加减求长度。
题目还要求在索引的数字内求长度,测试数据中询问的索引数是非降的,所以才在代码中用了ff控制当前索引的位置有多少条路已连接。
补充知识点:带权并查集
带权并查集即不仅更新当前节点去寻找根,在找根的同时累加当前节点到父亲节点,父亲节点到爷爷节点,…到根节点的每一段距离(权重)。
int f[maxn];
int w[maxn];
int find(int x){
//累加x寻根的过程中的权值。
if(x==f[x]) return x;
int fx=f[x];
f[x]=find(f[x]);
w[x]+=w[fx];
return f[x];
}
void add(int x,int y,int val){
//x点与y点相连,x为y的父亲,val是边权,加到y的权值数组里。
int xx=find(x);
int yy=find(y);
if(xx!=yy) f[xx]=yy;
w[xx]=val+w[y]-w[x];//取一段
}
AC代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=40010;
int f[maxn];
struct node{
int pre;
int to;
int val;
char v;
}a[maxn];
int row[maxn];//行
int column[maxn];//列
int n,m;
int find(int x){
if(x==f[x]) return x;
int fx=f[x];
f[x]=find(f[x]);
row[x]+=row[fx];
column[x]+=column[fx];
return f[x];
}
void add(int x,int y,int tx,int ty){
int xx=find(x);
int yy=find(y);
f[xx]=yy;
row[xx]=tx+row[y]-row[x];
column[xx]=ty+column[y]-column[x];
}
int main(){
scanf("%d %d",&n,&m);
for(int i=0;i<=n;i++) f[i]=i;
for(int i=1;i<=m;i++){
scanf("%d %d %d %c",&a[i].pre,&a[i].to,&a[i].val,&a[i].v);
getchar();
}
int t,x,y,index;
int ff=1;
scanf("%d",&t);
while(t--){
scanf("%d %d %d",&x,&y,&index);
for(int i=ff;i<=index;i++){
int t_row=0,t_column=0;
if(a[i].v=='N'){
t_column=a[i].val;
}else if(a[i].v=='S'){
t_column=-1*a[i].val;
}else if(a[i].v=='W'){
t_row=-1*a[i].val;
}else if(a[i].v=='E'){
t_row=a[i].val;
}
add(a[i].pre,a[i].to,t_row,t_column);
}
ff=index+1;
int xx=find(x);
int yy=find(y);
if(xx!=yy) printf("-1\n");
else printf("%d\n",abs(row[x]-row[y])+abs(column[x]-column[y]));
}
return 0;
}
本文介绍了一种利用带权并查集解决n个点间有向路径相加减求长度的问题,并通过具体代码实例展示了如何进行路径压缩及权值更新。
928

被折叠的 条评论
为什么被折叠?



