bzoj 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦(加权并查集)
3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 121 Solved: 63
[Submit][Status][Discuss]
Description
Input
Output
Sample Input
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
-1
10
HINT
在时刻1,约翰知道1到6的距离为13;在时刻3,1到4的距离仍然不知道;在时刻6,位置6向
北3个距离,向西7个距离于位置2,所以距离为10.
Source
Green
[Submit][Status][Discuss]
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100003
using namespace std;
int fa[N],disx[N],disy[N],ans[N];
int n,m,q;
struct data{int t,x,y,l,opt,mark;
}a[N],e[N];
int cmp(data a,data b)
{return a.t<b.t;
}
int find(int x)
{if (fa[x]==x) return x;int t=find(fa[x]);disx[x]+=disx[fa[x]];disy[x]+=disy[fa[x]];fa[x]=t;return fa[x];
}
int main()
{freopen("a.in","r",stdin);//freopen("my.out","w",stdout);scanf("%d%d",&n,&m);for (int i=1;i<=m;i++) {char s[10];scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].l,s);char c=getchar();while (c<'A'||c>'Z') c=getchar();switch(c){case 'E':a[i].mark=1;break;case 'W':a[i].mark=2;break;case 'S':a[i].mark=3;break;case 'N':a[i].mark=4;break;}a[i].t=i;a[i].opt=0;}scanf("%d",&q);for (int i=1;i<=q;i++) {scanf("%d%d%d",&e[i].x,&e[i].y,&e[i].t);e[i].l=i;}for (int i=1;i<=n;i++) fa[i]=i;sort(e+1,e+q+1,cmp); int now=1;for (int i=1;i<=q;i++) {//cout<<e[i].x<<" "<<e[i].y<<endl;while (now<=m&&now<=e[i].t) {int r1=find(a[now].x); int r2=find(a[now].y);// cout<<a[now].x<<" "<<a[now].y<<"!"<<endl;if (r1!=r2) { fa[r2]=r1; int ans=1;if (a[now].mark==2||a[now].mark==3) ans=-1;if (a[now].mark<=2) disx[r2]=disx[a[now].x]-disx[a[now].y]-ans*a[now].l,disy[r2]=disy[a[now].x]-disy[a[now].y];else disy[r2]=disy[a[now].x]-disy[a[now].y]-ans*a[now].l,disx[r2]=disx[a[now].x]-disx[a[now].y];}now++; }int r1=find(e[i].x); int r2=find(e[i].y);if (r1!=r2) ans[e[i].l]=-1;else {int x=disx[e[i].x]-disx[e[i].y];int y=disy[e[i].x]-disy[e[i].y];ans[e[i].l]=abs(x)+abs(y);}}for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
}
bzoj 3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦(加权并查集)相关推荐
- BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集
BZOJ_3362_[Usaco2004 Feb]Navigation Nightmare 导航噩梦_并查集 Description 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M( ...
- 带权并查集【bzoj3362】: [Usaco2004 Feb]Navigation Nightmare 导航噩梦
[bzoj]3362: [Usaco2004 Feb]Navigation Nightmare 导航噩梦 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤40000)条的不同的垂 ...
- bzoj3362[Usaco2004 Feb]Navigation Nightmare 导航噩梦
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=3362 题目大意: 农夫约翰有N(2≤N≤40000)个农场,标号1到N,M(2≤M≤400 ...
- POJ 1984 Navigation Nightmare 多权值并查集
一.内容 Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labe ...
- bzoj 3367: [Usaco2004 Feb]The Big Game 球赛(DP)
3367: [Usaco2004 Feb]The Big Game 球赛 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 56 Solved: 45 ...
- bzoj 3369: [Usaco2004 Feb]Pesky Parentheses 烦人括号(思维题)
3369: [Usaco2004 Feb]Pesky Parentheses 烦人括号 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 14 Sol ...
- bzoj 3372: [Usaco2004 Feb]Moo University -- Financial Aid 财政补助(set+贪心)
3372: [Usaco2004 Feb]Moo University -- Financial Aid 财政补助 Time Limit: 10 Sec Memory Limit: 128 MB S ...
- bzoj 3365: [Usaco2004 Feb]Distance Statistics 路程统计【容斥原理+点分治】
统计在一个root下的两个子树,每个子树都和前面的运算一下再加进去对于这种需要排序的运算很麻烦,所以考虑先不去同子树内点对的算出合法点对个数,然后减去每一棵子树内的合法点对(它们实际上是不合法的,相当 ...
- 【加权并查集】bzoj 4602 齿轮
立志用最少的代码做最高效的表达 Description 现有一个传动系统,包含了N个组合齿轮和M个链条.每一个链条连接了两个组合齿轮u和v,并提供了一个传动比x : y.即如果只考虑这两个组合齿轮,编 ...
最新文章
- 1024块TPU在燃烧!BERT训练从3天缩短到76分钟 | 技术头条
- 基于Springboot实现宠物医院综合管理系统
- linux select系统调用函数分析,Linux select系统调用
- python程序默认执行与多线程
- concat拼接的坑 mysql_DNSlog注入踩坑记录:
- ref与out一看就懂
- 为什么jdk的CLASSPATH环境变量需要设置rt.jar 和 tools.jar
- STM32 4*4矩阵按键
- 板子制作_使用Buildroot制作Ubootamp;Kernelamp;Rootfs
- SQL OUTER JOIN概述和示例
- 识别数学公式_新品独家发布(免费送码)丨极度公式 数学公式 OCR 识别编辑 LaTeX 公式软件...
- 《普林斯顿微积分读本》学习笔记
- 多线程常见面试题(含常见项目遇到多线程问题解决及面试对话)
- linux安装智能输入法,Ubuntu 8.04中安装智能拼音输入法
- 一起教育科技2020净收1.92亿元 官网却启用杂米域名17zyw.cn
- 习题6-5 巡逻机器人
- python正则表达式(关于提取数字)
- 计算机无法检索文件夹,win7系统不能搜索文件夹怎么回事
- IText导出PDF添加图片,解决中文问题
- 修复登录接口版最新知识付费小程序源码下载-独立后台版本