HDU 1233 还是畅通工程(最小生成树)
还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 41447 Accepted Submission(s): 18920
Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最小的公路总长度。
Sample Input
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
Sample Output
35
prim()
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = 105;
const int INF = 0x3f3f3f3f;
struct Edge{
int u,v,w,next;
bool operator < (const Edge &x)const
{
return w > x.w;
}
}edge[maxn*(maxn-1)];
int tot = 0,head[maxn],dis[maxn];
bool vis[maxn];
void init()
{
tot = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w)
{
edge[tot] = (Edge){u,v,w,head[u]
};
head[u] = tot++;
}
int prim()
{
int sum = 0;
priority_queue<Edge>que;
Edge p;
memset(dis,INF,sizeof(dis));
memset(vis,false,sizeof(vis));
p.v = 1;
que.push(p);
dis[1] = 0;
while (!que.empty())
{
p = que.top();
que.pop();
int u = p.v;
if (vis[u]) continue;
vis[u] = true;
sum += dis[u];
for (int i = head[u];~i;i = edge[i].next)
{
int v = edge[i].v,w = edge[i].w;
if (dis[v] > w)
{
dis[v] = edge[i].w;
p.u = u,p.v = v,p.w = w;
que.push(p);
}
}
}
return sum;
}
int main()
{
//freopen("input.txt","r",stdin);
int N;
while (~scanf("%d",&N) && N)
{
int u,v,w;
int len = N*(N - 1)/2;
init();
for (int i = 0;i < len;i++)
{
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
printf("%d\n",prim());
}
return 0;
}
Kruskal
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 105;
struct Edge{
int u,v,next;
LL w;
}edge[maxn*(maxn-1)<<1];
int tot = 0,head[maxn],fa[maxn];
void init(int N)
{
tot = 0;
memset(head,-1,sizeof(head));
for (int i = 0;i <= N;i++) fa[i] = i;
}
bool cmp(struct Edge x,struct Edge y)
{
return x.w < y.w;
}
int find(int x)
{
int r = x;
while (r != fa[r]) r = fa[r];
int i = x,j;
while (i != r)
{
j = fa[i];
fa[i] = r;
i = j;
}
return r;
}
int main()
{
//freopen("input.txt","r",stdin);
int N;
while (~scanf("%d",&N) && N)
{
LL sum = 0;
int len = N*(N-1)/2;
init(N);
for (int i = 0;i < len;i++)
{
scanf("%d%d%I64d",&edge[i].u,&edge[i].v,&edge[i].w);
}
sort(edge,edge+len,cmp);
for (int i = 0;i < len;i++)
{
int fx = find(edge[i].u),fy = find(edge[i].v);
if (fx != fy)
{
fa[fx] = fy;
sum += edge[i].w;
}
}
printf("%I64d\n",sum);
}
return 0;
}
HDU 1233 还是畅通工程(最小生成树)的更多相关文章
- hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)
还是畅通工程 Time Limit: 4000/2 ...
- HDU 1233 还是畅通工程 (最小生成树)
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1233.还是畅通工程-最小生成树(Prime)
还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- (step6.1.5)hdu 1233(还是畅通工程——最小生成树)
题目大意:输入一个整数n,表示有n个村庄,在接下来的n*(n-1)/2中,每行有3个整数beigin.end.weight,分别表示路的起始村庄,结束村庄和村庄之间的距离. 求索要修的路的最短距离 解 ...
- HDU 1233 还是畅通工程(最小生成树,prim)
题意:中文题目 思路:prim实现,因为有n*(n-1)/2条边,已经是饱和的边了,prim比较合适. (1)将点1置为浏览过,点1可以到达其他每个点,所以用low[i]数组记录下目前到达i点的最小长 ...
- HDU.1233 还是畅通工程(Prim)
HDU.1233 还是畅通工程(Prim) 题意分析 首先给出n,代表村庄的个数 然后出n*(n-1)/2个信息,每个信息包括村庄的起点,终点,距离, 要求求出最小生成树的权值之和. 注意村庄的编号从 ...
- hdu 1233 还是畅通工程 (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- HDU 1233 还是畅通工程(Kruskal算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1233 还是畅通工程 Time Limit: 4000/2000 MS (Java/Others) ...
- hdu 1233 还是畅通工程 并查集or最小生成树
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离.省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路 ...
- hdu 1233:还是畅通工程(数据结构,图,最小生成树,普里姆(Prim)算法)
还是畅通工程 Time Limit : 4000/2000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
随机推荐
- SVG坐标系统
SVG的画布.画布视区(viewBox).浏览器视窗的概念 画布 画布是绘制SVG内容的一块区域,理论上在所有维度上都是无限的.(也有人称为"SVG世界",但我觉得叫画布比较合适) ...
- 业务人员自助BI分析不够用,还要自助数据准备?
自助式BI工具,可以帮助业务人员充分了解和利用企业数据,通过可视化操作,拖拖拽拽来新建分析,生成可视化的报表,帮助企业决策.但近几年的调查研究发现,拥有强大分析策略和模型的产品,比如Tableau.q ...
- UITableView cell复用出错问题 页面滑动卡顿问题 & 各杂七杂八问题
UITableView 的cell 复用机制节省了内存,但是有时对于多变的自定义cell,重用时会出现界面出错(例如复用出错,出现cell混乱重影).滑动卡顿等问题,这里只简单敲下几点复用出错时的解决 ...
- 改变Activity启动时的默认动画
why 在开发中我们可能需要改变这一行为,一般基于2种理由: 产品的spec这么要求的: 想自己控制这个行为,因为在Android设备上可能每个的这一默认行为都不一样,厂商可以很容易修改它,从而导致a ...
- Linux crontab定时器的使用
crontab参数: -u:帮助其他用户建立或移除工作排程 -l:查阅crontab的工作内容 -r:移除所有的crontab的工作内容 -e:编辑crontab文件 每项工作有六个字段: * * * ...
- linux下安装 oracle 11g
oracle 11g安装 一.环境准备 划分区 / 15000M /tmp 4096M /boot 100M Swap 4096M /u01 剩余空间 2.更改主机名,ip地址 3.安装软件包 那么l ...
- [转]How to override HandleUnauthorizedRequest in ASP.NET Core
本文转自:http://quabr.com/40446028/how-to-override-handleunauthorizedrequest-in-asp-net-core I'm migrati ...
- 第9章 Shell基础(1)_Shell简介和脚本执行方式
1. Shell概述 1.1 Shell简介 (1)Shell是一个命令行解释器,它为用户提供了一个向Linux内核发送请求以便运行程序的界面系统级程序,用户可以用Shell来启动.挂起.停止甚至是编 ...
- 【转】虚拟机VMware与主机共享文件介绍
from: http://www.cnblogs.com/kerrycode/p/3818095.html 写的比较详细,但是vm版本较旧. 2:通过共享文件夹功能 虚拟机VMware提供了在宿主机与 ...
- 2015最流行的Android组件、工具、框架大全
Android 是目前最流行的移动操作系统之一. 随着新版本的不断发布, Android的功能也日益强大, 涌现了很多流行的应用程序, 也催生了一大批的优秀的组件. 本文试图将目前流行的组件收集起来以 ...