Network POJ - 1861kruskal最小生成树

Network POJ - 1861

Andrew is working as system administrator and is planning to establish a new network in his company. There will be N hubs in the company, they can be connected to each other using cables. Since each worker of the company must have access to the whole network, each hub must be accessible by cables from any other hub (with possibly some intermediate hubs).
Since cables of different types are available and shorter ones are cheaper, it is necessary to make such a plan of hub connection, that the maximum length of a single cable is minimal. There is another problem — not each hub can be connected to any other one because of compatibility problems and building geometry limitations. Of course, Andrew will provide you all necessary information about possible hub connections.
You are to help Andrew to find the way to connect hubs so that all above conditions are satisfied.
Input
The first line of the input contains two integer numbers: N - the number of hubs in the network (2 <= N <= 1000) and M - the number of possible hub connections (1 <= M <= 15000). All hubs are numbered from 1 to N. The following M lines contain information about possible connections - the numbers of two hubs, which can be connected and the cable length required to connect them. Length is a positive integer number that does not exceed 106. There will be no more than one way to connect two hubs. A hub cannot be connected to itself. There will always be at least one way to connect all hubs.
Output
Output first the maximum length of a single cable in your hub connection plan (the value you should minimize). Then output your plan: first output P - the number of cables used, then output P pairs of integer numbers - numbers of hubs connected by the corresponding cable. Separate numbers by spaces and/or line breaks.
Sample Input
4 6
1 2 1
1 3 1
1 4 2
2 3 1
3 4 1
2 4 1
Sample Output
1
3
1 2
1 3
3 4

kruskal求最小生成树模板题

#include <stdio.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
const int inf=0x3f3f3f3f;
#define ll long long
#define ull unsigned long long
#define mm(w,v) memset(w,v,sizeof(w))
#define f(x,y,z) for(int x=(y),_=(z);x<_;++x)
const int modn=1e9+7;
const int MAXN=1000000;
using namespace std;
ll qpow(ll a,ll b,ll c)//快速幂
{

    int r=1;
    while(b)
    {
        if(b&1) r=a*r%c;
        a=a*a%c;
        b>>=1;
    }
    return r%c;
}
struct node {
	int st,ed;
	int dis;
};
node b[16000+10];
int cnt=0;
int parent[1000+10];
int ans[15000+10][2];
int ct=0;
void init() {
	mm(parent,-1);
	cnt=0;
}
int find(int x) {
	int s=x;
	while(parent[s]>=0) {
		s=parent[s];
	}
	while(s!=x) {//路径压缩 
		int temp=parent[x];
		parent[x]=s;
		x=temp;
	}
	return s;
}
void unionset(int a,int b) {
	int an=find(a);
	int bn=find(b);
	if(an>bn) {//a合进b
		parent[bn]+=parent[an];
		parent[an]=bn; 
	}
	else{
		parent[an]+=parent[bn];
		parent[bn]=an; 
	} 
}
bool cmp(node a,node b) {
	return a.dis<b.dis;
}
void solve() {
	int n,m;	
	scanf("%d%d",&n,&m);
	init();
	f(i,0,m) {
		scanf("%d%d%d",&b[i].st,&b[i].ed,&b[i].dis);
	}
	sort(b,b+m,cmp);
	int maxn=0;
	int edgecnt=0;
	f(i,0,m) {
		if(find(b[i].st)!=find(b[i].ed)) {
			ans[ct][0]=b[i].st;
			ans[ct++][1]=b[i].ed;
			maxn=max(maxn,b[i].dis);
			unionset(b[i].st,b[i].ed);
			edgecnt++;
		}
		if(edgecnt==n-1) {
			break;
		}
	}
	printf("%d\n",maxn);
	printf("%d\n",ct);
	f(i,0,ct) {
		printf("%d %d\n",ans[i][0],ans[i][1]);
	}
}
int main(void) {
	solve();
}



已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 书香水墨 设计师:CSDN官方博客 返回首页