TOJ 2233 WTommy's Trouble
2233. WTommy's Trouble
Time Limit: 2.0 Seconds Memory Limit: 65536K
Total Runs: 1499 Accepted Runs: 437
As the captain, WTommy often has to inform all the TJU ACM team members of something important. But it will cost much time to inform all the members one by one. So WTommy chooses some people to inform, then he lets them inform all the people they know, and these informed people will inform more people. At last all the people will be informed.
Given the time cost to inform each person at the beginning, WTommy wants to find the minimum time he has to spend, so that at last all the people will be informed. Because the number of people can be as large as ten thousand, (eh... Maybe all the students in the university will join the ACM team? ) WTommy turns to you for help.
Please note it's possible that A knows B but B doesn't know A.
Input
The first line of each test case contains two integers N and M, indicating the number of people and the number of relationships between them. The second line contains N numbers indicating the time cost to inform each people. Then M lines followed, each contains two numbers Ai and Bi, indicating that Ai knows Bi.
You can assume that 1 ≤ N ≤ 10000, 0 ≤ M ≤ 200000. The time costs for informing each people will be positive and no more than 10000. All the people are numbered from 1 to N.
The input is terminated by a line with N = M = 0.
Output
Output one line for each test case, indicating the minimum time WTommy has to spend.
Sample Input
4 3
30 20 10 40
1 2
2 1
2 3
0 0
Sample Output
60
Hint
For the sample input, WTommy should inform two members, No.2 and No.4, which costs 20 + 40 = 60.
Author: RoBa
Source: TOJ 2006 Weekly Contest 6
解题:强连通缩点求出每个强连通分量的最小值,然后看缩点后入度为0的点的值的和
#include <iostream>
#include <cstring>
#include <cstdio>
#include <vector>
#include <stack>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
vector<int>g[maxn];
int belong[maxn],dfn[maxn],low[maxn],idx,scc;
int n,m,minV[maxn],val[maxn],in[maxn];
bool instack[maxn];
stack<int>stk;
void init(){
for(int i = ; i < maxn; ++i){
dfn[i] = low[i] = belong[i] = ;
instack[i] = false;
in[i] = ;
g[i].clear();
}
idx = scc = ;
while(!stk.empty()) stk.pop();
}
void tarjan(int u){
dfn[u] = low[u] = ++idx;
instack[u] = true;
stk.push(u);
for(int i = g[u].size()-; i >= ; --i){
if(!dfn[g[u][i]]){
tarjan(g[u][i]);
low[u] = min(low[u],low[g[u][i]]);
}else if(instack[g[u][i]]) low[u] = min(low[u],dfn[g[u][i]]);
}
if(low[u] == dfn[u]){
int v;
scc++;
minV[scc] = INF;
do{
instack[v = stk.top()] = false;
stk.pop();
belong[v] = scc;
minV[scc] = min(minV[scc],val[v]);
}while(v != u);
}
}
int main(){
int u,v;
while(scanf("%d %d",&n,&m),n||m){
init();
for(int i = ; i <= n; ++i)
scanf("%d",val+i);
for(int i = ; i < m; ++i){
scanf("%d %d",&u,&v);
g[u].push_back(v);
}
for(int i = ; i <= n; ++i)
if(!dfn[i]) tarjan(i);
int ans = ;
for(int i = ; i <= n; ++i)
for(int j = g[i].size()-; j >= ; --j)
if(belong[i] != belong[g[i][j]]) in[belong[g[i][j]]]++;
for(int i = ; i <= scc; ++i)
if(!in[i]) ans += minV[i];
printf("%d\n",ans);
}
return ;
}
TOJ 2233 WTommy's Trouble的更多相关文章
- TOJ 2776 CD Making
TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性... 贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...
- 【BZOJ-1863】trouble 皇帝的烦恼 二分 + DP
1863: [Zjoi2006]trouble 皇帝的烦恼 Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 559 Solved: 295[Submit] ...
- (并查集)~APTX4869(fzu 2233)
http://acm.fzu.edu.cn/problem.php?pid=2233 Problem Description 为了帮助柯南回到一米七四,阿笠博士夜以继日地研究APTX4869的解药.他 ...
- 快速幂 --- CSU 1556: Jerry's trouble
Jerry's trouble Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1556 Mean: 略. ana ...
- HDU 4334 Trouble (暴力)
Trouble Time Limit: 5000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Statu ...
- The trouble of Xiaoqian
The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- Linux 常见的trouble shooting故障排错
Linux 常见的trouble shooting故障排错 备份开机所必须运行的程序对一个运维人员来说是非常有必要的.在实际生产环境中,系统和数据基本都是安装在不同的硬盘上面,因为企业最关心的还是数据 ...
- HDU 4334 Trouble
Trouble Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- 【BZOJ】【1863】【ZJOI2006】trouble 皇帝的烦恼
二分+DP Orz KuribohG 神题啊= = 满足单调性是比较显然的…… 然而蒟蒻并不会判断能否满足……QwQ 神一样的DP姿势:f[i]表示第 i 个与第1个最多有多少个相同,g[i]表示最少 ...
随机推荐
- java源码学习
Collection List ArrayList LinkedList Vector Stack Set HashSet TreeSet Map HashMap TreeMap LinkedHash ...
- 参考《深度学习之PyTorch实战计算机视觉》PDF
计算机视觉.自然语言处理和语音识别是目前深度学习领域很热门的三大应用方向. 计算机视觉学习,推荐阅读<深度学习之PyTorch实战计算机视觉>.学到人工智能的基础概念及Python 编程技 ...
- Linux 中常用的基础命令一
1.目录相关命令的使用 pwd(printing working directory) 显示当前工作目录 pwd命令相关的环境变量: PWD 保存了当前工作目录路径 OLDP ...
- Delayer 基于 Redis 的延迟消息队列中间件
Delayer 基于 Redis 的延迟消息队列中间件,采用 Golang 开发,支持 PHP.Golang 等多种语言客户端. 参考 有赞延迟队列设计 中的部分设计,优化后实现. 项目链接:http ...
- 树根 Digital root
数根 (又称数字根Digital root)是自然数的一种性质.换句话说.每一个自然数都有一个数根.数根是将一正整数的各个位数相加(即横向相加),若加完后的值大于等于10的话,则继续将各位数进行横向相 ...
- vue5 过滤器 模版
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Cordic 算法入门
三角函数的计算是个复杂的主题,有计算机之前,人们通常通过查找三角函数表来计算任意角度的三角函数的值.这种表格在人们刚刚产生三角函数的概念的时候就已经有了,它们通常是通过从已知值(比如sin(π/2)= ...
- 控制器不存在:app\admin\controller\Document
控制器不存在:app\admin\controller\Document 报错: 控制器不存在:app\admin\controller\Document 但是我在代码里面找了半天没出现Documen ...
- tensorflow学习之路---Session、Variable(变量)和placeholder
---恢复内容开始--- 1.Session '''Session.run():首先里面的参数是一个API(函数的接口)的返回值或者是指定参数的值:功能:得知运算结果有两种访问方式:直接建立或者运用w ...
- 学习推荐《精通Python网络爬虫:核心技术、框架与项目实战》中文PDF+源代码
随着大数据时代的到来,我们经常需要在海量数据的互联网环境中搜集一些特定的数据并对其进行分析,我们可以使用网络爬虫对这些特定的数据进行爬取,并对一些无关的数据进行过滤,将目标数据筛选出来.对特定的数据进 ...