CF1027D Mouse Hunt 思维
2 seconds
256 megabytes
standard input
standard output
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%80% of applicants are girls and majority of them are going to live in the university dormitory for the next 44 (hopefully) years.
The dormitory consists of nn 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 ii costs cici burles. Rooms are numbered from 11 to nn.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room ii in second tt then it will run to room aiai in second t+1t+1 without visiting any other rooms inbetween (i=aii=ai means that mouse won't leave room ii). It's second 00 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 11 to nn at second 00.
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 nn (1≤n≤2⋅1051≤n≤2⋅105) — the number of rooms in the dormitory.
The second line contains nn integers c1,c2,…,cnc1,c2,…,cn (1≤ci≤1041≤ci≤104) — cici is the cost of setting the trap in room number ii.
The third line contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n) — aiai is the room the mouse will run to the next second after being in room ii.
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.
5
1 2 3 2 10
1 3 4 3 3
3
4
1 10 2 10
2 4 2 2
10
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
2
In the first example it is enough to set mouse trap in rooms 11 and 44. If mouse starts in room 11 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 44.
In the second example it is enough to set mouse trap in room 22. If mouse starts in room 22 then it gets caught immideately. If mouse starts in any other room then it runs to room 22 in second 11.
Here are the paths of the mouse from different starts from the third example:
- 1→2→2→…1→2→2→…;
- 2→2→…2→2→…;
- 3→2→2→…3→2→2→…;
- 4→3→2→2→…4→3→2→2→…;
- 5→6→7→6→…5→6→7→6→…;
- 6→7→6→…6→7→6→…;
- 7→6→7→…7→6→7→…;
So it's enough to set traps in rooms 22 and 66.
题意:一个寝室里有n个房间和一个老鼠,老鼠一开始可能在任意一个房间,老鼠会从房间i跳到a[i]房间,问在哪些房间下陷阱可以用最小的代价抓到老鼠?
分析:遍历所有房间,给从这些可能到达的房间打上标记,每次打标志在花费最小的房间布下陷阱
AC代码:
#include <map>
#include <set>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <bitset>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
#define ls (r<<1)
#define rs (r<<1|1)
#define debug(a) cout << #a << " " << a << endl
using namespace std;
typedef long long ll;
const ll maxn = 1e6+10;
const ll mod = 1e9+7;
const double pi = acos(-1.0);
const double eps = 1e-8;
ll n, ans, a[maxn], c[maxn], vis[maxn];
int main() {
ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
cin >> n;
for( ll i = 1; i <= n; i ++ ) {
cin >> c[i];
}
for( ll i = 1; i <= n; i ++ ) {
cin >> a[i];
}
for( ll i = 1; i <= n; i ++ ) {
ll x = i, v, mn;
while( !vis[x] ) { // 找到已被访问过的下一个房间
vis[x] = i, x = a[x];
}
if( vis[x] != i ) {
continue;
}
v = x, mn = c[x];
while( a[x] != v ) { //在可能经过的所有房间需要花费最小的房间下陷阱
x = a[x], mn = min( mn, c[x] );
}
ans += mn;
}
cout << ans << endl;
return 0;
}
CF1027D Mouse Hunt 思维的更多相关文章
- CF1027D Mouse Hunt题解
题目: 伯兰州立大学的医学部刚刚结束了招生活动.和以往一样,约80%的申请人都是女生并且她们中的大多数人将在未来4年(真希望如此)住在大学宿舍里. 宿舍楼里有nn个房间和一只老鼠!女孩们决定在一些房间 ...
- 【Edu49 1027D】 Mouse Hunt DFS 环
1027D. Mouse Hunt:http://codeforces.com/contest/1027/problem/D 题意: 有n个房间,每个房间放置捕鼠器的费用是不同的,已知老鼠在一个房间x ...
- Mouse Hunt
Mouse Hunt 给定一个n个点的图,每个点有权值\(c_i\),并且只有一条出边.现在你要在一些点上打标记,使得从任何一个点出发最终都会经过有标记的点.求标记点的权值和最小值. 就是找环啊!拓扑 ...
- Codeforces B. Mouse Hunt(强连通分解缩点)
题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
- Mouse Hunt CodeForces - 1027D(思维 找环)
Medicine faculty of Berland State University has just finished their admission campaign. As usual, a ...
- 【CF1027D】Mouse Hunt(拓扑排序,环)
题意:给定n个房间,有一只老鼠可能从其中的任意一个出现, 在第i个房间设置捕鼠夹的代价是a[i],若老鼠当前在i号房间则下一秒会移动到b[i]号, 问一定能抓住老鼠的最小的总代价 n<=2e5, ...
- 题解 CF1027D 【Mouse Hunt】
这道题原本写了一个很复杂的DFS,然后陷入绝望的调试. 看了一下题解发现自己完全想复杂了. 这里大概就是补充一些题解没有详细解释的代码吧... (小声BB)现在最优解rank4(话说$O2$负优化什么 ...
- Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)
<题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...
- 【Codeforces 1027D】Mouse Hunt
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 先求出来强连通分量. 每个联通分量里面,显然在联通块的尽头(没有出度)放一个捕鼠夹就ok了 [代码] #include <bits/st ...
随机推荐
- 运行sh文件
记下在Ubuntu下安装*.sh和*.bin的简单方法. *.sh文件安装方法: 运行终端到文件目录下 1.在终端输入:sudo sh *.sh直接运行 2.在终端输入:sudo chmod +x * ...
- DataOps系列丨数据的“资产负债表”与“现状”
作者:DataPipeline CEO 陈诚 <跨越鸿沟>的作者Geoffrey Moore曾说“没有数据,运营企业就像一个又聋又瞎的人在高速上开车一样”.数据的价值从未像现在这样被企业重 ...
- HTML5 第二章 列表和表格和媒体元素
列表: (1)什么是列表? 列表就是信息资源的一种展示形式. (2)无序列表: 语法: <ul> <li>第1项</li> <li>第2项</li ...
- axios配置请求头content-type
现在前端开发中需要通过Ajax发送请求获取后端数据是很普遍的一件事情了,鉴于我平时在撸码中用的是vue技术栈,今天这里来谈谈我们常用的发Ajax请求的一个插件—axios.> 现在网上可能发送A ...
- IPC机制1
1.Android IPC简介 Inter-Process Communication的缩写就是IPC,含义是进程间通信或是跨进程间通信,是指两个进程进行交换数据的过程. 进程是什么? 进程在pc上就 ...
- 基于jmeter+perfmon的稳定性测试记录
1. 引子 最近承接了项目中一些性能测试的任务,因此决定记录一下,将测试的过程和一些心得收录下来. 说起来性能测试算是软件测试行业内,有些特殊的部分.这部分的测试活动,与传统的测试任务差别是比较大的, ...
- 大数据学习之旅1——HDFS版本演化
最近开始学习大数据,发现大数据有很多很多组件,我现在负责的是HDFS(Hadoop分布式储存系统)的学习,整理了一下HDFS的版本情况.因为HDFS是Hadoop的重要组成部分,所以有关HDFS的版本 ...
- 编程使用c#连接到IBM db2的两种方式
一:使用c#通过odbc连接到IBM db2使用 ConnectionString 属性连接到各种数据源. 部署:只要在客户端安装IBM DB2 ODBC driver.配置DSn即可. 1):可以单 ...
- 帝国CMS(EmpireCMS) v7.5配置文件写入漏洞分析
帝国CMS(EmpireCMS) v7.5配置文件写入漏洞分析 一.漏洞描述 该漏洞是由于安装程序时没有对用户的输入做严格过滤,导致用户输入的可控参数被写入配置文件,造成任意代码执行漏洞. 二.漏洞复 ...
- 携程PMO--如何召开卓有成效的回顾会
话题介绍 回顾会提供团队反思迭代过程并提出改进措施的机会.回顾会是团队成员共同进行的协作活动,让团队成员跟进并落实改进措施,使团队在下一个冲刺中更高效,这是相当重要的. 我们给出了回顾会的 ...