Problem Description
The distance between any two villages is listed in the statistical table. The goal of the provincial government's "unimpeded project" is to enable road traffic between any two villages in the province (but not necessarily connected by direct roads, as long as it can be reached by indirect roads), and the total length of the roads to be paved is required to be the minimum. Please calculate the minimum total length of the road.
Input
The test input contains several test cases. The first line of each test case gives the number of villages N (< 100); the next N(N-1)/2 lines correspond to the distance between villages, and each line gives a pair of positive integers, which are the number of two villages and the distance between the two villages. For simplicity, villages are numbered from 1 to N.
When N is 0, the input ends and the use case is not processed.
Output
For each test case, output the minimum total road length in line 1.
Sample Input
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
Sample Output
3 5
Hint
Hint Huge input, scanf is recommended.
Source
Zhejiang University Computer postgraduate retest on computer examination - 2006
Recommend
JGShining
Prim algorithm:
#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 disbit(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+7; const int MAXN = 110; const int maxn = 10010; int n,m,v; int pos,imin ; int ans ; int st,ed; int vis[MAXN],dis[MAXN]; int mapp[MAXN][MAXN]; void Init(){ mst(vis,0); for(int i = 1 ;i <= n; i++) dis[i] = inf; for(int i = 1 ;i <= n; i++) for(int j = 1; j <= n; j++){ if(i == j) mapp[i][j] = 0; else mapp[i][j] = inf; } } void prim(){ for(int i = 1; i <= n ; i++) dis[i] = mapp[1][i]; dis[1] = 0; vis[1] = 1; for(int i = 1 ; i < n ; i ++) { pos = 1; imin = inf; for(int j = 1 ; j <= n ; j++ ) if(!vis[j] && dis[j] < imin) { pos = j , imin = dis[j]; } vis[pos] = 1; ans += imin ; for(int j = 1; j <= n; j++) if(!vis[j] && mapp[pos][j] < dis[j]) dis[j] = mapp[pos][j]; } } int main(){ while(read(n) && n){ m = n * (n - 1) / 2; Init(); for(int i = 0; i < m; i++){ read3(st,ed,v); mapp[st][ed] = v; mapp[ed][st] = v; } ans = 0; prim(); print(ans); } return 0; }
Kruskal algorithm:
#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+7; const int MAXN = 10005; const int maxn = 10010; struct node{ int st,ed,v; bool operator < (node b) const{ return v < b.v; } }rod[MAXN]; int n,m,v; 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[y] = x; return true; } return false; } void Init(){ ans = 0; cnt = 0; for(int i = 0 ; i < MAXN ; i++){ pre[i] = i; } } 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(cin >> n && n){ Init(); int a,b; for(int i = 0; i< n * (n - 1) / 2;i++){ cin >> a >> b >> v ; rod[cnt].st = a; rod[cnt].ed = b; rod[cnt++].v = v; } sort(rod,rod+cnt); kruskal(); cout << ans <<endl; } }