题意,从0点出发,遍历所有点,遍历边时候要付出代价,在一个SCC中的边不要付费。求最小费用。

有向图缩点(无需建立新图,,n《=50000,建则超时),遍历边,若不在一个SCC中,用一个数组更新记录最小到达该连通分量的最小边权即可。。。边聊天,边1A,哈哈。。。

#include<iostream>
#include<stack>
#include<queue>
#include<cstdio>
#include<cstring>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxv=50005,maxe=100005;
int nume=0;int head[maxv];int e[maxe][3];
void inline adde(int i,int j,int c)
{
e[nume][0]=j;e[nume][1]=head[i];head[i]=nume;
e[nume++][2]=c;
}
int dfn[maxv];int low[maxv];int vis[maxv];int ins[maxv]; stack<int>sta;
int scc[maxv];int numb=0;int times=0;
int n,m;
void tarjan(int u)
{
dfn[u]=low[u]=times++;
ins[u]=1;
sta.push(u);
for(int i=head[u];i!=-1;i=e[i][1])
{
int v=e[i][0];
if(!vis[v])
{
vis[v]=1;
tarjan(v);
if(low[v]<low[u])low[u]=low[v];
}
else if(ins[v]&&dfn[v]<low[u])
{
low[u]=dfn[v];
}
}
if(low[u]==dfn[u])
{
numb++;
int cur;
do
{
cur=sta.top();
sta.pop();
ins[cur]=0;
scc[cur]=numb;
}while(cur!=u);
}
}
int mincost_to_v[maxv]; //记录
void solve()
{
vis[0]=1;
tarjan(0);
for(int i=0;i<n;i++)
for(int j=head[i];j!=-1;j=e[j][1])
{
int v=e[j][0];
if(scc[i]!=scc[v])
{
if(e[j][2]<mincost_to_v[scc[v]])
{
mincost_to_v[scc[v]]=e[j][2];
}
}
}
int sums=0;
for(int i=1;i<=numb;i++)
{
if(mincost_to_v[i]!=inf) //起点
sums+=mincost_to_v[i];
}
printf("%d\n",sums);
}
void read_build()
{
int aa,bb,cc;
for(int i=0;i<m;i++)
{
scanf("%d%d%d",&aa,&bb,&cc);
adde(aa,bb,cc);
}
}
void init()
{
numb=times=nume=0;
for(int i=0;i<maxv;i++)
{
head[i]=-1;
ins[i]=dfn[i]=low[i]=scc[i]=vis[i]=0;
mincost_to_v[i]=inf;
}
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
init();
read_build();
solve();
}
return 0;
}

hdu 3072 有向图缩点成最小树形图计算最小权的更多相关文章

  1. hdu 1827 有向图缩点看度数

    题意:给一个有向图,选最少的点(同时最小价值),从这些点出发可以遍历所有. 思路:先有向图缩点,成有向树,找入度为0的点即可. 下面给出有向图缩点方法: 用一个数组SCC记录即可,重新编号,1.... ...

  2. hdu 3639 有向图缩点+建反向图+搜索

    题意:给个有向图,每个人可以投票(可以投很多人,一次一票),但是一个人只能支持一人一次,支持可以传递,自己支持自己不算,被投支持最多的人. 开始想到缩点,然后搜索,问题是有一点想错了!以为支持按票数计 ...

  3. hdu 3072 强连通+缩点+最小树形图思想

    #include<stdio.h> #include<string.h> #define N 51000 #define inf 1000000000 struct node ...

  4. hdu 2121 Ice_cream’s world II (无定根最小树形图)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2121 题目大意: 有n个点,有m条单向路,问这n个点组成最小树形图的最小花费. 解题思路: 1:构造 ...

  5. poj3164最小树形图模板题

    题目大意:给定一个有向图,根节点已知,求该有向图的最小树形图.最小树形图即有向图的最小生成树,定义为:选择一些边,使得根节点能够到达图中所有的节点,并使得选出的边的边权和最小. 题目算法:朱-刘算法( ...

  6. CF240E Road Repairs(最小树形图-记录路径)

    A country named Berland has n cities. They are numbered with integers from 1 to n. City with index 1 ...

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

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

  8. HDU 4966 GGS-DDU(最小树形图)

    n个技能,每个技能有0-a[i]的等级,m个课程,每个课程需要前置技能c[i]至少达到lv1[i]等级,效果是技能d[i]达到lv2[i]等级,花费w[i]. 输出最小花费使得全技能满级(初始全技能0 ...

  9. HDU 2121 Ice_cream’s world II 不定根最小树形图

    题目链接: 题目 Ice_cream's world II Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

随机推荐

  1. 第1-5章 慕课网微信小程序开发学习笔记

    第1章 前言:不同的时代,不同的Web --微信小程序商城构建全栈应用 http://note.youdao.com/noteshare?id=a0e9b058853dbccf886c1a890594 ...

  2. VirtualBox下vim无法正常使用问题解决

    由原来的使用VMware转到使用Virtual Box,发现其vim编辑器不是特别好用,需要进行一下更改设置: 1.使用命令删除vim,sudo apt-get remove vim-common 2 ...

  3. I2C总线协议图解(转载)

    转自:http://blog.csdn.net/w89436838/article/details/38660631 另外,https://blog.csdn.net/qq_38410730/arti ...

  4. Disharmony Trees HDU - 3015

    Disharmony Trees HDU - 3015 One day Sophia finds a very big square. There are n trees in the square. ...

  5. 动态规划:HDU1176-免费馅饼

    免费馅饼 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submi ...

  6. DFS:BZOJ1085-骑士精神

    题目: 1085: [SCOI2005]骑士精神 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 1461  Solved: 796 [Submit][ ...

  7. Storm: 遇到问题总结

    1.没有ack : kafkaspout id 重复导致每次读最新没有数据. 2.由于storm提供的读取kafka的enternal工具存在bug,导致重复读取数据,致使数据不准确.storm bu ...

  8. HDU 3848 CC On The Tree 树形DP

    题意: 给出一棵边带权的树,求距离最近的一对叶子. 分析: 通过DFS计算出\(min(u)\):以\(u\)为根的子树中最近叶子到\(u\)的距离. 然后维护一个前面子树\(v_i\)中叶子到\(u ...

  9. cf936c Lock Puzzle

    ref #include <algorithm> #include <iostream> #include <cstring> #include <cstdi ...

  10. 码农与UI沟通的日常

    事情是这样的,这是一个兴趣群组的效果图. 我看了一眼没有帖子时的提示,觉得这样的提示 不走心 不能展现出我们团队对于人生及世界的深度理解和高尚的品格. 于是,我选择了表达内心的真实感受. 我觉得这完美 ...