题解

这是一道裸的最小生成树题,拿来练手,题目就不放了

个人理解  Prim有些类似最短路和贪心,不断找距当前点最小距离的点

Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一颗树

AC代码:

Prim算法:

#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
using namespace std; typedef long long ll;
typedef pair<int,int> P;
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+;
const int MAXN = ;
const int maxn = ;
int n,m,v;
int pos,imin ;
int ans ;
char a,b ;
int vis[MAXN],dis[MAXN];
int mapp[MAXN][MAXN];
void Init(){
mst(vis,);
ans = ;
for(int i = ;i < n; i++) dis[i] = inf;
for(int i = ;i < n; i++)
for(int j = ; j < n; j++){
if(i == j) mapp[i][j] = ;
else mapp[i][j] = inf;
}
}
void prim(){
for(int i = ; i < n ; i++)
dis[i] = mapp[][i];
dis[] = ;
vis[] = ;
for(int i = ; i < n ; i ++) {
pos = ;
imin = inf;
for(int j = ; j < n ; j++ )
if(!vis[j] && dis[j] < imin) pos = j , imin = dis[j];
vis[pos] = ;
ans += imin ;
for(int j = ; j < n; j++)
if(!vis[j] && mapp[pos][j] < dis[j]) dis[j] = mapp[pos][j];
}
}
int main(){
while(cin >> n && n){
Init();
for(int i = ; i < n; i++){
cin >> a >> m ;
int st = a -'A';
while(m--) {
cin >> b >> v ;
int ed = b - 'A';
mapp[st][ed] = v;
mapp[ed][st] = v;
}
}
prim();
print(ans);
}
return ;
}

Kruskal算法:

#include<iostream>
#include<cstdio> //EOF,NULL
#include<cstring> //memset
#include<cstdlib> //rand,srand,system,itoa(int),atoi(char[]),atof(),malloc
#include<cmath> //ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))
#include<algorithm> //fill,reverse,next_permutation,__gcd,
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<utility>
#include<iterator>
#include<iomanip> //setw(set_min_width),setfill(char),setprecision(n),fixed,
#include<functional>
#include<map>
#include<set>
#include<limits.h> //INT_MAX
#include<bitset> // bitset<?> n
using namespace std; typedef long long ll;
typedef pair<int,int> P;
#define all(x) x.begin(),x.end()
#define readc(x) scanf("%c",&x)
#define read(x) scanf("%d",&x)
#define read2(x,y) scanf("%d%d",&x,&y)
#define read3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define print(x) printf("%d\n",x)
#define mst(a,b) memset(a,b,sizeof(a))
#define lowbit(x) x&-x
#define lson(x) x<<1
#define rson(x) x<<1|1
#define pb push_back
#define mp make_pair
const int INF =0x3f3f3f3f;
const int inf =0x3f3f3f3f;
const int mod = 1e9+;
const int MAXN = ;
const int maxn = ; struct node{
int st,ed,v;
bool operator < (node b) const{
return v < b.v;
}
}rod[MAXN];
int n,m;
int cnt,ans;
int pre[MAXN]; int find(int x){ return x == pre[x] ? x : pre[x] = find(pre[x]);}
bool join(int x,int y){
if(find(x)!=find(y)){
pre[find(y)] = find(x);
return true;
}
return false;
}
void Init(){
ans = ;
cnt = ;
for(int i = ; i < MAXN ; i++){
pre[i] = i;
}
}
void kruskal(){
for(int i = ;i < cnt ; i++){
int mp1 = find(rod[i].st);
int mp2 = find(rod[i].ed);
if(join(mp1,mp2)) ans+= rod[i].v;
}
}
int main(){
while(cin >> n && n){
Init();
char a,b ;
int m,v;
for(int i = ; i < n; i++){
cin >> a >> m ;
int st = a -'A';
while(m--) {
cin >> b >> v ;
int ed = b - 'A';
rod[cnt].st = st;
rod[cnt].ed = ed;
rod[cnt++].v = v;
}
}
sort(rod,rod+cnt);
kruskal();
print(ans);
}
return ;
}

POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】的更多相关文章

  1. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  2. POJ 1251 & HDU 1301 Jungle Roads

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  3. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  4. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  5. Hdu 1301 Jungle Roads (最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...

  6. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  7. 最小生成树 || HDU 1301 Jungle Roads

    裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...

  8. hdu 1301 Jungle Roads

    http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

  9. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

随机推荐

  1. PM2管理工具的使用

    linux上PM2可以管理服务程序,防止程序无故关闭,具有程序守护功能,自动重启服务器程序,监控程序等好处,很方便,具体自己去体会! 官网地址:  http://pm2.keymetrics.io/ ...

  2. STL之Vector容器

    1.Vector容器 1)vector是将元素置于一个动态数组中加以管理的容器. 2)vector可以随机存取元素(支持索引值直接存取, 用[]操作符或at()方法,这个等下会详讲). 3)vecto ...

  3. 记一次CentOS5.7更新glibc导致libc.so.6失效,系统无法启动

      以下是错误示范,错误过程还原,请勿模仿!!! wkhtmltopdf 启动,提示/lib64/libc.so.6版本过低 $ ./wkhtmltopdf http:www.baidu.com 1. ...

  4. 列表选择模式:单选、按shift、按shift或ctrl

    2018-10-29 21:23:16 开始写 import java.awt.BorderLayout; import java.awt.EventQueue; import javax.swing ...

  5. First Wainberg-2018-Deep learning in biomedicine Experience

    ppt+paper 链接:https://pan.baidu.com/s/14toqjcSJti5ZXT3ff4rwIA 提取码:xgkt

  6. 怎么查 ODBC Driver for SQL Server

    1)进入服务器,找到SQL Server 2016 Configuration... ,点进去就好了 2)

  7. mongodb可视化工具 studio3t robo3T 下载安装使用介绍

    mongodb可视化工具 studio3t  robo3T 下载安装使用介绍 下载地址: https://studio3t.com/download robo3T

  8. Top 5 Reasons to Get BMW ICOM A2 with Latest Software

    Top 5 Reasons to Get BMW ICOM A2 with Latest Software 1.BMW ICOM A2 Hardware Version: V2018.03 2.Sup ...

  9. (2018干货系列八)最新VR学习路线整合

    怎么学VR 即虚拟现实技术,是一种可以创建和体验虚拟世界的计算机仿真系统,它利用计算机生成一种模拟环境,是一种多源信息融合的.交互式的三维动态视景和实体行为的系统仿真使用户沉浸到该环境中.VR/AR/ ...

  10. Django框架----Ajax

    一.Ajax准备知识:json 说起json,我们大家都了解,就是python中的json模块,那么json模块具体是什么呢?那我们现在详细的来说明一下 1.json(Javascript  Obie ...