Intelligence System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3414    Accepted Submission(s): 1494

Problem Description
After a day, ALPCs finally complete their ultimate intelligence system, the purpose of it is of course for ACM ... ...
Now, kzc_tc, the head of the Intelligence Department (his code is once 48, but now 0), is sudden obtaining important information from one Intelligence personnel. That relates to the strategic direction and future development of the situation of ALPC. So it need for emergency notification to all Intelligence personnel, he decides to use the intelligence system (kzc_tc inform one, and the one inform other one or more, and so on. Finally the information is known to all).
We know this is a dangerous work. Each transmission of the information can only be made through a fixed approach, from a fixed person to another fixed, and cannot be exchanged, but between two persons may have more than one way for transferring. Each act of the transmission cost Ci (1 <= Ci <= 100000), the total cost of the transmission if inform some ones in our ALPC intelligence agency is their costs sum.
Something good, if two people can inform each other, directly or indirectly through someone else, then they belong to the same branch (kzc_tc is in one branch, too!). This case, it’s very easy to inform each other, so that the cost between persons in the same branch will be ignored. The number of branch in intelligence agency is no more than one hundred.
As a result of the current tensions of ALPC’s funds, kzc_tc now has all relationships in his Intelligence system, and he want to write a program to achieve the minimum cost to ensure that everyone knows this intelligence.
It's really annoying!
 
Input
There are several test cases.
In each case, the first line is an Integer N (0< N <= 50000), the number of the intelligence personnel including kzc_tc. Their code is numbered from 0 to N-1. And then M (0<= M <= 100000), the number of the transmission approach.
The next M lines, each line contains three integers, X, Y and C means person X transfer information to person Y cost C.
 
Output
The minimum total cost for inform everyone.
Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.
 
Sample Input
3 3
0 1 100
1 2 50
0 2 100
3 3
0 1 100
1 2 50
2 1 100
2 2
0 1 50
0 1 100
 
Sample Output
150
100
50
 
Source
 
Recommend
lcy   |   We have carefully selected several similar problems for you:  3069 3077 3070 3071 3073 
 
题目意思:
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点
分析:
存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点
但是需要注意贪心一下
比如1到强连通分量x
应该选择1到强连通分量x的所有点中权值最小的边
缩点和贪心之后
图变成了一个DAG图,有向无环图
然后就是求这个DAG图的最小树形图的值
这里的求发很巧妙,因为题目保证了图的连通性
所有我们只需要维护所有点的最小入边的权值就好
但是有个点是没有入边的
(从此点出发,可以到达所有点)
 
code:
#include<stdio.h>
#include<iostream>
#include<math.h>
#include<string.h>
#include<set>
#include<map>
#include<list>
#include<math.h>
#include<queue>
#include<algorithm>
using namespace std;
typedef long long LL;
#define INF 0x7fffffff
#define me(a,x) memset(a,x,sizeof(a))
int mon1[]= {,,,,,,,,,,,,};
int mon2[]= {,,,,,,,,,,,,};
int dir[][]= {{,},{,-},{,},{-,}}; int getval()
{
int ret();
char c;
while((c=getchar())==' '||c=='\n'||c=='\r');
ret=c-'';
while((c=getchar())!=' '&&c!='\n'&&c!='\r')
ret=ret*+c-'';
return ret;
} #define max_v 50005
struct node
{
int v,w;
node(int vv,int ww)
{
v=vv;
w=ww;
}
};
int dfn[max_v];
int low[max_v];
int vis[max_v];
int stk[max_v];
int color[max_v];
int a[max_v];
vector<node> G[max_v];
int n,m;
int sig,cnt,sp;
LL ans;
void init()
{
me(dfn,);
me(low,);
me(vis,);
me(stk,);
me(color,);
for(int i=;i<=n;i++)
{
G[i].clear();
a[i]=INF;
}
sig=;
cnt=;
sp=-;
ans=;
} void tarjan(int u)
{
vis[u]=;
dfn[u]=low[u]=cnt++;
stk[++sp]=u;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
if(vis[v]==)
tarjan(v);
if(vis[v]==)
low[u]=min(low[u],low[v]);
}
if(low[u]==dfn[u])//染色
{
sig++;
do
{
vis[stk[sp]]=-;
color[stk[sp]]=sig;
}while(stk[sp--]!=u);
}
} int f(int u,int x)//u点到颜色x的点中的最小的权值
{
int minv=INF;
for(int j=;j<G[u].size();j++)
{
int v=G[u][j].v;
if(color[v]==x)
{
minv=min(minv,G[u][j].w);
}
}
return minv;
}
int ff(int x,int y)//判断有没有x到y的边
{
for(int j=;j<G[x].size();j++)
{
if(G[x][j].v==y)
return ;
}
return ;
}
int main()
{
int x,y,z;
while(~scanf("%d %d",&n,&m))
{
init();
for(int i=;i<=m;i++)
{
scanf("%d %d %d",&x,&y,&z);
x++,y++;
if(ff(x,y)==)//没有x到y的边
{
G[x].push_back(node(y,z));
}else
{
if(G[x][y].w>z)//有x到y的边,但是存在权更小的边,替换
G[x].push_back(node(y,z));
}
}
for(int i=;i<=n;i++)
{
if(vis[i]==)
tarjan(i);
}
for(int i=;i<=n;i++)
{
for(int j=;j<G[i].size();j++)
{
int v=G[i][j].v;
if(color[i]!=color[v])
{
//维护每个点的最小入边,最小树形图算法
a[color[v]]=min(a[color[v]],f(i,color[v]));
}
}
}
//所有点的最小入边之和就是最小树形图的权值和(有一个点没有入边)
for(int i=;i<=sig;i++)
{
if(a[i]<INF)
ans+=a[i];
}
printf("%lld\n",ans);
}
return ;
}
/*
题目意思:
n个点,m条边的有向图,x y w表示x到y的花费是w
但是一个强连通分量内的点互相到达的花费是0
问你要到达所有点的最小花费
题目保证图是连通的,且存在一个点可以到所有点 分析:
存在强连通分量,强连通内的点互相到达花费为0,可以看成一个点
但是需要注意贪心一下
比如1到强连通分量x
应该选择1到强连通分量x的所有点中权值最小的边
缩点和贪心之后
图变成了一个DAG图,有向无环图
然后就是求这个DAG图的最小树形图的值 这里的求发很巧妙,因为题目保证了图的连通性
所有我们只需要维护所有点的最小入边的权值就好
但是有个点是没有入边的
(从此点出发,可以到达所有点) */

HDU 3072 Intelligence System(tarjan染色缩点+贪心+最小树形图)的更多相关文章

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

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

  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

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

  4. HDU - 3072 Intelligence System

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

  5. HDU——T 3072 Intelligence System

    http://acm.hdu.edu.cn/showproblem.php?pid=3072 Time Limit: 2000/1000 MS (Java/Others)    Memory Limi ...

  6. hdoj 3072 Intelligence System【求scc&&缩点】【求连通所有scc的最小花费】

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

  7. HDU 5934 Bomb(tarjan/SCC缩点)题解

    思路:建一个有向图,指向能引爆对象,把强连通分量缩成一点,只要点燃图中入度为0的点即可.因为入度为0没人能引爆,不为0可以由别人引爆. 思路很简单,但是早上写的一直错,改了半天了,推倒重来才过了... ...

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

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

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

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

随机推荐

  1. JS 相关记录(scrollTo,JSON)

    1. window.scrollTo window.scrollTo 有2种语法,比较常见的时候 window.scrollTo(x-coord,y-coord ),其中 x轴坐标与y坐标 第二种为 ...

  2. js-ES6学习笔记-Generator函数的异步应用

    1.ES6 诞生以前,异步编程的方法,大概有下面四种. 回调函数 事件监听 发布/订阅 Promise 对象 Generator 函数将 JavaScript 异步编程带入了一个全新的阶段. 2.所谓 ...

  3. 自定义适用于手机和平板电脑的 Dynamics 365(四):窗体脚本

    为 Web 应用程序中使用的窗体编写的脚本也应该适用于用于手机和平板电脑的 Dynamics 365,但存在一些差异. 通常,对于移动应用程序无效的方法不返回错误,但是它们也不返回任何值. 开发人员可 ...

  4. Python 排错UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法

    Python UnicodeEncodeError 'ascii' codec can't encode character 错误解决方法   by:授客 QQ:1033553122 错误描述: py ...

  5. Android 使用RecyclerView SnapHelper详解

    简介 RecyclerView在24.2.0版本中新增了SnapHelper这个辅助类,用于辅助RecyclerView在滚动结束时将Item对齐到某个位置.特别是列表横向滑动时,很多时候不会让列表滑 ...

  6. ionic 上拉菜单(ActionSheet)安装和iOS样式不一样

    ISO中的界面是这样的: 然而,Android中的界面是这样的: 代码如下: HTML部分: <body ng-app="starter" ng-controller=&qu ...

  7. 字符串匹配常见算法(BF,RK,KMP,BM,Sunday)

    今日了解了一下字符串匹配的各种方法. 并对sundaysearch算法实现并且单元. 字符串匹配算法,是在实际工程中经常遇到的问题,也是各大公司笔试面试的常考题目.此算法通常输入为原字符串(strin ...

  8. Node路由简单的处理

    看过node很多例子,都是将路由直接放到入口文件中处理,使得文件显得很大很乱,特别是当一个项目变大,有上百甚至上千的路由,那该怎么办? 最近在想如何将一个个的路由放到一个单独的模块中处理,比如'/us ...

  9. Oracle EBS OPM close batch

    --close_batch --created by jenrry DECLARE x_message_count NUMBER; x_message_list VARCHAR2 (4000); x_ ...

  10. JBoss EAP应用服务器部署方法和JBoss 开发JMS消息服务小例子

    一.download JBoss-EAP-6.2.0GA: http://jbossas.jboss.org/downloads JBoss Enterprise Application Platfo ...