强连通分量——tarjin 算法

这道题和前面那道hdu 2767唯一不同就是,2767需要找出最小数量的边使图成为连通分量,而这个题需要一点点贪心的思想在里面,它需要求出代价最小的边使图成为连通分量;

代码:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
#define N 50006
using namespace std; struct Edge
{
int u, val, next;
Edge() {}
Edge(int a, int b, int c)
{
u=a, val=b, next=c;
}
} edge[]; int head[N],tot,n,m,dfn[N],low[N],T,ind,id[N],in[N];
bool vs[N];
stack<int> S; void add_edge(int st, int en, int val)
{
edge[tot]=Edge(en,val,head[st]);
head[st]=tot++;
} void tarjan(int u)
{
S.push(u), vs[u]=true;
dfn[u]=low[u]=T++;
for(int e=head[u]; e!=-; e=edge[e].next)
{
int v=edge[e].u;
if(!dfn[v])
{
tarjan(v);
low[u]=min(low[u], low[v]);
}
else if(vs[v] && low[u]>dfn[v]) low[u]=dfn[v];
}
if(low[u]==dfn[u])
{
ind++;
int v;
do
{
v=S.top();
S.pop();
id[v]=ind;
vs[v]=false;
}while(v!=u);
}
} int main()
{
while(scanf("%d%d", &n, &m)!=EOF)
{
memset(head, -, sizeof head);
tot=;
for(int i=, a, b, c; i<m; i++)
{
scanf("%d%d%d", &a, &b, &c);
add_edge(a,b,c);
}
while(!S.empty()) S.pop();
memset(vs, , sizeof vs);
memset(dfn,, sizeof dfn);
memset(low,,sizeof low);
T=ind=;
for(int i=; i<n; i++) if(!dfn[i]) tarjan(i);
for(int i=; i<ind; i++) in[i]=;
for(int i=; i<n; i++)
{
int u=id[i];
for(int e=head[i]; e!=-; e=edge[e].next)
{
int v=id[edge[e].u];
if(u!=v) in[v]=min(in[v], edge[e].val);
}
}
int ans=;
for(int i=; i<ind; i++)
{
if(i==id[]||in[i]==) continue;
ans+=in[i];
}
printf("%d\n", ans);
}
return ;
}

hdu 3072的更多相关文章

  1. HDU 3072 (强连通分量)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3072 题目大意:为一个有向连通图加边.使得整个图全连通,有重边出现. 解题思路: 先用Tarjan把 ...

  2. HDU 3072 Intelligence System (强连通分量)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  3. HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  4. hdu 3072 Intelligence System(Tarjan 求连通块间最小值)

    Intelligence System Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) ...

  5. Intelligence System (hdu 3072 强联通缩点+贪心)

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

  6. HDU - 3072 Intelligence System

    题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...

  7. hdu 3072 有向图缩点成最小树形图计算最小权

    题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费.求最小费用. 有向图缩点(无需建立新图,,n<=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新 ...

  8. HDU 3072 SCC Intelligence System

    给出一个带权有向图,要使整个图连通.SCC中的点之间花费为0,所以就先缩点,然后缩点后两点之间的权值为最小边的权值,把这些权值累加起来就是答案. #include <iostream> # ...

  9. HDU——3072 Intelligence System

    Intelligence System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Othe ...

随机推荐

  1. Android(java)学习笔记153:layout_weight使用注意事项

    1. android:layout_weight使用说明: layout_weight是权重的意思,也就是各个控件所占的比重,用在LinearLayout布局中.当我们使用layout_weight的 ...

  2. [翻译]Python with 语句

    With语句是什么? Python's with statement provides a very convenient way of dealing with the situation wher ...

  3. 解决 kindle 书籍字体颜色偏淡问题的方法

    现象 通过Markdown转换而来的mobi格式书籍都有一个大问题:字体偏淡,放在kindle上看对比度很差. 原因分析: 导致这种问题的原因,可能是因为在制作电子书的过程中,这些内容是被标注了彩色或 ...

  4. 华为RH8100V3RAID 10配置

    a)华为RH8100V3RAID 10配置 1)开机按照提示按Ctrl+H键进入RAID卡WEBBIOS管理界面: 2)选中“Start”回车,进入RAID卡管理配置界面: 3)移动鼠标到 “conf ...

  5. Java并发——同步工具类

    CountDownLatch  同步倒数计数器 CountDownLatch是一个同步倒数计数器.CountDownLatch允许一个或多个线程等待其他线程完成操作. CountDownLatch对象 ...

  6. spring mvc 3.1的自动注入参数遇到的问题

    在网上下载了xheditor作为页面的编辑器,编辑内容后post到后台保存,后台方法用spring mvc的自动注入的方式接收参数. 这种方式在各个浏览器下运行良好,但是在ie11下发现,从word. ...

  7. C#Combobox显示名称保存代码

    private void Form1_Load(object sender, EventArgs e) { DataTable dt = new DataTable(); dt.Columns.Add ...

  8. Log4Net 在多层项目中的使用小记

    原文地址:http://www.cnblogs.com/zdh8675/p/3645556.html 这几天刚好在调整一个项目,把一些自己不是很清楚的东西先试验一下,这篇文章主要是对我在项目中需要使用 ...

  9. Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB

    Implicit conversion from enumeration type 'enum CGImageAlphaInfo' to different enumeration type 'CGB ...

  10. [转] jQuery按键响应事件keypress对应的按键编码keycode

    原文地址:http://blog.csdn.net/chenhj1988918/article/details/7534922 keypress  api 文档:http://api.jquery.c ...