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 ...
随机推荐
- 物联网网关MQTT应用与配置测试介绍
1.MQTT介绍: MQTT(Message Queuing Telemetry Transport,消息队列遥测传输协议),作为除Modbus外最常用的协议之一,因其基于发布/订阅的模式,具有资源消 ...
- Asp.Net MVC SingleServiceResolver类剖析
SingleServiceResolver一般用于类工厂创建和注入点接口留白.类工厂创建比如Controller控制依赖于此类的创建,注入点留白实质上是依赖注入所对外预留的接口. 以第二个特性为例. ...
- 90后iOS开发者的出路,如何规划30岁前的自己(程序员必修课)
前言: 最近发生了一些和我们没有直接关系但是有间接关系的事情.比如华为“清洗”高龄基层员工,比如游戏公司2号员工拿不到股份而离职.先不说事实到底如何,起码很多码农是心有戚戚焉. 最近一年多也发生了一些 ...
- 【转载】C# 中的委托和事件(详解)
<div class="postbody"> <div id="cnblogs_post_body" class="blogpost ...
- Linux下安装jupyter
又是美好的一天 开开心心写代码 1. 安装ipython, jupyter pip install ipython pip install jupyter 2. 生成配置文件[root@50e ...
- echarts3.x遇到的坑
此文章用来记录echarts3.x遇到的坑,方便以后自己不再犯. 1.柱形图设置了yAxis.splitArea.show=true,后面设置的splitLine就会变不可见了.也没在官方文档中找到说 ...
- vue+element搭建后台管理界面(支持table条件搜索)
代码地址(如果有帮助,请点个Star) vue:https://github.com/wwt729/ElementUIAdmin-master.git springboot后端:https://git ...
- 章节十五、7- 配置文件-Console Logging
一.创建xml文件 1.创建xml文件 在项目中我们需要专门建一个文件夹来放xml文件或者是其它文件. 2.然后对文件夹进行命名 3.选择new 其它 4.选择XML File 5.给xml文件命名 ...
- 大数据学习之旅2——从零开始搭hadoop完全分布式集群
前言 本文从零开始搭hadoop完全分布式集群,大概花费了一天的时间边搭边写博客,一步一步完成完成集群配置,所以相信大家按照本文一步一步来完全可以搭建成功.需要注意的是本文限于篇幅和时间的限制,也是为 ...
- windows 下 ctags 安装以及 tags 目录创建
最近处理 .as 格式代码,需要转换成 c# 格式, 用 VS 查看,无法跳转,十分蛋疼,又用起了好久没用的 VIM,配置 tags 文件,实现自动跳转. 1.下载ctag文件http://ctags ...