题目链接:http://poj.org/problem?id=1258
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
1 //poj1258 prim算法 2 #include <iostream> 3 #include <algorithm> 4 using namespace std; 5 const int maxn=1005; 6 const int INF=0x3f3f3f3f; 7 int cost[maxn][maxn];//表示边的权值不存在的情况下为INF 8 int mincost[maxn];//从集合x出发的边到每个顶点的最小权值 9 bool used[maxn];//顶点i是否包含在集合x中 10 int n;//顶点数 11 12 int prim() 13 { 14 for(int i=0;i<n;i++){ 15 mincost[i]=INF; 16 used[i]=false; 17 } 18 mincost[0]=0; 19 int res=0; 20 while(true){ 21 int v=-1;//从不属于x的顶点中选取从x到其权值最小的顶点 22 for(int i=0;i<n;i++){ 23 if(!used[i]&&(v==-1||mincost[i]<mincost[v])){ 24 v=i; 25 } 26 } 27 if(v==-1) break; 28 used[v]=true;//把顶点v加入x 29 res+=mincost[v];//把边的长度加到结果里 30 for(int i=0;i<n;i++){ 31 mincost[i]=min(mincost[i],cost[v][i]); 32 } 33 } 34 return res; 35 } 36 int main() 37 { 38 while(cin>>n){ 39 for(int i=0;i<n;i++){ 40 for(int j=0;j<n;j++){ 41 cin>>cost[i][j]; 42 } 43 } 44 cout<<prim()<<endl; 45 } 46 return 0; 47 }
脑子笨,就得不停地重复重复再重复
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int maxn=1005; 5 const int INF=0x3f3f3f3f; 6 int n; 7 int cost[maxn][maxn]; 8 int used[maxn]; 9 int mincost[maxn]; 10 int res; 11 void prim() 12 { 13 for(int i=0;i<n;i++){ 14 mincost[i]=INF; 15 used[i]=0; 16 } 17 mincost[0]=0; 18 res=0; 19 while(1){ 20 int v=-1; 21 for(int i=0;i<n;i++) if(!used[i]&&(v==-1||mincost[i]<mincost[v])) v=i; 22 if(v==-1) break; 23 res+=mincost[v]; 24 used[v]=1; 25 for(int i=0;i<n;i++) mincost[i]=min(mincost[i],cost[v][i]); 26 } 27 } 28 int main() 29 { 30 while(cin>>n){ 31 for(int i=0;i<n;i++) 32 for(int j=0;j<n;j++) 33 cin>>cost[i][j]; 34 prim(); 35 cout<<res<<endl; 36 } 37 return 0; 38 }
once again ...
1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 const int INF=0x3f3f3f3f; 5 const int N=105; 6 int n; 7 int a[N][N];//各点之间的距离 8 int mina[N]; 9 int used[N]; 10 int res; 11 int prim() 12 { 13 for(int i=0;i<n;i++){ 14 mina[i]=INF; 15 used[i]=0; 16 } 17 mina[0]=0; 18 res=0; 19 while(1){ 20 int v=-1; 21 for(int i=0;i<n;i++){ 22 if(!used[i]&&(v==-1||mina[i]<mina[v])) v=i; 23 } 24 if(v==-1) break; 25 used[v]=1; 26 res+=mina[v]; 27 for(int i=0;i<n;i++){ 28 mina[i]=min(mina[i],a[v][i]); 29 } 30 } 31 return res; 32 } 33 int main() 34 { 35 while(cin>>n){ 36 for(int i=0;i<n;i++){ 37 for(int j=0;j<n;j++){ 38 cin>>a[i][j]; 39 } 40 } 41 cout<<prim()<<endl; 42 } 43 return 0; 44 }