最小生成树模板题POJ - 1287-prim+kruskal
POJ - 1287超级模板题
大概意思就是点的编号从1到N,会给你m条边,可能两个点之间有多条边这种情况,求最小生成树总长度?
这题就不解释了,总结就算,prim是类似dijkstra,从第一个点出发,每次走这个点没走过的最小边权值,这样不断找下去就可以找出,本质就是贪心算法
而kruskal是利用并查集,先按照边权值大小排序,然后从小的边开始往里面添加边,利用并查集判断是否在一个联通分量里面(就是是否相连)如果不相
连就建立边,从而建图,注意,节点编号如果是从1->n,那么相应初始化就应该从1->N;
prim版本:
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const int maxx = 1e2+;
int cost[maxx][maxx];
int mincost[maxx];
bool used[maxx];
int v;
int m,n;
int prim(){
for (int i=;i<=n;i++){
mincost[i]=INF;
used[i]=false;
}
mincost[]=;//这里的数组的编号,代表我最开始建图的点
int res=;
while(true){
int v = -;
for (int u=;u<=n;u++){ //枚举边找到这个点的下一个边权值最小的点
if (!used[u] && (v==- || mincost[u] < mincost[v])) v=u;
}
if (v == -)break;//没有的话就返回,无法建图
used[v]=true;//顶点加入
res+=mincost[v];
for (int u=;u<=n;u++){
mincost[u] = min(mincost[u],cost[v][u]);//???
}
}
return res;
}
int main(){
int tmp1,tmp2,tmp3;
while(scanf("%d",&n) && n){
scanf("%d",&m);
if (m==){
cout<<""<<endl;
continue;
}
for (int i=;i<=n;i++){
for(int j=;j<=n;j++){
if (i==j)cost[i][j]=;
else cost[i][j]=INF;
}
}
for (int i=;i<=m;i++){
scanf("%d%d%d",&tmp1,&tmp2,&tmp3);
if (tmp3<cost[tmp1][tmp2])cost[tmp1][tmp2]=cost[tmp2][tmp1]=tmp3;//更新成最短边
}
cout<<prim()<<endl;
}
return ;
}
Kruskal
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<algorithm>
using namespace std;
struct node{
int u,v,w;
}a[];
int p[];
int r[];
int n,m;
int find(int x){return p[x] == x ? x : p[x] = find(p[x]);}//找根节点
int cmp(node x,node y)
{
return x.w<y.w;//按照边权值排序
}
int Kruskal(){
int ans=;
int num=;
for (int i=; i<=n; i++)p[i]=i;
sort(a+,a++m,cmp);
for (int i=; i<=m; i++)
{
int x=find(a[i].u);//出发点的根节点
int y=find(a[i].v);//到达点的根节点
if (x!=y)//不是一个根节点
{
ans+=a[i].w;//连接
p[x]=y;//y是x的父亲节点
num++;//建立好了n-1条边
}
if (num==n-){
break;
}
}
return ans;
}
int main()
{
int tmp1,tmp2,tmp3;
while(~scanf("%d",&n))
{
if (n==)break;
scanf("%d",&m);
for (int i=; i<=m; i++)
{
scanf("%d%d%d",&a[i].u,&a[i].v,&a[i].w);
}
printf("%d\n",Kruskal());
}
return ;
}
最小生成树模板题POJ - 1287-prim+kruskal的更多相关文章
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- POJ 1258:Agri-Net Prim最小生成树模板题
Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 45050 Accepted: 18479 Descri ...
- 最小生成树算法详解(prim+kruskal)
最小生成树概念: 一个有 n 个结点的连通图的生成树是原图的极小连通子图,且包含原图中的所有 n 个结点,并且有保持图连通的最少的边. 最小生成树可以用kruskal(克鲁斯卡尔)算法或prim(普里 ...
- O - 听说下面都是裸题 (最小生成树模板题)
Economic times these days are tough, even in Byteland. To reduce the operating costs, the government ...
- RMQ 模板题 poj 3264
题目:点这里 题意:给一个长度n的数列,然后又Q个询问,问L 到R 中最大值与最小值的差. 分析:RMQ 的模板题. 代码: #include<stdio.h> #include& ...
- POJ 1789 Truck History (Kruskal最小生成树) 模板题
Description Advanced Cargo Movement, Ltd. uses trucks of different types. Some trucks are used for v ...
- POJ 1287 Networking (最小生成树模板题)
Description You are assigned to design network connections between certain points in a wide area. Yo ...
- POJ 1789 Truck History【最小生成树模板题Kruscal】
题目链接:http://poj.org/problem?id=1789 大意: 不同字符串相同位置上不同字符的数目和是它们之间的差距.求衍生出全部字符串的最小差距. #include<stdio ...
- POJ1258:Agri-Net(最小生成树模板题)
http://poj.org/problem?id=1258 Description Farmer John has been elected mayor of his town! One of hi ...
随机推荐
- 用Python实现数据结构之链表
链表 链表与栈,队列不一样,它是由一个个节点构成的,每个节点存储着本身的一些信息,也存储着其他一个或多个节点的引用,可以从一个节点找到其他的节点,节点与节点之间就像是有链连在一起一样,这种数据结构就叫 ...
- 免费ARP
1. 免费ARP基本概念 免费ARP,也叫Gratutious ARP.无故ARP.这种ARP不同于一般的ARP请求,它的Sender IP和Target IP字段是相同的,相当于是请求自己的IP地址 ...
- Business talking in English
Talking one: A: Microsoft, this is Steve. B: Hi Steve, this is Richard from Third Hand Testing. I am ...
- Conjob For hybris
1.Defining the Job 写conjob的逻辑:core包下当做service层(要继承AbstractJobPerformable<CronJobModel>) public ...
- February 1st, 2018 Week 5th Thursday
The world is a fine place, and worth fighting for. 这世界是个好地方,值得为之奋斗. The world is beautiful, there ar ...
- [ISE 14.7]Fail to Link the designer导致无法仿真问题
一.当前配置 操作系统:WIN 8.1 64位 软件:Xilinx ISE 14.7 二.解决方法 首先,似乎64位的binary都有些问题,所以先把ISE Design Suite 14.7这个快捷 ...
- 关于前缀和,A - Hamming Distance Sum
前缀和思想 Genos needs your help. He was asked to solve the following programming problem by Saitama: The ...
- [MySQL学习]STRICT_ALL_TABLES相应的OUT of RANGE VALUE FOR COLUMN和DATA truncated FOR COLUMN
版权声明:声明:本文档能够转载,须署名原作者. 作者:无为 qq:490073687 周祥兴 zhou.xiangxing210@163.com https://blog.csdn.net/Rooki ...
- Problem UVA1572-Self-Assembly(拓扑排序)
Problem UVA1572-Self-Assembly Accept: 196 Submit: 1152 Time Limit: 3000 mSec Problem Description Au ...
- Domain Adaptation (1)选题讲解
1 所选论文 论文题目: <Unsupervised Domain Adaptation with Residual Transfer Networks> 论文信息: NIPS2016, ...