1027D. Mouse Hunt:http://codeforces.com/contest/1027/problem/D

题意:

  有n个房间,每个房间放置捕鼠器的费用是不同的,已知老鼠在一个房间x,那么他一定会在下一秒到一个特定的房间a【x】。老鼠一开始可能在任意一个房间,问最少需要多少的费用,使得一定能捉到老鼠。

思路:

  这道题要在每个环上找一个费用最小的点,放置捕鼠器,进入环的那些点是不用放捕鼠器的。如何在dfs中找到环,并找到最小的点?在dfs中,如果发现下一个点已经走过,就说明遇到环了,那么用u = a[u] 遍历环即可。

#include <algorithm>
#include <iterator>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <iomanip>
#include <bitset>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <stack>
#include <cmath>
#include <queue>
#include <list>
#include <map>
#include <set>
#include <cassert>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000") //c++
#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue typedef long long ll;
typedef unsigned long long ull; typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3; //priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl '\n' #define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A) //用来压行
#define REP(i , j , k) for(int i = j ; i < k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que; const ll mos = 0x7FFFFFFF; //
const ll nmos = 0x80000000; //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //
const int mod = 1e9+;
const double esp = 1e-;
const double PI=acos(-1.0); template<typename T>
inline T read(T&x){
x=;int f=;char ch=getchar();
while (ch<''||ch>'') f|=(ch=='-'),ch=getchar();
while (ch>=''&&ch<='') x=x*+ch-'',ch=getchar();
return x=f?-x:x;
} /*-----------------------showtime----------------------*/
const int maxn = 2e5+;
int a[maxn],n,c[maxn];
int vis[maxn];
int in[maxn];
int dfs(int x,int i){
if(vis[x])return inf;
int res = inf;
vis[x] = i;
if(vis[a[x]] == i){
res = min(res, c[x]);
int u = a[x];
while(u!=x){
res = min(res, c[u]);
u = a[u];
}
}
return min(dfs(a[x], i), res); }
int main(){
scanf("%d", &n);
for(int i=; i<=n; i++)scanf("%d", &c[i]);
for(int i=; i<=n; i++)scanf("%d", &a[i]); int ans = ;
for(int i=; i<=n; i++){
int tmp = dfs(i,i);
if(tmp <inf)
ans += tmp;
} printf("%d\n", ans);
return ;
}

CF-1027D

【Edu49 1027D】 Mouse Hunt DFS 环的更多相关文章

  1. Codeforces 1027D Mouse Hunt (强连通缩点 || DFS+并查集)

    <题目链接> 题目大意: 有n个房间,每个房间都会有一只老鼠.处于第i个房间的老鼠可以逃窜到第ai个房间中.现在要清理掉所有的老鼠,而在第i个房间中防止老鼠夹的花费是ci,问你消灭掉所有老 ...

  2. Codeforces B. Mouse Hunt(强连通分解缩点)

    题目描述: Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  3. Mouse Hunt

    Mouse Hunt 给定一个n个点的图,每个点有权值\(c_i\),并且只有一条出边.现在你要在一些点上打标记,使得从任何一个点出发最终都会经过有标记的点.求标记点的权值和最小值. 就是找环啊!拓扑 ...

  4. CF1027D Mouse Hunt 思维

    Mouse Hunt time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  5. Mouse Hunt CodeForces - 1027D(思维 找环)

    Medicine faculty of Berland State University has just finished their admission campaign. As usual, a ...

  6. 【CF1027D】Mouse Hunt(拓扑排序,环)

    题意:给定n个房间,有一只老鼠可能从其中的任意一个出现, 在第i个房间设置捕鼠夹的代价是a[i],若老鼠当前在i号房间则下一秒会移动到b[i]号, 问一定能抓住老鼠的最小的总代价 n<=2e5, ...

  7. 【Codeforces 1027D】Mouse Hunt

    [链接] 我是链接,点我呀:) [题意] 题意 [题解] 先求出来强连通分量. 每个联通分量里面,显然在联通块的尽头(没有出度)放一个捕鼠夹就ok了 [代码] #include <bits/st ...

  8. FZU1920 Left Mouse Button(dfs)

     Problem 1920 Left Mouse Button Accept: 385    Submit: 719 Time Limit: 1000 mSec    Memory Limit : 3 ...

  9. 题解 CF1027D 【Mouse Hunt】

    这道题原本写了一个很复杂的DFS,然后陷入绝望的调试. 看了一下题解发现自己完全想复杂了. 这里大概就是补充一些题解没有详细解释的代码吧... (小声BB)现在最优解rank4(话说$O2$负优化什么 ...

随机推荐

  1. Cookie和Session的使用详解

    我们在使用接口请求时经常听到Cookie和Session的知识,那么它们的实际意义和使用场景在哪里呢 ?    介绍如下 一.首先需要了解的是为什么需要有Cookie和Session这两个东西:Htt ...

  2. 【MySQL】添加多个字段

    MySQL 遇到了添加多个字段的问题,尝试了几次,搞定了,记录下. 示例代码如下: alter table ad_data add ( exposure_count bigint(20) defaul ...

  3. Mac Android 配置环境变量

    进入终端,输入以下命令: cd ~ touch .bash_profile //没有该文件的话新建一个 vi .bash_profile //vim 形式打开 输入内容jdk变量配置内容: expor ...

  4. JDK1.8源码分析01之学习建议(可以延伸其他源码学习)

    序言:目前有个计划就是准备看一下源码,来提升自己的技术实力.同时现在好多面试官都喜欢问源码,问你是否读过JDK源码等等? 针对如何阅读源码,也请教了我的老师.下面就先来看看老师的回答,也许会有帮助呢. ...

  5. 【TCP/IP】ICMP协议

    ICMP协议有两种报文: 1,查询报文 2,差错报文

  6. 基于Starling的mask实现

    作为一个从c++转过来的程序员,flash原生的自定义mask实在是太好用,能方便实现各种效果,比如新手引导的高亮.viewport效果等.可惜starling的显示对象并不支持mask特性,查阅go ...

  7. 基于RobotFramework实现自动化测试

    Java + robotframework + seleniumlibrary 使用Robot Framework Maven Plugin(http://robotframework.org/Mav ...

  8. HTML发展历程

    HTML是超文本标记语言的缩写,不同于C或JAVA等编程语言,HTML由标签组成.通过标签可以在网页中插入文字.图片.链接.音频.视频等元素,进而描述网页.和Windows一样,随着技术的发展,HTM ...

  9. JDK基础必备面试十问

    1. new一个对象在Java内部做了哪些工作? 从静态角度来看,new一个对象表示创建一个类的对象实例. 从JVM运行角度来看,当JVM执行到new字节码时,首先会去查看类有没有被加载到内存以及初始 ...

  10. Day 02--选题与设计(二)

    1.今天我们主要设计了一下我们微信小程序可以实现的功能,客户操作的基本流程,研究了墨刀这个工具的使用方法并试着将想法转化为原型设计项目.我们给自己的系统起名为“天天好餐”.我们认为食堂订送餐与网络上的 ...