HDU——3072 Intelligence System
Intelligence System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2909 Accepted Submission(s): 1259
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!
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.
Believe kzc_tc’s working! There always is a way for him to communicate with all other intelligence personnel.
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
100
50
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 300010
using namespace std;
bool vis[N];
int n,m,x,y,z,s,tot,num,sum,ans,top,tim;
int fa[N],low[N],dfn[N],stack[N],head[N],belong[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int to,dis,next;
}edge[N];
struct Edde
{
int x,y,z;
}edde[N];
int add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].dis=z;
edge[tot].next=head[x];
head[x]=tot;
}
int add1(int x,int y,int z)
{
s++;
edde[s].x=x;
edde[s].y=y;
edde[s].z=z;
}
void begin()
{
s=,tim=,sum=,tot=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
memset(belong,,sizeof(belong));
}
void tarjan(int now)
{
dfn[now]=low[now]=++tim;
stack[++top]=now; vis[now]=true;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
if(vis[t]) low[now]=min(low[now],dfn[t]);
else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
}
if(low[now]==dfn[now])
{
sum++;belong[now]=sum;
for(;stack[top]!=now;top--)
{
int x=stack[top];
belong[x]=sum;vis[x]=false;
}
vis[now]=false;top--;
}
}
void shrink_point()
{
;i<=n;i++)
{
for(int j=head[i];j;j=edge[j].next)
if(belong[i]!=belong[edge[j].to])
{
int t=edge[j].to;
add1(belong[i],belong[t],edge[j].dis);
add1(belong[t],belong[i],edge[j].dis);
}
}
}
int cmp(Edde a,Edde b)
{
return a.z<b.z;
}
int find(int x)
{
if(x==fa[x]) return x;
fa[x]=find(fa[x]);
return fa[x];
}
void kruskal()
{
ans=,num=;
;i<=n;i++) fa[i]=i;
sort(edde+,edde++s,cmp);
;i<=s;i++)
{
x=edde[i].x,y=edde[i].y;
int fx=find(x),fy=find(y);
if(fx==fy) continue;
fa[fx]=fy;num++;
ans+=edde[i].z;
) break;
}
printf("%lld\n",ans);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
begin();
;i<=m;i++)
x=read(),y=read(),z=read(),add(x+,y+,z);
;i<=n;i++)
if(!dfn[i]) tarjan(i);
shrink_point();
kruskal();
}
;
}
tarjan+最小生成树
wa、、、、
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define N 300010
#define maxn 0x7fffffff
using namespace std;
bool vis[N];
long long ans;
int n,m,x,y,z,s,tot,num,sum,top,tim;
int fa[N],low[N],dfn[N],cost[N],stack[N],head[N],belong[N];
int read()
{
,f=; char ch=getchar();
; ch=getchar();}
+ch-'; ch=getchar();}
return x*f;
}
struct Edge
{
int to,dis,next;
}edge[N];
struct Edde
{
int to,dis,next;
}edde[N];
int add(int x,int y,int z)
{
tot++;
edge[tot].to=y;
edge[tot].dis=z;
edge[tot].next=head[x];
head[x]=tot;
}
void begin()
{
s=,tim=,sum=,tot=;
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(vis,,sizeof(vis));
memset(head,,sizeof(head));
memset(belong,,sizeof(belong));
}
void tarjan(int now)
{
dfn[now]=low[now]=++tim;
stack[++top]=now; vis[now]=true;
for(int i=head[now];i;i=edge[i].next)
{
int t=edge[i].to;
if(vis[t]) low[now]=min(low[now],dfn[t]);
else if(!dfn[t]) tarjan(t),low[now]=min(low[now],low[t]);
}
if(low[now]==dfn[now])
{
sum++;belong[now]=sum;
for(;stack[top]!=now;top--)
{
int x=stack[top];
belong[x]=sum;vis[x]=false;
}
vis[now]=false;top--;
}
}
void work()
{
ans=;
;i<=n;i++)
for(int j=head[i];j;j=edge[j].next)
{
int t=edge[j].to;
if(belong[i]!=belong[t])
cost[belong[t]]=min(cost[belong[t]],edge[j].dis);
}
;i<=sum;i++)
if(cost[i]!=maxn) ans+=(long long)cost[i];
printf("%lld\n",ans);
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
begin();
;i<=n;i++) cost[i]=maxn;
;i<=m;i++)
x=read(),y=read(),z=read(),add(x+,y+,z);
;i<=n;i++)
if(!dfn[i]) tarjan(i);
work();
}
;
}
HDU——3072 Intelligence System的更多相关文章
- HDU 3072 Intelligence System (强连通分量)
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
题意: 给出一个N个节点的有向图.图中任意两点进行通信的代价为路径上的边权和.如果两个点能互相到达那么代价为0.问从点0开始向其余所有点通信的最小代价和.保证能向所有点通信. 题解: 求出所有的强连通 ...
- 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 ...
- Intelligence System
Intelligence System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 3072 SCC Intelligence System
给出一个带权有向图,要使整个图连通.SCC中的点之间花费为0,所以就先缩点,然后缩点后两点之间的权值为最小边的权值,把这些权值累加起来就是答案. #include <iostream> # ...
随机推荐
- robotframework + python2.7.9 + selenium 2.44.0 + selenium2library1.7 测试环境搭建成功!
真心不容易呀!开源软件搭建挺麻烦的,各种组件未必要使用最新的版本:有些最新版本反而不兼容.需要仔细看官方说明书来进行搭建(官方网站都是英文),所以闹得重新安装了几次. 先上测试用例通过的图:
- jQuery核心语法
.each只是处理jQuery对象的方法,jQuery还提供了一个通用的jQuery.each方法,用来处理对象和数组的遍历 jQuery/($).each(array, callback )jQue ...
- Common.Logging.dll----------配置方式,可选引用,用于日志输出
1.简介common logging是一个通用日志接口,log4net是一个具体实现. common logging可以把输出连接到其他非log类上, 如EntLib的日志.NLog等 2.使用接下来 ...
- jQuery 小实例 关于按字母排序
jQuery的强大再次不再赘述 一般情况下操作表格式数据的一种最常见的任务就是排序,在一个大型的表格中,能够对要寻找的信息进行重新排列是非常重要的,一般情况用来完成排序的方式有两种 :一种是服务器端排 ...
- 浅谈2015新版 U-Boot
过了挺长一断时间没有看U-BOOT了,这两天下载了新版的UBOOT源码(之前看的一些书都是基于早好多年的源码来讲述,总感觉心里有点不对劲,也许是我比较喜新的原因吧,不过小弟我并没有厌旧哈),好了不多扯 ...
- iOS开发 数据缓存-数据库
iOS中数据存储方式 Plist(NSArray\NSDictionary) Preference(偏好设置\NSUserDefaults) NSCoding (NSKeyedArchiver\NSk ...
- 框架开发之Java注解的妙用
注解的好处:1.能够读懂别人写的代码,特别是框架相关的代码.2.本来可能需要很多配置文件,需要很多逻辑才能实现的内容,就可以使用一个或者多个注解来替代,这样就使得编程更加简洁,代码更加清晰.3.(重点 ...
- Java入门第39课——猜字母游戏之实现字母生成方法
问题 实现猜字母游戏中的字母生成方法,即,随机生成5个不同的字母作为猜测的结果. 方案 实现generate方法,首先声明一个字符类型的数组,用于存储26个大写字母,然后声 ...
- 15数据库与ADO.Net
数据库与ADO.Net 数据库与ADO.Net 8.1 数据库基本概念 数据库提供了一种将信息集合在一起的方法.数据库应用系统主要由三部分组成:数据库管理系统(DBMS),是针对所有应用的,例如A ...
- 08C#事件
C#事件 1.2 事件 事件是C#语言内置的语法,可以定义和处理事件,为使用组件编程提供了良好的基础. 1.16.1 事件驱动 Windows操作系统把用户的动作都看作消息,C# ...