[POJ P1984]Navigation Nightmare
来源:互联网 发布:大通模型淘宝店 编辑:程序博客网 时间:2024/06/05 05:46
原题链接
因为POJ之前炸了
所以拖到现在才
带权并查集
记录到根的曼哈顿距离
然后间接算出别的点的
#include<iostream>#include<cstring>#include<algorithm>#include<cstdio>#include<queue>#include<vector>#include<climits>#include<string>#include<cstdlib>#include<ctime>#define MOD 1000000007#define LL long longusing namespace std;struct nico{ int f1,f2,l; char d;}load[40005];int n,m,i,j,fat[40005],k,r1,r2,dx[40005],dy[40005],t,p1,p2,q;int abs(int x){ if(x<0) return -x; return x;}int find(int x){ int tmp; if(fat[x]==x) return x; tmp=find(fat[x]); dx[x]+=dx[fat[x]]; dy[x]+=dy[fat[x]]; fat[x]=tmp; return tmp;}int main(){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++) fat[i]=i; for(i=1;i<=m;i++) { scanf("%d%d%d",&load[i].f1,&load[i].f2,&load[i].l); cin>>load[i].d; } scanf("%d",&k); j=1; for(i=1;i<=k;i++) { scanf("%d%d%d",&p1,&p2,&q); while(j<=q) { r1=find(load[j].f1); r2=find(load[j].f2); if(r1!=r2) { fat[r1]=r2; dy[r1]=dy[load[j].f2]-dy[load[j].f1]; dx[r1]=dx[load[j].f2]-dx[load[j].f1]; if(load[j].d=='W') dx[r1]-=load[j].l; if(load[j].d=='E') dx[r1]+=load[j].l; if(load[j].d=='S') dy[r1]+=load[j].l; if(load[j].d=='N') dy[r1]-=load[j].l; } j++; } r1=find(p1); r2=find(p2); if(r1!=r2) printf("-1\n"); else { t=abs(dx[p1]-dx[p2])+abs(dy[p1]-dy[p2]); printf("%d\n",t); } } return 0;}
阅读全文
0 0
- [POJ P1984]Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ-1984-Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- POJ-1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare
- Navigation Nightmare POJ
- POJ 1984 Navigation Nightmare
- Navigation Nightmare poj 1984
- poj 1984 Navigation Nightmare
- POJ 1984 Navigation Nightmare 笔记
- 【POJ 1984】Navigation Nightmare (并查集)
- POJ--1984-Navigation Nightmare--并查集
- poj 1984 - Navigation Nightmare(并查集)
- POJ 1984 Navigation Nightmare 并查集
- Windows操作系统下QT应用程序打包和发布。
- shu418丢史蒂芬妮 博弈n/p图
- WebRTC基本概念
- unix网络编程学习手册(1)
- 最近又遇到一个RST, 一眼就看出来了!
- [POJ P1984]Navigation Nightmare
- Leetcode 91. Decode Ways
- Scales HDU
- 你应该知道的 RPC 原理
- HPU1026: 素数判定
- 前端常用网站
- WebRTC手记之初探
- ConstraintLayout基础 及动态控件(动画效果)
- 欢迎使用CSDN-markdown编辑器