zoukankan      html  css  js  c++  java
  • Mouse Hunt CodeForces

    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?

    Input

    The first line contains as single integers nn (1n21051≤n≤2⋅105) — the number of rooms in the dormitory.

    The second line contains nn integers c1,c2,,cnc1,c2,…,cn (1ci1041≤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 (1ain1≤ai≤n) — aiai is the room the mouse will run to the next second after being in room ii.

    Output

    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.

    Examples

    Input
    5
    1 2 3 2 10
    1 3 4 3 3
    Output
    3
    Input
    4
    1 10 2 10
    2 4 2 2
    Output
    10
    Input
    7
    1 1 1 1 1 1 1
    2 2 2 3 6 7 6
    Output
    2

    Note

    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:

    • 1221→2→2→…;
    • 222→2→…;
    • 3223→2→2→…;
    • 43224→3→2→2→…;
    • 56765→6→7→6→…;
    • 6766→7→6→…;
    • 7677→6→7→…;

    So it's enough to set traps in rooms 22 and 66.

    一个连通块 肯定存在一个环

    所以找环上的最小值即可

    为什么一个连通块肯定存在一个环  因为每个点都有一个出度 。。。

    #include <bits/stdc++.h>
    #define mem(a, b) memset(a, b, sizeof(a))
    using namespace std;
    const int maxn = 1e5 + 10, INF = 0x7fffffff;
    typedef long long LL;
    int n, cnt;
    int a[maxn<<1], head[maxn<<1], vis[maxn<<1], pre[maxn<<1];
    int s, t;
    struct node
    {
        int u, v, next;
    }Node[maxn<<2];
    
    void add(int u, int v)
    {
        Node[cnt].u = u;
        Node[cnt].v = v;
        Node[cnt].next = head[u];
        head[u] = cnt++;
    }
    
    
    void dfs1(int u, int fa)
    {
        vis[u] = 1;
        for(int i=head[u]; i!=-1; i=Node[i].next)
        {
            node e = Node[i];
            if(!vis[e.v])
            {    
                pre[e.v] = u;
                dfs1(e.v, u);
            }
            else
            {    
                s = e.v;
                t = u;
                return;
            }
        }
    }
    int minn = INF;
    LL res = 0;
    void dfs2(int u)    //由t向e.v回溯,如果能回溯到s则说明这是一个新的环  那么就把res += minn   其实放到一个dfs里就好了 
    {
        minn = min(minn, a[u]);
    
        if(u == s) 
        {
            res += (LL)minn;
            return;
        }
        else if(u == 0) return;
        dfs2(pre[u]);
    }
    
    int main()
    {
        mem(head, -1);
        cnt = 0;
        cin>> n;
        for(int i=1; i<=n; i++)
            cin>> a[i];
        int u;
        for(int i=1; i<=n; i++)
        {
            cin>> u;
            add(i, u);
        }
        for(int i=1; i<=n; i++)
        {
            if(!vis[i])
            {
                minn = INF;
                d[i] = 0;
                dfs1(i, -1);
                dfs2(t);
    
            }
        }
        cout<< res <<endl;
        
        
        return 0;
    }

     

    自己选择的路,跪着也要走完。朋友们,虽然这个世界日益浮躁起来,只要能够为了当时纯粹的梦想和感动坚持努力下去,不管其它人怎么样,我们也能够保持自己的本色走下去。
  • 相关阅读:
    docker 的基本使用
    yum 工具安装的程序默认目录与位置
    Tampermonkey 脚本记录
    Windows 安装配置sublime-text3与破解
    heidisql 轻量级工具
    ubuntu 系统配置静态IP地址
    linux 日志分析
    Windows 桌面整理工具
    高效实用Linux命令行
    物联网实验4 alljoyn物联网实验之手机局域网控制设备
  • 原文地址:https://www.cnblogs.com/WTSRUVF/p/9606539.html
Copyright © 2011-2022 走看看