博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 1287 Networking 【最小生成树Kruskal】
阅读量:6279 次
发布时间:2019-06-22

本文共 3916 字,大约阅读时间需要 13 分钟。

Networking

Description

You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 

Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 

The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 02 31 2 372 1 171 2 683 71 2 192 3 113 1 71 3 52 3 893 1 911 2 325 71 2 52 3 72 4 84 5 113 5 101 5 64 2 120

Sample Output

0171626

 

题解

两点之间有很多条边  然而我们肯定选最小的   用Prim的话需要在输入的时候取最小的边 但用Kruskal就是一个模板的事、省力

代码

#include
#include
//EOF,NULL#include
//memset#include
//rand,srand,system,itoa(int),atoi(char[]),atof(),malloc#include
//ceil,floor,exp,log(e),log10(10),hypot(sqrt(x^2+y^2)),cbrt(sqrt(x^2+y^2+z^2))#include
//fill,reverse,next_permutation,__gcd,#include
#include
#include
#include
#include
#include
#include
//setw(set_min_width),setfill(char),setprecision(n),fixed,#include
#include
#include
#include
//INT_MAX#include
// bitset
nusing namespace std;typedef long long ll;typedef pair
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_pairconst int INF =0x3f3f3f3f;const int inf =0x3f3f3f3f;const int mod = 1e9+7;const int MAXN = 105;const int maxn = 10010;int n,m;int cnt ;int ans;int a,b,v;int pre[MAXN];struct node{ int st,ed,v; bool operator < (node b) const{ return v < b.v; }}rod[MAXN];void Init(){ ans = 0; cnt = 0; for(int i = 0 ; i < MAXN ; i++){ pre[i] = i; }}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 kruskal(){ for(int i = 0; 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(read(n) && n){ read(m) ; if(n==1) { printf("0\n"); continue; } Init(); while(m--){ read3(a,b,v); rod[cnt].st = a; rod[cnt].ed = b; rod[cnt++].v = v; } sort(rod,rod + cnt); kruskal(); print(ans); }}

 

转载于:https://www.cnblogs.com/llke/p/10780117.html

你可能感兴趣的文章
Android 密钥保护和 C/S 网络传输安全理论指南
查看>>
以太坊ERC20代币合约优化版
查看>>
Why I Began
查看>>
同一台电脑上Windows 7和Ubuntu 14.04的CPU温度和GPU温度对比
查看>>
js数组的操作
查看>>
springmvc Could not write content: No serializer
查看>>
Python系语言发展综述
查看>>
新手 开博
查看>>
借助开源工具高效完成Java应用的运行分析
查看>>
163 yum
查看>>
第三章:Shiro的配置——深入浅出学Shiro细粒度权限开发框架
查看>>
80后创业的经验谈(转,朴实但实用!推荐)
查看>>
让Windows图片查看器和windows资源管理器显示WebP格式
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
vim使用点滴
查看>>
embedded linux学习中几个需要明确的概念
查看>>
mysql常用语法
查看>>
Morris ajax
查看>>
【Docker学习笔记(四)】通过Nginx镜像快速搭建静态网站
查看>>