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开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...
随机推荐
- AJPFX关于Java中运用数组的四种排序方法
JAVA中在运用数组进行排序功能时,一般有四种方法:快速排序法.冒泡法.选择排序法.插入排序法.快速排序法主要是运用了Arrays中的一个方法Arrays.sort()实现.冒泡法是运用遍历数组进行比 ...
- restframework安装及APIView分析
一.restframework的安装 方式一:pip3 install djangorestframework 方式二:pycharm图形化界面安装 方式三:pycharm命令行下安装(装在当前工程所 ...
- 1044 拦截导弹 1999年NOIP全国联赛提高组 个人博客:attack.cf
1044 拦截导弹 1999年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 某国为了防御敌 ...
- Linux 安装Memcache扩展支持
查看相关软件包 yum search memcached 安装memcache yum -y install memcachedMemcache关联php yum -y install php-pec ...
- 11gR2 如何诊断节点重启问题
本文对如何诊断11gR2 GI环境下的节点重启问题进行了一些介绍. 首先,像10g版本一样,我们首先介绍在GI中能够导致节点重启的进程.1.Ocssd.bin:这个进程的功能和10g版本的功能基本差不 ...
- UVALive 5031 Graph and Queries (Treap)
删除边的操作不容易实现,那么就先离线然后逆序来做. 逆序就变成了合并,用并存集判断连通,用Treap树来维护一个连通分量里的名次. Treap = Tree + Heap.用一个随机的优先级来平衡搜索 ...
- Classes and metaclasses
http://www.sealiesoftware.com/blog/archive/2009/04/14/objc_explain_Classes_and_metaclasses.html Obje ...
- js中替换字符串
function formatStr(str){ str=str.replace(/\r\n/ig,"<br/>"); return str; } 要注意两点: 要使用 ...
- java,求1-100以内所有偶数的和。
package study01; public class Even { public static void main(String[] args) { int sum = 0; for (int ...
- mina架构在JT/T808协议应用程序中的应用
Apache Mina Server 是一个网络通信应用框架,也就是说,它主要是对基于TCP/IP.UDP/IP协议栈的通信框架(当然,也可以提供JAVA 对象的序列化服务.虚拟机管道通信服务等),M ...