题意:

首先给你一个图,需要你求出最小生成树,首先输入n个节点,用大写字母表示各节点,接着说有几个点和它相连,然后给出节点与节点之间的权值。
拿第二个样例举例:比如有3个节点,然后接下来有3-1行表示了边的情况,拿第一行来说:
A 2 B 10 C 40
表示A有2个邻点,B和C,AB权值是10,AC权值是40。

思路:

直接套prime算法模板和kuskal算法模板,然后处理下数据就可以了。

代码:

prime:

#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f;
int road[maxn][maxn],dis[maxn];
bool vis[maxn];
int n; void prim()
{
int minn, v;
for(int i = ; i < n; i++)
{
dis[i] = road[][i];
vis[i] = false;
}
for(int i = ; i <= n; i++)//包括第一个点在内,一共要纳入n个点
{
minn = inf;
for(int j = ; j < n; j++)//每次找出未纳入顶点集与已知顶点集构成的权值最小的一条边
{
if(!vis[j] && minn > dis[j])
{
v = j;
minn = dis[j];
}
}
vis[v] = true;//把该顶点纳入已知集合
for(int j = ; j < n; j++)//更新与未纳入集合中的顶点的边的最小权值
{
if(!vis[j] && dis[j] > road[v][j])
dis[j] = road[v][j];
}
}
for(int i = ; i < n; i++)
dis[] += dis[i];
cout << dis[] << endl;
} int main()
{
int m,w;
char a[],b[];
while(cin>>n,n)
{
memset(road,,sizeof(road));
for(int i=;i<n;i++)
road[i][i]=;
for(int i=;i<n;i++)
{
scanf("%s%d",a,&m);
for(int j=;j<m;j++)
{
scanf("%s%d",b,&w);
road[a[]-'A'][b[]-'A']=road[b[]-'A'][a[]-'A']=w;
}
}
prim();
}
return ;
}

kruskal:

#include<iostream>
#include<algorithm>
using namespace std;
int p[]; struct node
{
int villagea;
int villageb;
int distance;
}roads[]; bool cmp(node a, node b)
{
return a.distance < b.distance;
} int kruskal(int x)
{
return p[x] == x ? x : p[x] = kruskal(p[x]);
} int main()
{
int n, k, distance, number, ans;
char start_village, end_village;
while(cin >> n, n)
{
ans = k = ;
for(int i = ; i < ; i++)
p[i] = i;
for(int i = ; i < n- ; i++)
{
cin >> start_village >> number;
for(int j = ; j < number; j++, k++)
{
cin >> end_village >> distance;
roads[k].villagea = start_village - 'A';
roads[k].villageb = end_village - 'A';
roads[k].distance = distance;
}
}
sort(roads, roads + k, cmp);
for(int i = ; i < k; i++)
{
int x = kruskal(roads[i].villagea);
int y = kruskal(roads[i].villageb);
if(x != y)
{
ans = ans + roads[i].distance;
p[y] = x;
}
}
cout << ans << endl;
}
return ;
}

POJ1251 Jungle Roads【最小生成树】的更多相关文章

  1. POJ1251 Jungle Roads 【最小生成树Prim】

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Des ...

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

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

  3. HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 25993   Accepted: 12181 De ...

  4. POJ1251 Jungle Roads(Kruskal)(并查集)

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 23882   Accepted: 11193 De ...

  5. POJ1251 Jungle Roads Kruskal+scanf输入小技巧

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  6. POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解

    题意: 输入n,然后接下来有n-1行表示边的加边的权值情况.如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25.求连接这棵树的最小权值. 思路: 一开始是在做莫 ...

  7. hdu 1301 Jungle Roads 最小生成树

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

  8. hdu Jungle Roads(最小生成树)

    Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of for ...

  9. hdu1301 Jungle Roads 最小生成树

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

随机推荐

  1. if --else的注意点

  2. 如何在Anaconda中实现多版本python共存

    anaconda中Python版本是3.5,因为爬虫原因,需要Python2.7版本,因此,希望能在anaconda中Python3和Python2共存. 1. 打开Anaconda Prompt,可 ...

  3. MyBatis:传参

    MyBatis从入门到放弃二:传参 前言 我们在mapper.xml写sql,如果都是一个参数,则直接配置parameterType,那实际业务开发过程中多个参数如何处理呢? 从MyBatis API ...

  4. UVALive5870-Smooth Visualization-模拟水题

    很水的模拟题,拿数组搞就好了. 注意边界的地方不要算重. #include <cstdio> #include <cstring> #include <algorithm ...

  5. HDU-1686-KMP-水题

    纯KMP #include <cstdio> #include <algorithm> #include <cstring> #include <ctype. ...

  6. Codeforces960G Bandit Blues 【斯特林数】【FFT】

    题目大意: 求满足比之前的任何数小的有A个,比之后的任何数小的有B个的长度为n的排列个数. 题目分析: 首先写出递推式,设s(n,k)表示长度为n的排列,比之前的数小的数有k个. 我们假设新加入的数为 ...

  7. POJ3268(Silver Cow Party)

    题意: 有n头牛去第x个点开party(有点高大上~),单向路,去到还得回来,问这n头牛每一头花费的总时间的最大值是多少 模板spfa: #include <iostream> #incl ...

  8. Codeforces Round #382 (Div. 2) C. Tennis Championship

    C. Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input stand ...

  9. Educational Codeforces Round 23 B. Makes And The Product

    B. Makes And The Product time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  10. Zabbix 添加对交换机端口流量超出阈值的监控

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 22 Zabbix 添加对交换机端口流量超出阈值的监控 本文主要讲解利用zabbix 添加对交换机 ...