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. iOS开发——剪切板

    剪切板的调用是很简单的,常用的就这两个,不管文字或是图片的粘贴复制,对剪切板的操作就是基于下面两个属性的: [UIPasteboard generalPasteboard].string [UIPas ...

  2. 洛谷3833 [SHOI2012]魔法树

    SHOI2012 D2T3 题目描述 Harry Potter 新学了一种魔法:可以让改变树上的果子个数.满心欢喜的他找到了一个巨大的果树,来试验他的新法术. 这棵果树共有N个节点,其中节点0是根节点 ...

  3. [NOIP2016普及组]魔法阵

    题目:洛谷P2119.Vijos P2012.codevs5624. 题目大意:有n件物品,每件物品有个魔法值.要求组成魔法阵(Xa,Xb,Xc,Xd),该魔法阵要满足Xa<Xb<Xc&l ...

  4. mysql主从同步错误恢复

    Mysql主从同步集群在生成环境使用过程中,如果主从服务器之间网络通信条件差或者数据库数据量非常大,容易导致MYSQL主从同步延迟. MYSQL主从产生延迟之后,一旦主库宕机,会导致部分数据没有及时同 ...

  5. Unity C# 设计模式(五)建造者模式

    定义: 将一个复杂对象的构建与它的表示分离,使得同样的构建过程可以创建不同的表示. 组成部分: 1.Builder:给出一个抽象接口,以规范产品对象的各个组成成分的建造.这个接口规定要实现复杂对象的哪 ...

  6. KM HDU 3718

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  7. 智能指针的->和 * 重载

  8. Launcher3实现壁纸居中

    Launcher3的wallpaper显示是动态的,与Launcher预置桌面数目有关,让壁纸居中,仅仅能确保第一次开机时壁纸居中,后面用户改动桌面数目后,就无法达到了.怎样要在默认桌面数目配置时居中 ...

  9. android 聊天室窗体

    public class MainActivity extends Activity { ScrollView scrollView; Button button; LinearLayout layo ...

  10. Entity Framework之Model First开发方式

    一.Model First开发方式 在项目一开始,就没用数据库时,可以借助EF设计模型,然后根据模型同步完成数据库中表的创建,这就是Model First开发方式.总结一点就是,现有模型再有表. 二. ...