305409: CF1027D. Mouse Hunt

Memory Limit:256 MB Time Limit:2 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Mouse Hunt

题意翻译

伯兰州立大学的医学部刚刚结束了招生活动。和以往一样,约80%的申请人都是女生并且她们中的大多数人将在未来4年(真希望如此)住在大学宿舍里。 宿舍楼里有$n$个房间和**一只老鼠**!女孩们决定在一些房间里设置捕鼠器来除掉这只可怕的怪物。在$i$号房间设置陷阱要花费$c_i$伯兰币。房间编号从$1$到$n$。 要知道老鼠不是一直原地不动的,它不停地跑来跑去。如果$t$秒时它在$i$号房间,那么它将在$t+1$秒时跑到$a_i$号房间,但这期间不会跑到别的任何房间里($i=a_i$表示老鼠没有离开原来的房间)。时间从$0$秒开始,一旦老鼠窜到了有捕鼠器的房间里,这只老鼠就会被抓住。 如果女孩们知道老鼠一开始在哪里不就很容易吗?不幸的是,情况不是这样,老鼠在第$0$秒时可能会在从$1$到$n$的任何一个房间内。 那么女孩们最少要花多少钱设置捕鼠器,才能保证老鼠无论从哪个房间开始流窜最终都会被抓到? #### 输入输出格式 **输入格式** 第1行一个整数n($1\le n \le 2*10^5$),表示宿舍楼里的房间数。 第2行有$n$个整数,$c_1, c_2, \dots, c_n(1\le c_i \le 10^4)$,$c_i$表示在$i$号房间设置捕鼠器的花费。 第3行有$n$个整数,$a_1, a_2, \dots, a_n(1\le a_i \le n)$,$a_i$是老鼠在$i$号房间时,下一秒会窜到的房间号。 **输出格式** 输出一个整数,表示设置捕鼠器的最小花费(保证无论老鼠一开始在什么位置最终都能被抓住!)。

题目描述

Medicine faculty of Berland State University has just finished their admission campaign. As usual, about $ 80\% $ of applicants are girls and majority of them are going to live in the university dormitory for the next $ 4 $ (hopefully) years. The dormitory consists of $ n $ rooms and a single mouse! Girls decided to set mouse traps in some rooms to get rid of the horrible monster. Setting a trap in room number $ i $ costs $ c_i $ burles. Rooms are numbered from $ 1 $ to $ n $ . Mouse doesn't sit in place all the time, it constantly runs. If it is in room $ i $ in second $ t $ then it will run to room $ a_i $ in second $ t + 1 $ without visiting any other rooms inbetween ( $ i = a_i $ means that mouse won't leave room $ i $ ). It's second $ 0 $ in the start. If the mouse is in some room with a mouse trap in it, then the mouse get caught into this trap. That would have been so easy if the girls actually knew where the mouse at. Unfortunately, that's not the case, mouse can be in any room from $ 1 $ to $ n $ at second $ 0 $ . What it the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from?

输入输出格式

输入格式


The first line contains as single integers $ n $ ( $ 1 \le n \le 2 \cdot 10^5 $ ) — the number of rooms in the dormitory. The second line contains $ n $ integers $ c_1, c_2, \dots, c_n $ ( $ 1 \le c_i \le 10^4 $ ) — $ c_i $ is the cost of setting the trap in room number $ i $ . The third line contains $ n $ integers $ a_1, a_2, \dots, a_n $ ( $ 1 \le a_i \le n $ ) — $ a_i $ is the room the mouse will run to the next second after being in room $ i $ .

输出格式


Print a single integer — the minimal total amount of burles girls can spend to set the traps in order to guarantee that the mouse will eventually be caught no matter the room it started from.

输入输出样例

输入样例 #1

5
1 2 3 2 10
1 3 4 3 3

输出样例 #1

3

输入样例 #2

4
1 10 2 10
2 4 2 2

输出样例 #2

10

输入样例 #3

7
1 1 1 1 1 1 1
2 2 2 3 6 7 6

输出样例 #3

2

说明

In the first example it is enough to set mouse trap in rooms $ 1 $ and $ 4 $ . If mouse starts in room $ 1 $ then it gets caught immideately. If mouse starts in any other room then it eventually comes to room $ 4 $ . In the second example it is enough to set mouse trap in room $ 2 $ . If mouse starts in room $ 2 $ then it gets caught immideately. If mouse starts in any other room then it runs to room $ 2 $ in second $ 1 $ . Here are the paths of the mouse from different starts from the third example: - $ 1 \rightarrow 2 \rightarrow 2 \rightarrow \dots $ ; - $ 2 \rightarrow 2 \rightarrow \dots $ ; - $ 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots $ ; - $ 4 \rightarrow 3 \rightarrow 2 \rightarrow 2 \rightarrow \dots $ ; - $ 5 \rightarrow 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots $ ; - $ 6 \rightarrow 7 \rightarrow 6 \rightarrow \dots $ ; - $ 7 \rightarrow 6 \rightarrow 7 \rightarrow \dots $ ; So it's enough to set traps in rooms $ 2 $ and $ 6 $ .

Input

题意翻译

伯兰州立大学的医学部刚刚结束了招生活动。和以往一样,约80%的申请人都是女生并且她们中的大多数人将在未来4年(真希望如此)住在大学宿舍里。 宿舍楼里有$n$个房间和**一只老鼠**!女孩们决定在一些房间里设置捕鼠器来除掉这只可怕的怪物。在$i$号房间设置陷阱要花费$c_i$伯兰币。房间编号从$1$到$n$。 要知道老鼠不是一直原地不动的,它不停地跑来跑去。如果$t$秒时它在$i$号房间,那么它将在$t+1$秒时跑到$a_i$号房间,但这期间不会跑到别的任何房间里($i=a_i$表示老鼠没有离开原来的房间)。时间从$0$秒开始,一旦老鼠窜到了有捕鼠器的房间里,这只老鼠就会被抓住。 如果女孩们知道老鼠一开始在哪里不就很容易吗?不幸的是,情况不是这样,老鼠在第$0$秒时可能会在从$1$到$n$的任何一个房间内。 那么女孩们最少要花多少钱设置捕鼠器,才能保证老鼠无论从哪个房间开始流窜最终都会被抓到? #### 输入输出格式 **输入格式** 第1行一个整数n($1\le n \le 2*10^5$),表示宿舍楼里的房间数。 第2行有$n$个整数,$c_1, c_2, \dots, c_n(1\le c_i \le 10^4)$,$c_i$表示在$i$号房间设置捕鼠器的花费。 第3行有$n$个整数,$a_1, a_2, \dots, a_n(1\le a_i \le n)$,$a_i$是老鼠在$i$号房间时,下一秒会窜到的房间号。 **输出格式** 输出一个整数,表示设置捕鼠器的最小花费(保证无论老鼠一开始在什么位置最终都能被抓住!)。

加入题单

上一题 下一题 算法标签: