题目
有 nnn 间屋子和 111 只老鼠,老鼠会从第 iii 间房子跑到 aia_iai 间房子。在每间房子放捕鼠夹都有代价 cic_ici,而老鼠的初始位置可能是任何一个房间,为了保证抓到老鼠(无论老鼠在哪个房间,总有一种路径会经过有捕鼠夹的房子),你需要在若干房间放上捕鼠夹,求最小代价和。
数据范围:1⩽ai⩽n⩽2⋅10^5 ----1⩽ci⩽10^4.
题解:
(1)由于每个点的出度为0 所以形成的环是可以通成一条路的。所以 dfs一下这个环。找到最小值。就是需要在这个处放一个夹子。解决这一个环内的老鼠。并查集找到这样的多个环。
(2)这个环也是一个强连通分量。用tarjan对这些染色。找分量中出度为0的。每个分量用vector排序找到最小的。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#define m(a,b) memset(a,b,sizeof a)
using namespace std;
typedef long long ll;
const int N=2e5+5;
struct Edge{
int to,nex;}edge[N*100];
int head[N],vis[N],dfn[N],low[N],st[N],col[N],out[N],val[N];
int tot,cnt,index,color;
vector<int>vec[N];
void add(int from,int to)
{
edge[++tot]=(Edge){
to,head[from]};head[from]=tot;
}
void tarjan(int x)
{
dfn[x]=low[x]=++cnt;
st[++index]=x,vis[x]=1