间谍网络

Posted ppxppx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了间谍网络相关的知识,希望对你有一定的参考价值。

洛咕

题意:由于外国间谍的大量渗入,国家安全正处于高度的危机之中。如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B。有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报。所以,如果我们能够收买一些间谍的话,我们就可能控制间谍网中的每一分子。因为一旦我们逮捕了一个间谍,他手中掌握的情报都将归我们所有,这样就有可能逮捕新的间谍,掌握新的情报。我们的反间谍机关提供了一份资料,包括所有已知的受贿的间谍,以及他们愿意收受的具体数额。同时我们还知道哪些间谍手中具体掌握了哪些间谍的资料。假设总共有\(n\)个间谍(\(n<=3000\)),每个间谍分别用1到3000的整数来标识。请根据这份资料,判断我们是否有可能控制全部的间谍,如果可以,求出我们所需要支付的最少资金。否则,输出不能被控制的一个间谍。

简化题意:\(N(N<=3000)\)个点\(M(M<=8000)\)条有向边,给定p个特殊的点以及它们的权值,问遍历整个图的最小代价(若\(x->y\)则从x走到y不需要任何花费).

分析:先tarjan缩点,重新建图得到一个有向无环图.然后对于入度为零的点(强连通分量),如果里面没有特殊点,那么肯定不能遍历整张图,如果里面有特殊点,把最小的权值累加进答案.

至于每个强连通分量里面最小的权值,我们可以在tarjan的时候就处理好.

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#define ll long long
using namespace std;
inline int read()
    int x=0,o=1;char ch=getchar();
    while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
    if(ch=='-')o=-1,ch=getchar();
    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();
    return x*o;

const int N=3005;
const int M=8005;
int n,m,p,ans;
int a[M],b[M],deg[N],val[N];
int tot,head[N],nxt[M],to[M];
int top,sum,timeclock;
int dfn[N],low[N],st[N],color[N],size[N];
inline void add(int a,int b)
    nxt[++tot]=head[a];head[a]=tot;to[tot]=b;

inline void tarjan(int u)
    dfn[u]=low[u]=++timeclock;
    st[++top]=u;
    for(int i=head[u];i;i=nxt[i])
        int v=to[i];
        if(!dfn[v])
            tarjan(v);
            low[u]=min(low[u],low[v]);
        
        else if(!color[v])
            low[u]=min(low[u],dfn[v]);
        
    
    if(low[u]==dfn[u])
        color[u]=++sum;
        if(val[u])size[sum]=min(size[sum],val[u]);
//处理好每个强连通分量里最小的权值,一定要有值才能更新
        while(st[top]!=u)
            color[st[top]]=sum;
            if(val[st[top]])size[sum]=min(size[sum],val[st[top]]);
            --top;
        
        --top;
    

int main()
    n=read();p=read();
    for(int i=1;i<=p;++i)
        int x=read(),y=read();
        val[x]=y;
    
    m=read();
    for(int i=1;i<=m;++i)
        a[i]=read(),b[i]=read();
        add(a[i],b[i]);
    
    for(int i=1;i<=n;++i)size[i]=1e9;//初始化极大值
    for(int i=1;i<=n;++i)if(!dfn[i])tarjan(i);
    tot=0;memset(head,0,sizeof(head));
    for(int i=1;i<=m;++i)//重新建图
        if(color[a[i]]!=color[b[i]])
            add(color[a[i]],color[b[i]]);
            ++deg[color[b[i]]];
        
    
    for(int i=1;i<=n;++i)//先特判
        if(size[color[i]]==1e9&&!deg[color[i]])
            printf("NO\n%d\n",i);return 0;
        
    for(int i=1;i<=sum;++i)
        if(!deg[i])ans+=size[i];
    printf("YES\n%d\n",ans);
    return 0;

以上是关于间谍网络的主要内容,如果未能解决你的问题,请参考以下文章

间谍网络

间谍网络

间谍网络

间谍网络

洛谷 P1262 间谍网络

洛谷P1262 间谍网络