D. Mouse Hunt(环)

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

这道题搞明白了好开心啊!!!感激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);
}

 

在 C# 中实现图片缩放功能,尤其是涉及 `MouseWheel` 事件的绑定与解除绑定,通常用于图像处理或可视化控件中。例如,在使用 Halcon 或其他视觉库时,开发者可能希望通过鼠标滚轮控制图像的缩放级别。以下是一个完整的示例说明如何实现该功能,并包含 `this.MouseWheel -= hSmartWindowControl_D.HSmartWindowControl_MouseWheel` 的上下文。 ### 示例代码 假设你正在使用一个自定义控件 `HSmartWindowControl` 来显示图像,并希望通过鼠标滚轮进行图像缩放操作。 ```csharp using System; using System.Drawing; using System.Windows.Forms; namespace ImageZoomExample { public partial class MainForm : Form { private HSmartWindowControl hSmartWindowControl_D; private double zoomFactor = 1.0; // 缩放因子 public MainForm() { InitializeComponent(); // 初始化 HSmartWindowControl 控件 hSmartWindowControl_D = new HSmartWindowControl(); hSmartWindowControl_D.Dock = DockStyle.Fill; this.Controls.Add(hSmartWindowControl_D); // 加载图像 LoadImage("path_to_your_image.png"); // 绑定鼠标滚轮事件 this.MouseWheel += hSmartWindowControl_D.HSmartWindowControl_MouseWheel; this.MouseWheel += MainForm_MouseWheel; } private void LoadImage(string imagePath) { // 假设 HSmartWindowControl 有加载图像的方法 hSmartWindowControl_D.LoadImage(imagePath); } private void MainForm_MouseWheel(object sender, MouseEventArgs e) { if (e.Delta > 0) { // 向上滚动,放大图像 zoomFactor *= 1.1; } else if (e.Delta < 0) { // 向下滚动,缩小图像 zoomFactor /= 1.1; } // 应用缩放变换 hSmartWindowControl_D.SetScale(zoomFactor); // 假设有 SetScale 方法 } protected override void OnFormClosed(FormClosedEventArgs e) { // 在窗体关闭时移除事件订阅 this.MouseWheel -= hSmartWindowControl_D.HSmartWindowControl_MouseWheel; this.MouseWheel -= MainForm_MouseWheel; base.OnFormClosed(e); } } } ``` ### 关键点说明 - **HSmartWindowControl** 是一个用于图像显示和处理的控件,常见于 Halcon 视觉库的应用程序中。 - `this.MouseWheel += hSmartWindowControl_D.HSmartWindowControl_MouseWheel;` 表示将默认的滚轮事件绑定到控件的处理函数上。 - 在 `MainForm_MouseWheel` 方法中,根据滚轮方向调整 `zoomFactor`,并通过 `SetScale()` 方法应用缩放[^1]。 - 窗体关闭时,使用 `this.MouseWheel -= hSmartWindowControl_D.HSmartWindowControl_MouseWheel;` 移除事件订阅,防止内存泄漏[^2]。 ### 注意事项 - 确保 `HSmartWindowControl` 支持 `SetScale` 或类似方法来改变图像的缩放比例。 - 如果未正确解除事件绑定,可能会导致应用程序在关闭时仍然监听滚轮事件,从而引发异常。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值