[USACO 2004DEC] Navigation Nightmare
2024-09-01 02:14:33
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=3362
[算法]
带权并查集
时间复杂度 : O(NlogN)
[代码]
#include<bits/stdc++.h>
using namespace std;
const int MAXN = ; struct Que
{
int f1 , f2 , t;
int id;
} que[MAXN]; int n , m;
int f[MAXN] , dist[MAXN] , x[MAXN] , y[MAXN] , X[MAXN] , Y[MAXN] , d[MAXN];
int ans[MAXN];
char dir[MAXN][]; template <typename T> inline void chkmax(T &x,T y) { x = max(x,y); }
template <typename T> inline void chkmin(T &x,T y) { x = min(x,y); }
template <typename T> inline void read(T &x)
{
T f = ; x = ;
char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << ) + (x << ) + c - '';
x *= f;
}
inline bool cmp(Que a,Que b)
{
return a.t < b.t;
}
inline int get_root(int x)
{
if (f[x] == x) return x;
int fa = get_root(f[x]);
X[x] += X[f[x]];
Y[x] += Y[f[x]];
return f[x] = fa;
} int main()
{ scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++) f[i] = i;
for (int i = ; i <= m; i++) scanf("%d%d%d%s",&x[i],&y[i],&d[i],&dir[i]);
int q;
read(q);
for (int i = ; i <= q; i++)
{
scanf("%d%d%d",&que[i].f1 , &que[i].f2 , &que[i].t);
que[i].id = i;
}
sort(que + ,que + q + ,cmp);
int cur = ;
for (int i = ; i <= q; i++)
{
while (cur <= que[i].t)
{
int sx = get_root(x[cur]) , sy = get_root(y[cur]);
if (sx == sy) continue;
f[sx] = sy;
if (dir[cur][] == 'E')
{
X[sx] = -X[x[cur]] + X[y[cur]] - d[cur];
Y[sx] = -Y[x[cur]] + Y[y[cur]];
}
if ( dir[cur][] == 'W' )
{
X[sx] = -X[x[cur]] + X[y[cur]] + d[cur];
Y[sx] = -Y[x[cur]] + Y[y[cur]];
}
if (dir[cur][] == 'N')
{
Y[sx] = -Y[x[cur]] + Y[y[cur]] - d[cur];
X[sx] = -X[x[cur]] + X[y[cur]] ;
}
if (dir[cur][] == 'S')
{
Y[sx] = -Y[x[cur]] + Y[y[cur]] + d[cur];
X[sx] = -X[x[cur]] + X[y[cur]];
}
++cur;
}
if (get_root(que[i].f1) != get_root(que[i].f2)) ans[que[i].id] = -;
else ans[que[i].id] = abs(X[que[i].f1] - X[que[i].f2]) + abs(Y[que[i].f1] - Y[que[i].f2]);
}
for (int i = ; i <= q; i++) printf("%d\n",ans[i]); return ; }
最新文章
- bzoj3208--记忆化搜索
- jsp打印页面 js代码
- How to control printer orientation(Landscape / Portrait) for an AX report in X++
- about JNI
- angular $apply()以及$digest()讲解1
- 【leetcode】260. Single Number III
- UnicodeEncodeError: &#39;ascii&#39; codec can&#39;t encode characters in position 0-25: ordinal not in range(128)
- NAND Flash底层原理,SLC MLC TLC比较【转】
- 【python接口自动化-requests库】【一】requests库安装
- iOS app签名原理
- 配置vCenter Server Appliance 6.7
- [Android] 基于 Linux 命令行构建 Android 应用(一):关于 Android 项目
- 4. mysql 1449 : The user specified as a definer (&#39;test&#39;@&#39;%&#39;) does not exist 解决方法
- PTA (Advanced Level) 1009 Product of Polynomials
- HDU 3642 - Get The Treasury - [加强版扫描线+线段树]
- POJ2228 Naptime
- linux引导系统
- RAC安装重新运行root.sh
- boost 中文编码转换
- 【数据库_Mysql】JAVA-数据库Date格式在前台JSP页面的获取