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. uwsgi erro

    Installing collected packages: uwsgi Running setup.py install for uwsgi: started Running setup.py in ...

  2. [JLOI2015]装备购买(线性基)

    [JLOI2015]装备购买 题目描述 脸哥最近在玩一款神奇的游戏,这个游戏里有 nn 件装备,每件装备有 \(m\) 个属性,用向量 \(\mathbf{z_i}\)=\((a_1, \ldots ...

  3. 洛谷 P1171 售货员的难题

    P1171 售货员的难题 题目背景 数据有更改 题目描述 某乡有n个村庄(1<n<20),有一个售货员,他要到各个村庄去售货,各村庄之间的路程s(0<s<1000)是已知的,且 ...

  4. MethodFilterInterceptor(方法拦截器)配置excludeMethors

    由于该类有setExcludeMethods方法,因此在xml中可以配置一个excludeMethods参数 刚开始老是拦截不成功,tomcat显示这个参数没找到,后来终于找到错误:不应该在拦截器栈中 ...

  5. openCV 和GDI画线效率对照

    一. 因为项目须要,原来用GDI做的画线的功能.新的项目中考虑到垮平台的问题.打算用openCV来实现.故此做个效率对照. 二. 2点做一条线,来測试效率. 用了相同的画板大小---256*256的大 ...

  6. js插件---图片裁剪cropImgBox(适合练习编写插件之用)

    js插件---图片裁剪cropImgBox(适合练习编写插件之用) 一.总结 一句话总结:无论是灰度还是高对比度的图片,都是先处理canvas的像素,使其变成灰度或者高对比度,然后再用canvas.t ...

  7. 利用CORS实现跨域请求--转

    原文地址:http://newhtml.net/using-cors/ 跨域请求一直是网页编程中的一个难题,在过去,绝大多数人都倾向于使用JSONP来解决这一问题.不过现在,我们可以考虑一下W3C中一 ...

  8. android学习笔记五。2、其他组件

    一.ContentProvider内容提供者.是是android中一个应用向第三方共享数据的方式,android中的联系人,sms(短信记录)等都是通过这一方式来向外提供的 1.使用: 在应用中使用C ...

  9. 最长回文子串 hihocode 1032 hdu 3068

    最长回文 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  10. MongoDB常用操作整理

    Mongodb:是一种NoSQL数据库,NoSQL:Not Only SQLSQL: 数据表->JDBC读取->POJO(VO.PO)->控制层转化为JSON数据->客户端 这 ...