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]表示最少 ...
随机推荐
- js实现观察者模式风格替换
如下图,我们看到两种风格:在选择男士时,页面颜色为黑色:在选择女士时,页面颜色为粉红色. 主要可以分为两类: 下拉框 ---> 被观察者 div ---> 观察者 面向过程实现风格替换: ...
- DedeCMS筛选简单实现方法不改后台源文件
笔者在前面几篇文章中提到的dedecms筛选的实现方法,对新手来说实现过程较复杂.接下来分享一个最简单的实现在移动端上筛选功能. 最近在筹备移动端企业站,实现功能有:实现一个管理后台管理PC端和移动端 ...
- 使用U盘作为启动盘安装ubuntu系统
一.使用U盘刻录镜像 1.安装之后我们打开软件,点击文件打开,找到我们刚才进行下载的Ubuntu的ISO文件,然后点击打开,完成ISO文件的加载.接着我们插入U盘,点击UltraISO启动选项,然后 ...
- mysql主从同步错误恢复
Mysql主从同步集群在生成环境使用过程中,如果主从服务器之间网络通信条件差或者数据库数据量非常大,容易导致MYSQL主从同步延迟. MYSQL主从产生延迟之后,一旦主库宕机,会导致部分数据没有及时同 ...
- CSU 1510 Happy Robot
1510: Happy Robot Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 19 Solved: 7 Description Input The ...
- 《R实战》读书笔记二
第一章 R简单介绍 本章概要 1安装R 2理解R语言 3执行R程序 本章所介绍的内容概括例如以下. 一个典型的数据分析步骤如图1所看到的. 图1:典型数据分析步骤 简而言之,现今的数据分析要求我们从多 ...
- HDU Victor and World (最短路+状态压缩)
题目链接:传送门 题意: n个城市m条路.刚開始在点1,求把每一个城市都遍历一边最后回到1的花费的最小值. 分析: +n2∗2n). 转自Bestcode. 以下说说我的状态转移,首先 ...
- .Net经典笔试题
1. 简述 private. protected. public. internal 修饰符的访问权限. 答:private:私有成员,在类的内部才可以访问: protected:保护成员,该类内部和 ...
- 2.CString转换到char*
多字节模式下: CString -->char * CString str1 ="; char *t1 =str1.GetBuffer(str1.GetLength()); str1. ...
- iOS菜鸟成长笔记(1)——第一个iOS应用
前言:阳光小强最近抽时间学习iOS开发,在学习过程中发现了很多有趣的东西也遇到了很多问题,为了在学习过程中能和大家交流,记录下学习的心得和学习成果,所以就有了这一个系列文章,希望这一系列文章能形成一个 ...