Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 29545 | Accepted: 11716 |
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
Source
求无向图中遍历所有顶点的最短路径之和,生成一棵最小生成树。用Prim算法或是Kruskal算法均可(但是我还没学Kruskal...= =!)
最初时间复杂度为O(V^3)的算法
#include<iostream>
using namespace std;
#define Inf 100000000
long dis[101][101];
bool visit[101];
int i,j,k,n;
long long ans;
void Prim()
{
int flag;
long temp;
for(k=1;k<n;k++)
{
temp=Inf;
for(i=0;i<n;i++)
{
if(visit[i])
//若该顶点已加入U集中,对这一点的后继点进行搜索,找到以U集中某一顶点为起点的最短路径
{
for(j=0;j<n;j++)
{
if(i!=j&&!visit[j])
{if(temp>dis[i][j])
{
temp=dis[i][j];
flag=j;
}
}
}
}
}
visit[flag]=true;
ans+=temp;
}
}
int main()
{
while(cin>>n)
{
memset(visit,false,sizeof(visit));
memset(dis,0,sizeof(dis));
visit[0]=true;
ans=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>dis[i][j];
Prim();
cout<<ans<<endl;
}
return 0;
}
时间复杂度为(O^2)的算法
#include<iostream>
using namespace std;
#define Inf 100000000
int dis[101][101];
int lowcost[101];
bool visit[101];
int i,j,k,n;
long long ans;
void Prim()
{
long min;int flag;
for(i=1;i<n;i++)
lowcost[i]=dis[0][i];//初始化到所有顶点的最短路径为源点到这些顶点的距离
for(i=1;i<n;i++)
{
min=Inf;
for(j=0;j<n;j++)
{
if(!visit[j]&&min>lowcost[j]&&lowcost[j]!=0)
//找到V-U集中的最短路径并记下顶点
{
min=lowcost[j];
flag=j;
}
}
visit[flag]=true;//把刚才记下的顶点加入到U集中
for(j=0;j<n;j++)
//更新到顶点j的最短路径为U集中元素到j的最短路径
//注意是每加一个元素就更新一次,始终保持到j点的路径为最优路径
{
if(!visit[j]&&dis[flag][j]<lowcost[j])
lowcost[j]=dis[flag][j];
}
ans+=min;//加上本轮循环的最短路径
}
}
int main()
{
while(cin>>n)
{
memset(visit,false,sizeof(visit));
memset(dis,0,sizeof(dis));
visit[0]=true;
ans=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
cin>>dis[i][j];
Prim();
cout<<ans<<endl;
}
return 0;
}