首页 > 其他分享 >E - Networking

E - Networking

时间:2023-02-22 22:34:18浏览次数:32  
标签:given Networking int 最小 points between include

E - Networking

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 0

2 3
1 2 37
2 1 17
1 2 68

3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32

5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12

0
Output
0
17
16
26

题意

求将所有点链接起来的最小代价(求最小生成树)

最小生成树

  最小生成树(Minimum Spanning Tree,MST):或者称为最小代价树Minimum-cost Spanning Tree:对无向连通图的生成树,各边的权值总和称为生成树的权,权最小的生成树称为最小生成树。

  构成生成树的准则有三条:

  <1> 必须只使用该网络中的边来构造最小生成树(最小)。

  <2> 必须使用且仅使用n-1条边来连接网络中的n个顶点(链接所有顶点

  <3> 不能使用产生回路的边(无环)。

  构造最小生成树的算法主要有:克鲁斯卡尔(Kruskal)算法和普利姆(Prim)算法他们都遵循以上准则。

  Kruskal算法主要是利用并查集的思想来实现

  它是以边为主导地位,始终选择当前可用(所选的边不能构成回路)的最小权植边。所以Kruskal算法的第一步是给所有的边按照从小到大的顺序排序。这一步可以直接使用库函数sort。

  接下来从小到大依次考察每一条边(u,v)。每次添加选择一条具有最小权植的边时,若该边的两个顶点落在不同的连通分量上(不在一个集合中),则将此边加入到T中(并查集思想);

  否则,即这条边的两个顶点落到同一连通分量上,则将此边舍去(此后永不选用这条边),重新选择一条权植最小的边。一直重复下去知道所有的顶点在同一连通分量上面。

kruskal求最小生成树

image

代码

点击查看代码
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;

#define X first
#define Y second

typedef pair<int,int> pii;
typedef long long LL;
const char nl = '\n';
const int N = 1300;
const int M = 2e5+10;
int n,m;
int p[55];
LL ans;

typedef struct edge{
	int a,b,w;
}edge;
edge e[N];

bool cmp(edge a,edge b){
	return a.w < b.w;
}

int find(int x){
	if(p[x] != x)p[x] = find(p[x]);
	return p[x];
}

void solve(){
	while(cin >> n && n){
		cin >> m;
		if(n == 1){
			cout << 0 << nl;
			continue;
		}
		for(int i = 1; i <= n; i ++ )p[i] = i;
		ans = 0;
		for(int i = 1; i <= m; i ++ ){
			int a,b,w;
			cin >> e[i].a >> e[i].b >> e[i].w;
			//e[i] = {a,b,w};
		}
		sort(e+1,e+m+1,cmp);
		for(int i = 1; i <= m; i ++ ){
			int a = e[i].a,b = e[i].b,w = e[i].w;
			a = find(a),b = find(b);
			if(a != b){
				p[a] = b;
				ans += w;
			}
		}
		cout << ans << nl;
	}
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);

	solve();
}

标签:given,Networking,int,最小,points,between,include
From: https://www.cnblogs.com/J-12045/p/17146252.html

相关文章