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的更多相关文章

  1. TOJ 2776 CD Making

    TOJ 2776题目链接http://acm.tju.edu.cn/toj/showp2776.html 这题其实就是考虑的周全性...  贡献了好几次WA, 后来想了半天才知道哪里有遗漏.最大的问题 ...

  2. 【BZOJ-1863】trouble 皇帝的烦恼 二分 + DP

    1863: [Zjoi2006]trouble 皇帝的烦恼 Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 559  Solved: 295[Submit] ...

  3. (并查集)~APTX4869(fzu 2233)

    http://acm.fzu.edu.cn/problem.php?pid=2233 Problem Description 为了帮助柯南回到一米七四,阿笠博士夜以继日地研究APTX4869的解药.他 ...

  4. 快速幂 --- CSU 1556: Jerry's trouble

    Jerry's trouble Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1556 Mean: 略. ana ...

  5. HDU 4334 Trouble (暴力)

    Trouble Time Limit: 5000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Statu ...

  6. The trouble of Xiaoqian

    The trouble of Xiaoqian Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...

  7. Linux 常见的trouble shooting故障排错

    Linux 常见的trouble shooting故障排错 备份开机所必须运行的程序对一个运维人员来说是非常有必要的.在实际生产环境中,系统和数据基本都是安装在不同的硬盘上面,因为企业最关心的还是数据 ...

  8. HDU 4334 Trouble

    Trouble Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  9. 【BZOJ】【1863】【ZJOI2006】trouble 皇帝的烦恼

    二分+DP Orz KuribohG 神题啊= = 满足单调性是比较显然的…… 然而蒟蒻并不会判断能否满足……QwQ 神一样的DP姿势:f[i]表示第 i 个与第1个最多有多少个相同,g[i]表示最少 ...

随机推荐

  1. vsCode 快捷键、插件

    插件 参考链接:https://blog.csdn.net/shunfa888/article/details/79606277 快捷键及常用插件:https://www.jianshu.com/p/ ...

  2. 紫书 例题 10-27 UVa 10214(欧拉函数)

    只看一个象限简化问题,最后答案乘4+4 象限里面枚举x, 在当前这条固定的平行于y轴的直线中 分成长度为x的一段段.符合题目要求的点gcd(x,y) = 1 那么第一段1<= y <= x ...

  3. Python学习笔记(2)--基本数据类型

    在介绍基本数据类型之前,先说一个系统方法type():返回对象的数据类型,可以帮助我们查看系统的类型定义 python不同的版本,类型名称稍有不同,这里使用的是3.5.2版本 一.基本数据类型: 1. ...

  4. PKU 3281 Dining 网络流 (抄模板)

    题意: 农夫约翰为他的牛准备了F种食物和D种饮料.每头牛都有各自喜欢的食物和饮料,而每种食物或饮料只能分配给一头牛.最多能有多少头牛可以同时得到各自喜欢的食物和饮料? 思路: 用 s -> 食物 ...

  5. HDU——T 2647 Reward

    http://acm.hdu.edu.cn/showproblem.php?pid=2647 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  6. [2012山东省第三届ACM大学生程序设计竞赛]——Mine Number

    Mine Number 题目:http://acm.sdut.edu.cn/sdutoj/problem.php? action=showproblem&problemid=2410 Time ...

  7. ios中静态库的创建和使用、制作通用静态库(Cocoa Touch Static Library)

    创建静态库可能出于以下几个理由: 1.你想将工具类代码或者第三方插件快捷的分享给其他人而无需拷贝大量文件.2.你想让一些通用代码处于自己的掌控之下,以便于修复和升级.3.你想将库共享给其他人,但不想让 ...

  8. HttpClient方式调用接口的java 简单案例源码+附jar包

    1 package com.itNoob.httpClient; import org.apache.commons.httpclient.HttpClient; import org.apache. ...

  9. 【Codeforces Round #425 (Div. 2) A】Sasha and Sticks

    [Link]: [Description] [Solution] 傻逼题; 获取n/k; 对n/k的奇偶性讨论一下就好 [NumberOf WA] 0 [Reviw] [Code] #include ...

  10. 洛谷—— P1328 生活大爆炸版石头剪刀布

    https://www.luogu.org/problem/show?pid=1328 题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在< ...