HDU 3072 SCC Intelligence System
给出一个带权有向图,要使整个图连通。SCC中的点之间花费为0,所以就先缩点,然后缩点后两点之间的权值为最小边的权值,把这些权值累加起来就是答案。
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <stack>
using namespace std; int n, m; const int maxn = + ; vector<int> G[maxn], C[maxn]; stack<int> S;
int pre[maxn], lowlink[maxn], sccno[maxn];
int dfs_clock, scc_cnt; void dfs(int u)
{
pre[u] = lowlink[u] = ++dfs_clock;
S.push(u);
for(int i = ; i < G[u].size(); i++)
{
int v = G[u][i];
if(!pre[v])
{
dfs(v);
lowlink[u] = min(lowlink[u], lowlink[v]);
}
else if(!sccno[v]) lowlink[u] = min(lowlink[u], pre[v]);
} if(lowlink[u] == pre[u])
{
scc_cnt++;
for(;;)
{
int x = S.top(); S.pop();
sccno[x] = scc_cnt;
if(x == u) break;
}
}
} void find_scc()
{
dfs_clock = scc_cnt = ;
memset(pre, , sizeof(pre));
memset(sccno, , sizeof(sccno));
for(int i = ; i < n; i++) if(!pre[i]) dfs(i);
} int cost[maxn]; int main()
{
while(scanf("%d%d", &n, &m) == )
{
for(int i = ; i < n; i++) { G[i].clear(); C[i].clear(); }
while(m--)
{
int u, v, d; scanf("%d%d%d", &u, &v, &d);
G[u].push_back(v); C[u].push_back(d);
}
find_scc(); memset(cost, -, sizeof(cost));
for(int i = ; i < n; i++)
for(int j = ; j < G[i].size(); j++)
{
int u = sccno[i], v = sccno[G[i][j]];
if(u == v) continue;
if(cost[v] == -) cost[v] = C[i][j];
else cost[v] = min(cost[v], C[i][j]);
} int ans = ;
for(int i = ; i <= scc_cnt; i++) if(cost[i] != -) ans += cost[i];
printf("%d\n", ans);
} return ;
}
代码君
HDU 3072 SCC Intelligence System的更多相关文章
- HDU 3072 Intelligence System (强连通分量)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU——T 3072 Intelligence System
http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others) Memory Limi ...
- hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Intelligence System (hdu 3072 强联通缩点+贪心)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 3072 Intelligence System(Tarjan 求连通块间最小值)
Intelligence System Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
- HDU——3072 Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU - 3072 Intelligence System
题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...
随机推荐
- nodejs 实践:express 最佳实践(八) egg.js 框架的优缺点
nodejs 实践:express 最佳实践(八) egg.js 框架的优缺点 优点 所有的 web开发的点都考虑到了 agent 很有特色 文件夹规划到位 扩展能力优秀 缺点 最大的问题在于: 使用 ...
- Jquery3
动画 动画动画效果一:show(时间),hide(时间)说明:时间的单位为毫秒方法toggle(时间):使用动画切换显示与隐藏动画效果二:slideDown(时间),slideUp(时间)切换:sli ...
- Spring Bean的一生
Spring Bean的一生 When you work directly in Java, you can do anything you like with your objects and do ...
- 牛客NOIP提高组(二)题解
心路历程 预计得分:100 + 40 + 30 = 170 实际得分:100 + 30 + 0 = 130 T2有一个部分分的数组没开够RE了. T3好像是思路有点小问题.. 思路没问题,实现的时候一 ...
- ES6学习(2)
Set和Map数据结构 ES6 提供了新的数据结构 Set.它类似于数组,但是成员的值都是唯一的,没有重复的值. Set 本身是一个构造函数,用来生成 Set 数据结构. const s = new ...
- 在Office 365 添加就地保留用户邮箱
基于客户需求,要求将用户批量添加到Office 365中的现有就地保留.如您所了解的,我们可以通过Exchange在线图形用户GUI界面完成,也可以通过PowerShell完成. 要将用户批量添加到O ...
- javascript 和Jquery 互转
jQuery对象转成DOM对象: 两种转换方式将一个jQuery对象转换成DOM对象:[index]和.get(index); (1)jQuery对象是一个数据对象,可以通过[index]的方法,来得 ...
- 转载《五大免费采集器哪个好,火车头,海纳,ET,三人行,狂人采集 》
在目前的站长圈内,比较流行的采集工具有很多,但是总结起来,比较出名的免费的就这么几个:火车头,海纳,ET,三人行,狂人. 下面我们对这几款采集工具作一个简单的评比. 1.火车头 基本上人人都知道,那就 ...
- userBean之设置属性
package com.java.model; public class Student { private String name;private int age; public String ge ...
- 关于“为何Unicode中文字符占取2个字节,而 UTF-8却占3个字节”的网络解释修正
学到编码时,有个疑问——好好的占2字节的Unicode不用,却要用占3字节的UTF-8编码.发明 UTF-8的初衷不就是为了修正Unicode中任何字符至少占用2个字节的弊端吗? 虽然UTF-8英文字 ...