zoukankan      html  css  js  c++  java
  • P1262 间谍网络

    题目描述

    洛谷

    由于外国间谍的大量渗入,国家安全正处于高度的危机之中。如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B。有些间谍收受贿赂,只要给他们一定数量的美元,

    他们就愿意交出手中掌握的全部情报。所以,如果我们能够收买一些间谍的话,我们就可能控制间谍网中的每一分子。因为一旦我们逮捕了一个间谍,他手中掌握的情报都将归

    我们所有,这样就有可能逮捕新的间谍,掌握新的情报。

    我们的反间谍机关提供了一份资料,包括所有已知的受贿的间谍,以及他们愿意收受的具体数额。同时我们还知道哪些间谍手中具体掌握了哪些间谍的资料。

    假设总共有n个间谍(n不超过3000),每个间谍分别用1到3000的整数来标识。

    请根据这份资料,判断我们是否有可能控制全部的间谍,如果可以,求出我们所需要支付的最少资金。否则,输出不能被控制的一个间谍。

    solution

    首先,我们可以先判断间谍是否能全部别抓住。

    我们可以对可以被收买的间谍打上标记,再对他掌握情报的间谍打上标记。

    最后遍历一遍,如果有一个点没有标记,就说明间谍不能被全部抓住,直接输出NO就可以了。

    剩下的情况,就是都可以被抓住的情况。

    我们可以进行一遍缩点,把请联通分量的权值更新为强联通分量中,花费最少的点。

    在遍历一遍所有的边,同时把终点所在的强联通量的入度加一,表明他可以不买。

    入度为0的点使我们必须要买的点,最后答案加上这些强联通分量的权值就行了。

    code:

    #include<iostream>
    #include<cstdio>
    #include<algorithm>
    using namespace std;
    const int N = 3010;
    int n,p,m,ans,tot,num,top,cnt,x,val,u,v;
    int dfn[N],low[N],shu[N],sta[N],head[N],in[N],w[N],pri[N];
    bool vis[N],kongzhi[N];
    inline int read()
    {
    	int s = 0,w = 1; char ch = getchar();
    	while(ch < '0' || ch > '9'){if(ch == '-') w = -1; ch = getchar();}
    	while(ch >= '0' && ch <= '9'){s = s * 10+ch -'0'; ch = getchar();}
    	return s * w;
    }
    struct node{int to,net;}e[8010];
    void add(int x,int y)
    {
    	e[++tot].to = y;
    	e[tot].net = head[x];
    	head[x] = tot;
    }
    void tarjain(int x)
    {
    	dfn[x] = low[x] = ++num;
    	sta[++top] = x; vis[x] = 1;
    	for(int i = head[x]; i; i = e[i].net)
    	{
    		int to = e[i].to;
    		if(!dfn[to])
    		{
    			tarjain(to);
    			low[x] = min(low[x],low[to]);
    		}
    		else if(vis[to])
    		{
    			low[x] = min(low[x],dfn[to]);
    		}
    	}
    	if(dfn[x] == low[x])
    	{
    		cnt++; int y;
    		do
    		{
                         y = sta[top--];
                         vis[y] = 0;
                         shu[y] = cnt;
                         pri[cnt] = min(pri[cnt],w[y]);
    		}while(x != y);
    	}
    }
    int main()
    {
        n = read(); p = read();
        for(int i = 1; i <= n; i++) pri[i] = 2333333;//一定要初始化
        for(int i = 1; i <= n; i++) w[i] = 2333333;
        for(int i = 1; i <= p; i++)
        {
        	x = read(); val = read();
        	w[x] = val; kongzhi[x] = 1;
        }
        m = read();
        for(int i = 1; i <= m; i++)
        {
        	u = read(); v = read();
        	add(u,v); kongzhi[v] = 1;
        }
        for(int i = 1; i <= n; i++)//判断所有的间谍是否都能被控制
        {
        	if(!kongzhi[i])
        	{
        		cout<<"NO"<<endl;
        		cout<<i<<endl;
        		return 0;
        	}
        }
        for(int i = 1; i <= n; i++) if(!dfn[i])tarjain(i);//缩点
        {
        	for(int j = head[i]; j; j = e[j].net)
        	{
        		int to = e[j].to;
        		if(shu[i] != shu[to])
        		{
        			in[shu[to]]++;
        		}
        	}
        }
        for(int i = 1; i <= cnt; i++)
        {
        	if(in[i] == 0) ans += pri[i];//入度为0的为我们必须要买的点
        }
        cout<<"YES"<<endl;
        printf("%d\n",ans);
        return 0;
    }
    

    ENDING

  • 相关阅读:
    ServletConfig类
    坑爹的去哪儿网订酒店经历
    python + opencv + pycharm +语音生成
    最近看到的工作要求
    pip in windows
    路由器外接硬盘做nas可行吗?
    阅读201706
    scrum学习
    学习concurrency programming进展
    Reactor/Proactor的比较 (ZZ)
  • 原文地址:https://www.cnblogs.com/genshy/p/13416209.html
Copyright © 2011-2022 走看看