POJ 1984 Navigation Nightmare 【经典带权并查集】

利用 Docker Distribution 搭建私有的 Docker Registry 1、拓扑 阅读详情

任意门:http://poj.org/problem?id=1984

Navigation Nightmare

Time Limit: 2000MS Memory Limit: 30000K
Total Submissions: 7783 Accepted: 2801
Case Time Limit: 1000MS

Description

Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 <= length <= 1000) connect the farms. A map of these farms might look something like the illustration below in which farms are labeled F1..F7 for clarity and lengths between connected farms are shown as (n): 
           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 1, FJ knows the distance between 1 and 6 is 13. 
At time 3, the distance between 1 and 4 is still unknown. 
At the end, location 6 is 3 units west and 7 north of 2, so the distance is 10. 

 

​题意概括:

给出 M 个建树的操作,K 次查询,每次查询 x 到 y 经过前 num 次建树操作的距离,如果未联通则输出-1;

解题思路:

带权并查集,路径压缩采用向量法。

dx【i】表示 i 距离所在树根结点的横坐标

dy【i】表示 i 距离所在树根结点的纵坐标

合并 u v 过程:

先合并两棵子树

ru = getfa(u)

rv = getfa(v)

改变其中一棵子树的根结点

fa[ rv ] = ru;

更新根结点的相对值(之后子树的相对值会通过查找父结点的过程进行更新)

dx[ rv ] = dx[ u ] - dx[ v ] - wx[ u, v];

dy[ rv ] = dy[ u ] - dy[ v ] - wy[ u, v];

 

先执行num次建树操作

查找父结点的同时压缩路径

 

查询最后结果

如果相同根,直接计算两点的曼哈顿距离

否则不连通

Tip:

题目没有说查询的 num 是非递减有序的,所以处理查询前要对 num 进行排序!!!

也就是说离线处理,在线处理会出错。(虽然poj上的数据我没有排序也AC了, 但这是需要考虑的情况)

 

AC code:

 1 //离线带权并查集
 2 #include <cstdio>
 3 #include <iostream>
 4 #include <algorithm>
 5 #include <cstring>
 6 #define INF 0x3f3f3f3f
 7 using namespace std;
 8 const int MAXN = 4e4+5;
 9 const int MAXK = 1e4+10;
10 struct Query{int x, y, index, no;}q[MAXN];  //查询信息
11 int fa[MAXN], dx[MAXN], dy[MAXN];   //并查集,路径压缩(距离起点的横坐标距离 和 纵坐标距离)
12 int u[MAXN], v[MAXN];               //第i个操作的起点 和 终点
13 int ans[MAXK];    //第 k 次查询的结果
14 int wx[MAXN], wy[MAXN];             //wx[ i ] 第i个操作的横坐标边权 wy[ i ] 第i个操作的纵坐标边权
15 int N, M, K;
16 
17 void init()
18 {
19     for(int i = 0; i <= N; i++){
20         fa[i] = i;
21         dx[i] = 0;
22         dy[i] = 0;
23     }
24     memset(q, 0, sizeof(q));
25 }
26 int aabs(int x){return x>0?x:-x;}
27 int getfa(int s)
28 {
29     if(s == fa[s]) return s;
30     int t = fa[s];
31     fa[s] = getfa(fa[s]);    //压缩路径
32     dx[s] += dx[t];
33     dy[s] += dy[t];
34     return fa[s];
35 }
36 bool cmp(Query q1,Query q2){return q1.index < q2.index;}
37 int main()
38 {
39     while(~scanf("%d%d", &N, &M)){
40         //scanf("%d%d", &N, &M);
41         init();
42         char nod;
43         for(int i = 1, d; i <= M; i++){
44             scanf("%d%d%d %c", &u[i], &v[i], &d, &nod);
45             if(nod == 'E') {wx[i] = d; wy[i] = 0;}
46             else if(nod == 'W') {wx[i] = -d; wy[i] = 0;}
47             else if(nod == 'N') {wy[i] = d; wx[i] = 0;}
48             else if(nod == 'S') {wy[i] = -d; wx[i] = 0;}
49         }
50         scanf("%d", &K);
51         for(int i = 1; i <= K; i++){
52             scanf("%d%d%d", &q[i].x, &q[i].y, &q[i].index);
53             q[i].no = i;
54         }
55         sort(q+1, q+K+1, cmp);
56         int k = 1;
57         for(int i = 1; i <= K; i++){
58             //printf("i:%d\n", i);
59             while(k <= q[i].index){                 //合并index个操作
60                 //printf("k:%d\n", k);
61                 int ru = getfa(u[k]);
62                 int rv = getfa(v[k]);
63                 //printf("u:%d ru:%d v:%d rv:%d\n", u[k], ru, v[k], rv);
64                 fa[rv] = ru;                            //合并两个集合
65                 dx[rv] = dx[u[k]] - dx[v[k]] - wx[k];   //
66                 dy[rv] = dy[u[k]] - dy[v[k]] - wy[k];
67                 k++;
68                 }
69             //printf("k:%d\n", k);
70             if(getfa(q[i].x) != getfa(q[i].y)) ans[q[i].no] = -1;  //两点经过index次操作后还是没有相连
71             else{
72                 ans[q[i].no] = aabs(dx[q[i].x] - dx[q[i].y]) + aabs(dy[q[i].x] - dy[q[i].y]);
73             }
74             //puts("");
75         }
76         for(int it = 1; it <= K; it++) printf("%d\n", ans[it]);
77     }
78     return 0;
79 }
View Code

 

一道拖了好久好久的带权并查集,给几个数据纪念一下

  1 Input:
  2 8 7
  3 1 2 1 S
  4 3 4 5 S
  5 5 6 8 S
  6 7 8 2 S
  7 3 2 3 N
  8 7 6 13 N
  9 5 4 7 N
 10 6
 11 1 3 1
 12 1 4 5
 13 3 4 2
 14 4 5 7
 15 1 8 6
 16 1 8 7
 17 
 18 Output:
 19 -1
 20 9
 21 5
 22 7
 23 -1
 24 39
 25 
 26 Input:
 27 100 99
 28 1 2 1000 E
 29 2 3 1000 E
 30 3 4 1000 E
 31 4 5 1000 E
 32 5 6 1000 E
 33 6 7 1000 E
 34 7 8 1000 E
 35 8 9 1000 E
 36 9 10 1000 E
 37 10 11 1000 E
 38 11 12 1000 E
 39 12 13 1000 E
 40 13 14 1000 E
 41 14 15 1000 E
 42 15 16 1000 E
 43 16 17 1000 E
 44 17 18 1000 E
 45 18 19 1000 E
 46 19 20 1000 E
 47 20 21 1000 E
 48 21 22 1000 E
 49 22 23 1000 E
 50 23 24 1000 E
 51 24 25 1000 E
 52 26 27 1000 E
 53 27 28 1000 E
 54 28 29 1000 E
 55 29 30 1000 E
 56 30 31 1000 E
 57 31 32 1000 E
 58 32 33 1000 E
 59 33 34 1000 E
 60 34 35 1000 E
 61 35 36 1000 E
 62 36 37 1000 E
 63 37 38 1000 E
 64 38 39 1000 E
 65 39 40 1000 E
 66 40 41 1000 E
 67 41 42 1000 E
 68 42 43 1000 E
 69 43 44 1000 E
 70 44 45 1000 E
 71 45 46 1000 E
 72 46 47 1000 E
 73 47 48 1000 E
 74 48 49 1000 E
 75 49 50 1000 E
 76 51 52 1000 E
 77 52 53 1000 E
 78 53 54 1000 E
 79 54 55 1000 E
 80 55 56 1000 E
 81 56 57 1000 E
 82 57 58 1000 E
 83 58 59 1000 E
 84 59 60 1000 E
 85 60 61 1000 E
 86 61 62 1000 E
 87 62 63 1000 E
 88 63 64 1000 E
 89 64 65 1000 E
 90 65 66 1000 E
 91 66 67 1000 E
 92 67 68 1000 E
 93 68 69 1000 E
 94 69 70 1000 E
 95 70 71 1000 E
 96 71 72 1000 E
 97 72 73 1000 E
 98 73 74 1000 E
 99 74 75 1000 E
100 76 77 1000 E
101 77 78 1000 E
102 78 79 1000 E
103 79 80 1000 E
104 80 81 1000 E
105 81 82 1000 E
106 82 83 1000 E
107 83 84 1000 E
108 84 85 1000 E
109 85 86 1000 E
110 86 87 1000 E
111 87 88 1000 E
112 88 89 1000 E
113 89 90 1000 E
114 90 91 1000 E
115 91 92 1000 E
116 92 93 1000 E
117 93 94 1000 E
118 94 95 1000 E
119 95 96 1000 E
120 96 97 1000 E
121 97 98 1000 E
122 98 99 1000 E
123 99 100 1000 E
124 10 40 100 S
125 30 60 100 S
126 70 90 100 S
127 100
128 34 44 96
129 55 65 96
130 38 48 96
131 63 73 96
132 2 12 96
133 76 86 96
134 1 11 96
135 62 72 96
136 8 18 96
137 41 51 96
138 58 68 96
139 60 70 96
140 30 40 96
141 67 77 96
142 60 70 96
143 74 84 96
144 80 90 96
145 11 21 96
146 21 31 96
147 87 97 96
148 33 43 96
149 20 30 96
150 83 93 96
151 35 45 96
152 56 66 96
153 32 42 96
154 44 54 96
155 36 46 96
156 69 79 96
157 74 84 96
158 51 61 96
159 64 74 96
160 38 48 96
161 89 99 96
162 37 47 96
163 40 50 96
164 36 46 96
165 89 99 96
166 11 21 96
167 5 15 96
168 39 49 96
169 30 40 96
170 64 74 96
171 30 40 96
172 58 68 96
173 33 43 96
174 14 24 96
175 10 20 96
176 43 53 96
177 86 96 96
178 6 16 96
179 37 47 96
180 67 77 96
181 51 61 96
182 33 43 96
183 32 42 96
184 82 92 96
185 77 87 96
186 30 40 96
187 60 70 96
188 22 32 96
189 80 90 96
190 34 44 96
191 60 70 96
192 40 50 96
193 32 42 96
194 61 71 96
195 75 85 96
196 82 92 96
197 33 43 96
198 80 90 96
199 83 93 96
200 25 35 96
201 15 25 96
202 22 32 96
203 44 54 96
204 48 58 96
205 35 45 96
206 53 63 96
207 52 62 96
208 82 92 96
209 59 69 96
210 51 61 96
211 59 69 96
212 71 81 96
213 83 93 96
214 90 100 96
215 62 72 96
216 31 41 96
217 29 39 96
218 84 94 96
219 53 63 96
220 71 81 96
221 79 89 96
222 22 32 96
223 20 30 96
224 72 82 96
225 44 54 96
226 5 15 96
227 63 73 96
228 
229 Output:
230 10000
231 10000
232 10000
233 10000
234 10000
235 10000
236 10000
237 10000
238 10000
239 -1
240 10000
241 10000
242 10000
243 -1
244 10000
245 -1
246 10000
247 10000
248 -1
249 10000
250 10000
251 -1
252 10000
253 10000
254 10000
255 10000
256 -1
257 10000
258 -1
259 -1
260 10000
261 10000
262 10000
263 10000
264 10000
265 10000
266 10000
267 10000
268 10000
269 10000
270 10000
271 10000
272 10000
273 10000
274 10000
275 10000
276 10000
277 10000
278 -1
279 10000
280 10000
281 10000
282 -1
283 10000
284 10000
285 10000
286 10000
287 10000
288 10000
289 10000
290 -1
291 10000
292 10000
293 10000
294 10000
295 10000
296 10000
297 -1
298 10000
299 10000
300 10000
301 10000
302 -1
303 10000
304 -1
305 -1
306 -1
307 10000
308 10000
309 10000
310 10000
311 10000
312 10000
313 10000
314 -1
315 10000
316 10000
317 10000
318 10000
319 10000
320 10000
321 10000
322 -1
323 10000
324 -1
325 -1
326 -1
327 -1
328 10000
329 10000
text

 

 

小蒟蒻不才,如有错误,欢迎大佬们指正~
海思芯片YOLOv8模型部署完整指南:从RPN硬化到8bit量化精度优化 本文详细介绍了在海思芯片平台部署YOLOv8目标检测模型的完整方案,重点解决8bit量化精度损失问题。通过分析海思RPN硬化层的性能优势(1.9ms推理时间),对比不同量化策略(16bit仅损失0.6AP,8bit损失5.2AP),并深入定位到strides计算层是量化误差的主要来源。关键突破在于采用混合精度策略(敏感层16bit,其余8bit),使8bit量化AP回升4.1%,同时保持3.4ms的推理速度和60%的低NPU占用率。文章还提供了量化参数调整、校准数据集优化等进阶技巧,为边缘计算应用提供了性能 阅读详情

相关推荐

力软敏捷开发框架源码7.0.6旗舰版 JNPF开发平台3.4.5 企业版 旗舰版

JNPF作为作为一款深受用户喜爱的低代码产品,凭借雄厚的科研技术力量、丰富的软件开发项目经验、严谨的项目开发流程和高质量的售后服务,已成功帮助千家企业完成数字化项目,建立了覆盖不同行业客户和不同发展阶段的软件产品服务体系。

huoooos0的博客 1841

poj1984

Navigation Nightmare Time Limit:2000MS Memory Limit:30000K Total Submissions:2033 Accepted:848 Case Time Limit:1000MS Description Farmer John's pastor...

weixin_34075551的博客 182

华为服务器RAID配置避坑指南:如何通过iBMC快速识别阵列卡型号并完成初始化

本文详细解析华为服务器RAID配置全流程,重点介绍通过iBMC快速识别阵列卡型号、硬盘健康诊断及RAID级别选择的场景化策略。提供从iBMC登录到缓存策略调优的实战技巧,帮助工程师避免常见配置陷阱,提升服务器存储系统性能和稳定性。

weixin_28475533的博客 492

Navigation Nightmare POJ - 1984(带权并查集)

Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths (1 ...

caowenbo2311694605的博客 262

DP3(3D Diffusion Policy)算法详解:从点云感知到扩散动作生成

DP3 的全称是:DP3 = 3D Diffusion Policy\boxed{\text{DP3 = 3D Diffusion Policy}}DP3 = 3D Diffusion Policy​对应论文题目是:3D Diffusion Policy: Generalizable Visuomotor Policy Learning via Simple 3D Representations该工作发表于 Robotics: Science and Systems(RSS)2024。DP3 是一种面向机器

u013250861的博客 356

保姆级教程:用Python+OpenCV搞定Orbbec Gemini深度与彩色流对齐(附完整代码)

本文提供了一份详细的Orbbec Gemini深度与彩色流对齐教程,涵盖从环境配置到完整代码实现的每一步。通过解析深度流对齐原理,对比不同对齐模式,并提供工业级应用中的问题排查方法,帮助开发者快速掌握Orbbec Gemini相机的使用技巧。文章还包含进阶应用如点云生成与三维重建的实战代码。

weixin_29090917的博客 470

POJ 1984 Navigation Nightmare 并查集

Description Farmer John’s pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizonta...

dataxiang6224的博客 135

ChatGPT Plus完整开通指南:从账号注册到稳定使用全流程

自然语言处理(NLP)技术通过深度学习模型实现对人类语言的理解与生成,其核心原理是基于Transformer架构的大规模预训练。GPT-4作为当前最先进的生成式AI模型,在代码编写、文档分析等场景展现出显著的技术价值。在实际应用中,稳定的网络环境和合规的支付方式是确保AI助手持续服务的关键因素。针对ChatGPT Plus的开通,需要系统性地解决账号验证、虚拟信用卡支付等工程实践问题,才能实现工作流的高效整合。

weixin_34239592的博客 430

p1984_因为没有仔细看样例导致题目理解错误

Navigation NightmareTime Limit: 2000MSMemory Limit: 30000KTotal Submissions: 1345Accepted: 551Case Time Limit: 1000MSDescriptionFarmerJohns pasto

carrot12_24的专栏 545

Renesas:定时器输入捕获

一、PWM输入捕获 1、Smart Configurator配置 注意:在使用前,需要使能对应的定时器!否侧无法正常进行输入捕获! 2、脉冲宽度的获取 如何区分高低电平的脉冲宽度:最常用的方法就是在触发中断后,判断IO口的高低电平,来确认是上升沿触发的中断还是下降沿触发的中断。 若无法进入中断,可查看:Renesas:配置中断的常规流程 (1)中断方式(Smart Configurator生成的代码) #pragma interrupt r_Config_TAUB0_0_i...

不吃鱼的猫的博客 2484

uniapp:微信支付报错requestPayment:fail errors,uniAPP调用uni.requestPayment微信支付提示错误(安卓app、ios苹果app、微信小程序支付报错)

uniapp微信支付报错requestPayment:fail errors,uniApp APP端调起微信支付失败报错requestPayment:fail errors,uniapp微信小程序支付的坑 “errMsg“: “requestPayment:fail errors“,uni.requestPayment微信支付,UNI-APP支付报错“errMsg“: “requestPayment:fail errors“ app微信支付,uniapp 安卓app微信支付报错,uni.requestPay

✅ 精选推荐文章 3917

POJ 1984 Navigation Nightmare (离线并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 4599   Accepted: 1734 Case Time Limit: 1000MS Description Farmer John's pastoral neighbor

Tc的专栏 2247

android 蓝牙通讯demo

android 下蓝牙通讯demo,实现了发现蓝牙,使能蓝牙,通过蓝牙发送消息。

POJ 1984 Navigation Nightmare【带权并查集 二维】

POJ 1984 Navigation Nightmare 题目链接: vjudge传送门 题目大意: 依次给定二维坐标轴上的若干点的相对坐标,问在指定的前i次数据前,能否得到两点之间的距离(横纵坐标之差的绝对值之和)。 具体思路: 并查集,用两个权值数组hori[],vert[],分别维护水平方向和垂直方向上相对于根节点的相对距离,然后对每问两点的距离进行建树和输出。如果是一次query就建图一...

指弹键盘哼民谣 451

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

RU DOS版、EFI版32bit/64bit BIOS常用工具

DOS版、EFI版的RU工具,可用来查看IO、MEM、PCI、SMBIOS等资源。亲测在EFI SHELL下可用。

poj1984 Navigation Nightmare(带权并查集)

http://poj.org/problem?id=1984 题意:n个点,m个输入边,边中有权值和方向,q个查询,查询两点之间的最短路。 ps:好题,不过题目真尼玛长,这一道阅读的量啊。里面的index是啥始终看不懂,后来才知道每加入一条边需要1时间,而这个查询就是时间的限制。这题和poj1986很像,但是这题查询时两点不一定有关系,相当于在线,边输入边查询。而1986是离线,先

Iguodala的博客 875

POJ1984:Navigation Nightmare(带权并查集)

Navigation Nightmare Time Limit: 2000MS Memory Limit: 30000K Total Submissions: 7871 Accepted: 2831 Case Time Limit: 1000MS 题目链接:http://poj.org/problem?id=1984 De...

dft539533的博客 154

Navigation Nightmare POJ - 1984(带权并查集(两个权值))认真看注释一定可以理解

Navigation Nightmare POJ - 1984   点击打开链接题意:有n个点位于一平面上,现给出m条关系(给出的关系有时间上的顺序,每秒给一条)u  v  w  ch  。(ch是方向)表示v在u的  东/南/西/北 w米。再给出k次查询,每次查询给出两个点a,b,以及一个时间t,表示在时间t的...

JiaYao的博客 271

POJ1984 Navigation Nightmare —— 种类并查集

题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit:2000MS Memory Limit:30000K Total Submissions:7136 Accepted:2556 Case Time Limit:1000MS ...

alince20008的博客 147

BZOJ 3362 POJ 1984 Navigation Nightmare 带权并查集

题目大意:一些农场由一些东西向或者南北向的路相互连接。在不断加边的过程中会询问两个农场的曼哈顿距离是多少,如果目前还不连通,那么输出-1。 思路:带权并查集,f[i]为点i到father[i]的距离,要维护两个值,一个是东西向的距离,一个是南北向的距离,因为以后更新的时候要用到。在合并的时候有些特殊。现在有一条边(x->y),设fx为x的根,fy为y的根,那么现在知道f到fx的距离,y到

(╯°口°)╯(┴—┴ 1406

poj 1984 Navigation Nightmare (带权并查集)

题解思路: 带权并查集只不过维护2个量; 在线查询十分恶心.. 需要先把查询 和答案全记录下来,最后按pos输出; #include<cstdio> #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include&lt...

weixin_30446197的博客 148

POJ 1984 Navigation Nightmare 二维带权并查集

题目来源:POJ 1984 Navigation Nightmare 题意:给你一颗树 k次询问 求2点之间的曼哈顿距离 并且要在只有开始k条边的情况下 思路:按照方向 我是以左上角为根 左上角为原点 dx[i]为i点距离根的x坐标 dy[]是y坐标 这两个可以通过路径压缩求出 只不过是二维而已 #include #include #include using namespace st

嗯。 1277

POJ 1984 Navigation Nightmare

带权并查集

sdfzrlt的博客 446

POJ 1984 Navigation Nightmare(路径压缩并查集)

POJ 1984 Navigation Nightmare(路径压缩并查集) 分析:定最左上角的坐标为(0,0).x为横坐标,y为纵坐标. 首先本题需要离线处理,即先读入所有的输入数据,然后根据询问的时间点来处理. 其次,本题可用并查集,且x[]和y[]数组保存了当前点到其父节点的坐标差值.比如第一句输入为 1 2 3 N 表示从1点朝北方向长3的路可以到2点,那么2的父亲就是1点,且x[2

code 1474

POJ 1984 Navigation Nightmare(带权并查集)

Description Farmer John's pastoral neighborhood has N farms (2 <= N <= 40,000), usually numbered/labeled 1..N. A series of M (1 <= M < 40,000) vertical and horizontal roads each of varying lengths

←_← 303

POJ 1984 Navigation Nightmare(带权并查集)

Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 8001   Accepted: 2883 Case Time Limit: 1000MS Description Farmer John's pastoral neighborhood has ...

LSD20164388的博客 266
上一篇: 树的直径的求法即相关证明【树形DP || DFS】
下一篇: Codeforces Round #515 (Div. 3) B. Heaters【 贪心 区间合并细节 】
莜莫oO
博客等级 码龄9年 7粉丝 226原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值