Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another.
Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this
problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
这道题我第一感觉是求最短路,原谅我最近做这个有点多,结果用了Floyd之后发现结果不多,最后发现原来是最小生成树,用kruskal,prim其实也可以,不过还是kruskal写的顺手,所以就写了,没啥好说的。不过用dijkstra也可以,可能是各个最短路算法之间的区别导致的,有空再研究一下。
AC代码:
/////////////////////////////////////////////////////////
// // //
// Created by Team 3 //
// Copyright (c) 2015年 Team 3. All rights reserved. //
/////////////////////////////////////////////////////////
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <cctype>
#include <stack>
#include <queue>
#include <map>
#include <string>
#include <set>
#include <vector>
#define INF 0x3f3f3f3f
#define cir(i,a,b) for (int i=a;i<=b;i++)
#define CIR(j,a,b) for (int j=a;j>=b;j--)
#define CLR(x) memset(x,0,sizeof(x))
typedef long long ll;
using namespace std;
#define maxn 150
struct Edge
{
int u,v,cost;
}es[maxn*maxn];
int par[maxn*maxn],rank[maxn*maxn];
int n;
void init(int n) //初始化
{
for (int i=0;i<=n;i++)
{
par[i]=i;
rank[i]=0;
}
}
int find(int x)
{
if (par[x]==x) return x;
else return par[x]=find(par[x]);
}
bool cmp(const Edge &a,const Edge &b)
{
return a.cost < b.cost;
}
bool same(int x,int y)
{
return find(x)==find(y);
}
void unite(int x,int y)
{
x=find(x);y=find(y);
if (x==y) return ; //是同一个集合
if (rank[x]<rank[y]) par[x]=y;
else
{
par[y]=x;
if (rank[x]==rank[y])
rank[x]++; //同样的深度则在增减一个父节点
}
}
int kruskal()
{
int res=0;
init(n*n+1);
sort(es+1,es+1+n*n,cmp);
for (int i=1;i<=n*n;i++)
{
Edge e=es[i];
if (!same(e.u,e.v))
{
unite(e.u,e.v);
res+=e.cost;
}
}
return res;
}
int main()
{
while (scanf("%d",&n)!=EOF)
{
int tot=1;
for (int i=1;i<=n;i++)
for (int j=1;j<=n;j++)
{
int w;
scanf("%d",&w);
es[tot++]=(Edge){i,j,w};
}
cout << kruskal() << endl;
}
return 0;
}