poj1984 带权并查集(向量处理)
| Time Limit: 2000MS | Memory Limit: 30000K | |
| Total Submissions: 5939 | Accepted: 2102 | |
| Case Time Limit: 1000MS | ||
Description
F1 --- (13) ---- F6 --- (9) ----- F3
| |
(3) |
| (7)
F4 --- (20) -------- F2 |
| |
(2) F5
|
F7
Being an ASCII diagram, it is not precisely to scale, of course.
Each farm can connect directly to at most four other farms via roads that lead exactly north, south, east, and/or west. Moreover, farms are only located at the endpoints of roads, and some farm can be found at every endpoint of every road. No two roads cross, and precisely one path
(sequence of roads) links every pair of farms.
FJ lost his paper copy of the farm map and he wants to reconstruct it from backup information on his computer. This data contains lines like the following, one for every road:
There is a road of length 10 running north from Farm #23 to Farm #17
There is a road of length 7 running east from Farm #1 to Farm #17
...
As FJ is retrieving this data, he is occasionally interrupted by questions such as the following that he receives from his navigationally-challenged neighbor, farmer Bob:
What is the Manhattan distance between farms #1 and #23?
FJ answers Bob, when he can (sometimes he doesn't yet have enough data yet). In the example above, the answer would be 17, since Bob wants to know the "Manhattan" distance between the pair of farms.
The Manhattan distance between two points (x1,y1) and (x2,y2) is just |x1-x2| + |y1-y2| (which is the distance a taxicab in a large city must travel over city streets in a perfect grid to connect two x,y points).
When Bob asks about a particular pair of farms, FJ might not yet have enough information to deduce the distance between them; in this case, FJ apologizes profusely and replies with "-1".
Input
* Line 1: Two space-separated integers: N and M * Lines 2..M+1: Each line contains four space-separated entities, F1,
F2, L, and D that describe a road. F1 and F2 are numbers of
two farms connected by a road, L is its length, and D is a
character that is either 'N', 'E', 'S', or 'W' giving the
direction of the road from F1 to F2. * Line M+2: A single integer, K (1 <= K <= 10,000), the number of FB's
queries * Lines M+3..M+K+2: Each line corresponds to a query from Farmer Bob
and contains three space-separated integers: F1, F2, and I. F1
and F2 are numbers of the two farms in the query and I is the
index (1 <= I <= M) in the data after which Bob asks the
query. Data index 1 is on line 2 of the input data, and so on.
Output
* Lines 1..K: One integer per line, the response to each of Bob's
queries. Each line should contain either a distance
measurement or -1, if it is impossible to determine the
appropriate distance.
Sample Input
7 6
1 6 13 E
6 3 9 E
3 5 7 S
4 1 3 N
2 4 20 W
4 7 2 S
3
1 6 1
1 4 3
2 6 6
Sample Output
13
-1
10
Hint
At time 3, the distance between 1 and 4 is still unknown.
#include<set>
#include<map>
#include<queue>
#include<stack>
#include<cmath>
#include<string>
#include<time.h>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define INF 1000000001
#define ll long long
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
const int MAXN = ;
struct node
{
int x;
int y;
int z;
int id;
char s[];
ll ans;
}q[],a[MAXN];
int pa[MAXN],n,m,k,siz[MAXN];
ll rel[MAXN][];
void Init()
{
for(int i = ; i <= n; i++){
pa[i] = i;
siz[i] = ;
}
memset(rel,,sizeof(rel));
}
bool cmp1(node fa,node fb)
{
return fa.z < fb.z;
}
bool cmp2(node fa,node fb)
{
return fa.id < fb.id;
}
void getp(int& x,int& y,node fp,int flag)
{
if(fp.s[] == 'E'){
x = fp.z;
y = ;
}
else if(fp.s[] == 'W'){
x = - fp.z;
y = ;
}
else if(fp.s[] == 'S'){
x = ;
y = fp.z;
}
else {
x = ;
y = - fp.z;
}
if(flag)x *= -, y *= -;
}
int find(int x)
{
if(x != pa[x]){
int fx = find(pa[x]);
siz[fx] += siz[x];
rel[x][] = rel[x][] + rel[pa[x]][];
rel[x][] = rel[x][] + rel[pa[x]][];
pa[x] = fx;
}
return pa[x];
}
int main()
{
while(~scanf("%d%d",&n,&m)){
Init();
for(int i = ; i < m; i++){
scanf("%d%d%d%s",&a[i].x,&a[i].y,&a[i].z,a[i].s);
}
scanf("%d",&k);
for(int i = ; i < k; i++){
q[i].id = i;
scanf("%d%d%d",&q[i].x,&q[i].y,&q[i].z);
}
sort(q,q+k,cmp1);
int p = ;
for(int i = ; i < m; i++){
int x = a[i].x;
int y = a[i].y;
int tx = ,ty = ;
int fx = find(x);
int fy = find(y);
if(fx != fy){
if(siz[fx] > ){
getp(tx,ty,a[i],);
siz[fx] += siz[fy];
pa[fy] = fx;
rel[fy][] = -rel[y][] - tx + rel[x][];
rel[fy][] = -rel[y][] - ty + rel[x][];
}
else {
getp(tx,ty,a[i],);
siz[fy] += siz[fx];
pa[fx] = fy;
rel[fx][] = rel[y][] - tx - rel[x][];
rel[fx][] = rel[y][] - ty - rel[x][];
}
}
while(p < k && q[p].z == i + ){
x = q[p].x;
y = q[p].y;
fx = find(x);
fy = find(y);
if(fx != fy){
q[p].ans = -;
}
else {
// cout<<rel[x][0]<<' '<<rel[y][0]<<' '<<rel[x][1]<<' '<<rel[y][1]<<endl;
q[p].ans = fabs(rel[x][] - rel[y][]) + fabs(rel[x][] - rel[y][]);
}
p ++;
}
}
sort(q,q+k,cmp2);
for(int i = ; i < k; i++){
printf("%lld\n",q[i].ans);
}
}
return ;
}
poj1984 带权并查集(向量处理)的更多相关文章
- poj1984 带权并查集
题意:有多个点,在平面上位于坐标点上,给出一些关系,表示某个点在某个点的正东/西/南/北方向多少距离,然后给出一系列询问,表示在第几个关系给出后询问某两点的曼哈顿距离,或者未知则输出-1. 只要在元素 ...
- POJ 1182 食物链 (带权并查集 && 向量偏移)
题意 : 中文题就不说题意了…… 分析 : 通过普通并查集的整理归类, 能够单纯地知道某些元素是否在同一个集合内.但是题目不仅只有种类之分, 还有种类之间的关系, 即同类以及吃与被吃, 而且重点是题目 ...
- POJ 2492 A Bug's Life (带权并查集 && 向量偏移)
题意 : 给你 n 只虫且性别只有公母, 接下来给出 m 个关系, 这 m 个关系中都是代表这两只虫能够交配, 就是默认异性, 问你在给出的关系中有没有与异性交配这一事实相反的, 即同性之间给出了交配 ...
- POJ 1182 食物链(经典带权并查集 向量思维模式 很重要)
传送门: http://poj.org/problem?id=1182 食物链 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: ...
- 带权并查集 - How Many Answers Are Wrong
思路: 带权并查集+向量偏移 #include <iostream> using namespace std; int n, m; ]; ]; // 到根节点的距离 ; void init ...
- POJ1984:Navigation Nightmare(带权并查集)
Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2 ...
- HDU 1829 A Bug's Life 【带权并查集/补集法/向量法】
Background Professor Hopper is researching the sexual behavior of a rare species of bugs. He assumes ...
- Zjnu Stadium(hdu3047带权并查集)
题意:一个300列的无限行的循环场地,a b d代表a,b顺时针相距d的距离,现在给你一些距离,判断是否有冲突,如果有冲突计算冲突的次数 思路:带权并查集 a,b的距离等于b到根节点的距离 - a到根 ...
- poj 1733 Parity game(带权并查集+离散化)
题目链接:http://poj.org/problem?id=1733 题目大意:有一个很长很长含有01的字符串,长度可达1000000000,首先告诉你字符串的长度n,再给一个m,表示给你m条信息, ...
随机推荐
- PHPstudy和ecshop的安装和使用
PHPstudy和ecshop: phpStudy是一个PHP调试环境的程序集成包. 该程序包集成最新的Apache+PHP+MySQL+phpMyAdmin+ZendOptimizer,一次性安装, ...
- [No00002E]关于大数据,你不知道的6个迷思
还是那个观点:计算机,编程语言,互联网,大数据等等都只是工具! 导语:看过美剧<纸牌屋>没?知道这部"白宫甄嬛传"为什么会火吗?靠的是大!数!据! 过去两年,在 Net ...
- Js实现简单的省市级联的效果
需要注意的是当需要动态添加项的时候一定要先var newoption=new Option("项","值");然后再 select.options.add(ne ...
- HTML 学习笔记 JavaScript(数据类型)
字符串 数字 布尔 数组 对象 Null Undefined JavaScript 拥有动态类型 JavaScript拥有动态类型 这意味着相同的变量可用作不同的类型: 实例: var x // x ...
- asp.net webapi支持跨域
1.Install-Package Microsoft.AspNet.WebApi.Cors 2. using System.Web.Http; namespace WebService { ...
- 51nod lyk与gcd
1678 lyk与gcd 基准时间限制:2 秒 空间限制:131072 KB 分值: 80 难度:5级算法题 这天,lyk又和gcd杠上了.它拥有一个n个数的数列,它想实现两种操作. 1:将 ai ...
- 【MySQL】使用trim函数删除两侧字符
第一个LEADING,可以删除左侧指定的字符以及字符串 SELECT trim(LEADING '/' FROM `path`) 第二个TRAILING,可以删除右侧的指定字符以及字符串 SELECT ...
- [转]Python 命令行参数和getopt模块详解
FROM : http://www.tuicool.com/articles/jaqQvq 有时候我们需要写一些脚本处理一些任务,这时候往往需要提供一些命令行参数,根据不同参数进行不同的处理,在Pyt ...
- C#开发中Windows域认证登录2(扩展吉日嘎拉GPM系统)
原文地址:http://www.cuiwenyuan.com/shanghai/post/Windows-AD-Logon-Intergrated-into-Jirigala-GPM-DotNet-B ...
- vtk renderer / rendering 绘制
1.在绘制窗口中绘制出物体(静态的)vtkRenderWindow * w=vtkRenderWindow::New(); w->AddRenderer(r); for(int ...