POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】
题解
这是一道裸的最小生成树题,拿来练手,题目就不放了
个人理解 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 【最小生成树】的更多相关文章
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ 1251 & HDU 1301 Jungle Roads
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- Hdu 1301 Jungle Roads (最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...
- 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 ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
随机推荐
- docker tool
安装docker tool http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 安装1.8.3 有把vbox等服务器地址加 ...
- win10系统进入BIOS
按住shift+重启,在重启过程中界面会出现“疑难解答”,点击后,在新的界面点击“高级选项”,之后在新界面上点击“UEFI固件设置”,最后点击重启,重启过程中点击Delete键,就进入了BIOS界面了 ...
- javascript(二):数据类型&数值
第一部分:数据类型 javascript数据类型通常来说是6种(ES6新增第七种Symbol类型) number:数值 string:字符串 boolean:布尔类型,true或false undef ...
- keras tensorboard的使用
http://blog.csdn.net/xiaojiajia007/article/details/72865764 https://stackoverflow.com/questions/4211 ...
- 如何注销Sitecore CMS
登录Sitecore很容易,但是在旧版本的Sitecore中使用不同的界面,退出可能会给未经证实的人带来挑战. Sitecore 8 Sitecore 6和7 Sitecore 8 Sitecore ...
- Rower Bo (高数 + 物理)
#include<bits/stdc++.h> #define esp (1e-5) using namespace std; int main(){ int a; double v1, ...
- spring部分注解
@Controller @SpringBootApplication @Configuration @ComponentScan(basePackages={"first",&qu ...
- c# 使用checked和unchecked
首先要知道int型在c#中是一个32位的数.由此可以知道int型的取值范围是(-2147483648~2147483647)当要使用int的最小值或者是最大值的时候,可以使用int.MinValue和 ...
- python smtplib 发送邮件简单介绍
SMTP(Simple Mail Transfer Protocol)即简单邮件传输协议,它是一组用于由源地址到目的地址传送邮件的规则,由它来控制信件的中转方式python的smtplib提供了一种很 ...
- C#中对Web.Config、App.Config字符串加密与解密的方法
我们平常的项目里面的配置文件通常都是明文形式的存在,现在就是为了项目安全性增强,同时又显得高逼格点, 我们可以采用加密的方式,而我们C#很强大,因为他内置的一些指令方式,很方便而且使用起来还不用解密, ...