A12053 | Mouse Hunt
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
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 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$ .
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$ .
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$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted