codeforce Edu49 - 1027D. Mouse Hunt DFS 环

Posted ckxkexing

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了codeforce Edu49 - 1027D. Mouse Hunt DFS 环相关的知识,希望对你有一定的参考价值。

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 << "
";
#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 ‘
‘

#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;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;       
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 1e9+7;
const double esp = 1e-8;
const double PI=acos(-1.0);



template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<0||ch>9) f|=(ch==-),ch=getchar();
    while (ch>=0&&ch<=9) x=x*10+ch-0,ch=getchar();
    return x=f?-x:x;
}


/*-----------------------showtime----------------------*/
            const int maxn = 2e5+9;
            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=1; i<=n; i++)scanf("%d", &c[i]);
            for(int i=1; i<=n; i++)scanf("%d", &a[i]);

            int ans = 0;
            for(int i=1; i<=n; i++){
                    int tmp = dfs(i,i);
                    if(tmp <inf)
                        ans += tmp;
            }
           
            printf("%d
", ans);
            return 0;
}
CF-1027D

 

以上是关于codeforce Edu49 - 1027D. Mouse Hunt DFS 环的主要内容,如果未能解决你的问题,请参考以下文章

codeforce#edu125d 补题

codeforce#edu125d 补题

Codeforces Edu80

[Codeforces Round49F] Session in BSU

Codeforces Edu Round 57 A-D

Codeforces Edu Round 48 A-D