CODEVS 4093 EZ的间谍网络
来源:互联网 发布:淘宝卖家新手全攻略 编辑:程序博客网 时间:2024/05/01 05:20
题目描述 Description
由于外国间谍的大量渗入,学校安全正处于高度的危机之中。YJY决定挺身而作出反抗。如果A间谍手中掌握着关于B间谍的犯罪证据,则称A可以揭发B。有些间谍收受贿赂,只要给他们一定数量的美元,他们就愿意交出手中掌握的全部情报。所以,如果我们能够收买一些间谍的话,我们就可能控制间谍网中的每一分子。因为一旦我们逮捕了一个间谍,他手中掌握的情报都将归我们所有,这样就有可能逮捕新的间谍,掌握新的情报。
我们的神通广大的YJY获得了一份资料,色括所有已知的受贿的间谍,及他们愿意收受的具体数额。同时我们还知道哪些间谍手中具体掌握了哪些间谍的资料。假设总共有n个间谍(n不超过3000),每个间谍分别用1到3000的整数来标识。
请根据这份资料,判断我们是否有可能控制全部的间谍,如果可以,求出我们所需要支付的最少资金。否则,输出不能被控制的一个间谍。
输入描述 Input Description
第一行只有一个整数n。
第二行是整数p。表示愿意被收买的人数,1≤p≤n。
接下来的p行,每行有两个整数,第一个数是一个愿意被收买的间谍的编号,第二个数表示他将会被收买的数额。这个数额不超过20000。
紧跟着一行只有一个整数r,1≤r≤8000。然后r行,每行两个正整数,表示数对(A, B),A间谍掌握B间谍的证据。
输出描述 Output Description
如果可以控制所有间谍,第一行输出YES,并在第二行输出所需要支付的贿金最小值。否则输出NO,并在第二行输出不能控制的间谍中,编号最小的间谍编号。
样例输入 Sample Input【样例1】
3
2
1 10
2 100
2
1 3
2 3
【样例2】
4
2
1 100
4 200
2
1 2
3 4
样例输出 Sample Output
【样例1】
YES
110
【样例2】
NO
3
听说今天你们提高组讲了tarjan?
@Loi_black 苗嘿嘿嘿%%%%%
随便水一波吧….
tarjan缩点题。
题目大意…给你一个图,其中有些点可以购买,购买了某个点就可以获得这个点能到的点。
如果不能通过购买其中的一些点而获得所有点的话,输出编号最小的点,这个比较好处理,如果有SCC不能到的话的缩掉,记录一下这个SCC中最小的点。
如果能获得所有点的话,输出最小花费。
显然,当我们处理了以后,只购买掉没有入度的SCC就可以了。(过程自证,特别水= =)
这题关键在缩点,直接看代码吧….讲的很详细。
#include<algorithm>#include<stack>#include<cstdio>#include<cmath>#include<iostream>#include<cstring>using namespace std;const int maxn=200000+500;const int inf=0x7ffffff;struct Edge{ int f; int to; int d; int next;}edge[maxn];int n;int head[maxn];int dq[maxn],mindq[maxn];int pre[maxn],low[maxn];int ff[maxn],tt[maxn];int minum[maxn];int rd[maxn],cd[maxn];int tot;void add(int f,int t){ edge[++tot].f=f; edge[tot].to=t; edge[tot].next=head[f]; head[f]=tot;}int scc[maxn];int sccnt;int dfscnt;stack<int>s;void dfs(int x){ pre[x]=low[x]=++dfscnt; s.push(x); for(int i=head[x];i;i=edge[i].next) { Edge e=edge[i]; if(!pre[e.to]) { dfs(e.to); low[x]=min(low[x],low[e.to]); } if(!scc[e.to]) { low[x]=min(low[x],pre[e.to]); } } if(pre[x]==low[x]) { sccnt++; minum[sccnt]=x; while(1) { int hah=s.top(); scc[hah]=sccnt; mindq[sccnt]=min(mindq[sccnt],dq[hah]); minum[sccnt]=min(minum[sccnt],hah); s.pop(); if(hah==x) { break; } } }}int main(){ int p; scanf("%d%d",&n,&p); for(int i=1;i<=n;i++) { dq[i]=mindq[i]=inf; } for(int i=1;i<=p;i++) { int a,b; scanf("%d%d",&a,&b); dq[a]=b; } int r; scanf("%d",&r); for(int i=1;i<=r;i++) { scanf("%d%d",&ff[i],&tt[i]); add(ff[i],tt[i]); } for(int i=1;i<=n;i++) { if(!pre[i]) dfs(i); } bool flag=0; for(int i=1;i<=r;i++) { if(scc[ff[i]]!=scc[tt[i]]) { rd[scc[tt[i]]]++; } } int nm=inf; int ans=0; for(int i=1;i<=sccnt;i++) { if(!rd[i]) { if(mindq[i]>=inf) { nm=min(nm,minum[i]); flag=1; break; } else { ans+=mindq[i]; } } } if(flag) { puts("NO"); printf("%d",nm); } else { puts("YES"); printf("%d",ans); } return 0;}
- CODEVS 4093 EZ的间谍网络
- 洛谷 P1262 间谍网络==Codevs 4093 EZ的间谍网络
- EZ的间谍网络
- 【codevs4093】EZ的间谍网络 tarjan
- CodeVS&Luogu 间谍网络
- 【基础练习】【强连通tarjan】codevs4093 EZ的间谍网络题解
- Idiot的间谍网络
- 间谍网络
- 间谍网络
- 网络间谍:你的共享文件夹网络监视器
- Codevs 4040 EZ系列之奖金
- 洛谷 P1262 间谍网络
- 洛谷 P1262 间谍网络
- [洛谷P1262]间谍网络
- P1262间谍网络
- 洛谷 P1262 间谍网络
- 洛谷1262 间谍网络
- 洛谷 P1262 间谍网络
- hdu 5119 Happy Matt Friends【dp】
- Spring核心技术之JDBC支持
- 二叉树
- storm简介
- 2016 四川省赛 Around the World
- CODEVS 4093 EZ的间谍网络
- PTA--一元多项式的乘法与加法运算
- adb 5037 端口占用解决
- leetcode 237. Delete Node in a Linked List
- 页式管理--物理地址计算问题小结
- 如何让Vim Gui启动时默认自动最大化
- 数据通路的习题研究
- javaWeb之Servlet装载情况
- 汉诺塔(必须经过中间柱子)递归与非递归详解与代码实现