原题:
Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 51679 | Accepted: 21556 |
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
题意:
给出结点之间的边长,求最短路。
思路:
第一次做最小生成树的题,用的是并查集。这么写也是为了巩固前面的一些知识,连快排都是自己写的。。
并查集的做法是:将所有的边从小到大排序,然后每次取最短的边,假如这次取得边的两个顶点没有连通,就选用这条边,否则不选用。最后所有被选取的边构成最小生成树。
用结构体存储边,用并查集判断两个顶点是否已经连通。
代码:
#include <iostream>
using namespace std;
struct node
{
int x, y, w;
}edge[5100];
void quicksort(int left, int right)
{
if(left >= right)
return;
int i, j;
i = left;
j = right;
while(i < j)
{
while(i < j && edge[j].w >= edge[left].w)
j--;
while(i < j && edge[i].w <= edge[left].w)
i++;
if(i < j)
swap(edge[i],edge[j]);
}
swap(edge[i],edge[left]);
quicksort(left,i-1);
quicksort(i+1,right);
return;
}
int tol, F[110], count, sum, map[110][110];
int bcj(int x)
{
if(F[x] == x)
return x;
else
{
F[x] = bcj(F[x]);
return F[x];
}
}
bool merge(int x,int y)
{
int t1 = bcj(x);
int t2 = bcj(y);
if(t1!=t2)
{
F[t2] = t1;
return false;
}
return true;
}
int main()
{
int n;
while(cin>>n)
{
tol = 0;
count = 0;
sum = 0;
for(int i = 0;i < n;i++)
for(int j = 0;j < n;j++)
cin>>map[i][j];
for(int i = 0;i < n;i++)
for(int j = i+1;j < n;j++)
{
edge[tol].x = i, edge[tol].y = j, edge[tol++].w = map[i][j];
}
quicksort(0,tol-1);
for(int i = 0;i < n;i++)
F[i] = i;
for(int i = 0;i < tol;i++)
{
if(!merge(edge[i].x,edge[i].y))
{
count++;
sum += edge[i].w;
}
if(count == n-1)
break;
}
cout<<sum<<endl;
}
return 0;
}