[USACO 2004DEC] Navigation Nightmare
[题目链接]
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 ; }
[USACO 2004DEC] Navigation Nightmare的更多相关文章
- 【POJ 1984】Navigation Nightmare(带权并查集)
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40 ...
- POJ 1984 Navigation Nightmare (数据结构-并检查集合)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 4072 Accepted: 1 ...
- POJ1984 Navigation Nightmare —— 种类并查集
题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K T ...
- POJ 1984 Navigation Nightmare 带全并查集
Navigation Nightmare Description Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- POJ 1984 Navigation Nightmare 【经典带权并查集】
任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K To ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
[bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...
- [POJ1984]Navigation Nightmare
[POJ1984]Navigation Nightmare 试题描述 Farmer John's pastoral neighborhood has N farms (2 <= N <= ...
随机推荐
- [转]Fedora22添加国内软件源和本地软件源
Fedora22添加国内软件源和本地软件源 Linux系统和Windows系统一个很大的区别就是软件安装方式,windows系统下安软件,我们去相应的网站下载软件安装包离线安装就可以了.虽然Linux ...
- BZOJ1777: [Usaco2010 Hol]rocks 石头木头
n<=10000的树,节点有初始石头数<=1000,进行这样的游戏:两人轮流行动,我先手,每次可以选一个节点(≠1)把不超过m<=1000个石头移到父亲,最后所有石头都在节点1,没法 ...
- msp430项目编程01
msp430中项目---点阵LED显示 1.点阵LED介绍 2.代码(直接使用引脚驱动) 3.代码(使用芯片驱动) 4.项目总结 msp430项目编程 msp430入门学习
- angularjs ngRoute的使用简单例子
很丑的小例子,刚学angularjs,写下来方面以后看. 1.例子的工程目录如下: 2.index.html代码如下: <!DOCTYPE html><html><hea ...
- WKWebView的了解
1. http://blog.csdn.net/chenyong05314/article/details/53735215 2. http://www.jianshu.com/p/6ba250744 ...
- Event Logging 技术简介
https://blog.csdn.net/xiliang_pan/article/details/41805023
- python之-- socket 基础篇
socket 网络模块 注意事项:在python3中,所有数据的传输必须用bytes类型(bytes只支持ascii码)所以在发送数据的时候要么在发送的字符串前面加 'b',要么使用encode('u ...
- eclipse提速01 - 禁用不常用的eclipse启动插件
会不断更新,需要的收藏 禁用不常用的eclipse启动插件(当然如果不需要可以卸载)
- SeaGlass:手工搭建伪基站监控系统
“伪基站”即假基站,设备一般由主机和笔记本电脑或手机组成,通过短信群发器.短信发信机等相关设备能够搜取以其为中心.一定半径范围内的手机卡信息,利用2G移动通信的缺陷,通过伪装成运营商的基站,冒用他人手 ...
- [Bash] Find Files and Folders with `find` in Bash
find is a powerful tool that can not only find files but it can run a command on each matching file ...