这道题搞明白了好开心啊!!!感激LY大佬!!!
http://codeforces.com/contest/1027/problem/D
D. Mouse Hunt
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Medicine faculty of Berland State University has just finished their admission campaign. As usual, about 80%
of applicants are girls and majority of them are going to live in the university dormitory for the next 4
(hopefully) years.
The dormitory consists of n
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 i costs ci burles. Rooms are numbered from 1 to n
.
Mouse doesn't sit in place all the time, it constantly runs. If it is in room i
in second t then it will run to room ai in second t+1 without visiting any other rooms inbetween (i=ai means that mouse won't leave room i). It's second 0
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 1
to n at second 0
.
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 n
(1≤n≤2⋅105
) — the number of rooms in the dormitory.
The second line contains n
integers c1,c2,…,cn (1≤ci≤104) — ci is the cost of setting the trap in room number i
.
The third line contains n
integers a1,a2,…,an (1≤ai≤n) — ai is the room the mouse will run to the next second after being in room i
.
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
Copy
5
1 2 3 2 10
1 3 4 3 3
Output
Copy
3
Input
Copy
4
1 10 2 10
2 4 2 2
Output
Copy
10
Input
Copy
7
1 1 1 1 1 1 1
2 2 2 3 6 7 6
Output
Copy
2
Note
In the first example it is enough to set mouse trap in rooms 1
and 4. If mouse starts in room 1 then it gets caught immideately. If mouse starts in any other room then it eventually comes to room 4
.
In the second example it is enough to set mouse trap in room 2
. If mouse starts in room 2 then it gets caught immideately. If mouse starts in any other room then it runs to room 2 in second 1
.
Here are the paths of the mouse from different starts from the third example:
- 1→2→2→…
- ;
- 2→2→…
- ;
- 3→2→2→…
- ;
- 4→3→2→2→…
- ;
- 5→6→7→6→…
- ;
- 6→7→6→…
- ;
- 7→6→7→…
- ;
So it's enough to set traps in rooms 2
and 6.
这道题就是给了n个位置放捕鼠器的价值。然后给了n个数字,代表点 i 能够走到这个点。问最少价值放捕鼠器使得老鼠全被抓到。
思路:无非就是有环,无环的情况。(有环的时候还不要忘记考虑“纯环”,就是没有东西引进去的环。)关于下图情况的处理方法代码注释得很详细。
有环的话一定是在环上并且该点花费最小。
无环的话就是放在最后那个点那里。
遇到还了的话就要进去找一圈。
代码很好!!!!!!!

代码:
#include<bits/stdc++.h>
using namespace std;
const int MAX=2e5+5;
int T,n,c[MAX],fa[MAX],num,in[MAX],out[MAX],look[MAX],ss[MAX];
long long ans;
void PP(int rt,int ff,int ok,int minn) ///rt代表当前扫到了哪个点。ff代表起点。ok
{
look[rt]=1;
if( ok && rt==ff ) ///又回到了起点
{
ans+=minn;
return;
}
PP( fa[rt] , ff , 1 , min(minn,c[rt]) );///ok代表当前节点有没有被访问过,这样就不用另开数组了
}
void dfs(int rt,int id)///rt代表当前扫到了哪个点
{
if( ss[rt]!=0 && ss[rt]!=id && look[rt] )///如果这个点之前被访问过,并且这个点所属的初始点不是当前的初始点,就说明之前的一条链就访问过了,此时后边一定有捕鼠器,就不用再找了
return;
ss[rt]=id;
if( look[rt] )///如果该店原来就访问过,说明环出现了!!!0代表第一次走这个点
{PP(rt,rt,0,0x3f3f3f3f);return;}
look[rt]=1;
if(fa[rt]==rt)///说明走到这里就停止了,这个位置必须放捕鼠器!!!
{ans+=c[rt];return;}
dfs(fa[rt],id);
}
int main()
{
scanf("%d",&n);
ans=0;
for(int i=1;i<=n;i++)
scanf("%d",&c[i]);
for(int i=1;i<=n;i++)
{
scanf("%d",&fa[i]);
if(fa[i]==i)
{
continue;
}
in[fa[i]]++;
}
for(int i=1;i<=n;i++)
{
if(in[i]==0)
dfs(i,i);
}
for(int i=1;i<=n;i++)
{
if(look[i]==0)///为了判断是环的情况!!!!一定不能忘记!!!!
PP(i,i,0,0x3f3f3f3f);
}
printf("%lld\n",ans);
}

本文介绍了一种通过放置捕鼠器来确保捕获所有可能路径上的老鼠的算法。重点在于如何利用图论中的环和链的概念,以最小的成本实现目标。
561

被折叠的 条评论
为什么被折叠?



