最小生成树(Prim)代码实现

来源:互联网 发布:nginx apache php性能 编辑:程序博客网 时间:2024/06/11 12:03

    代码的思路就是按数据结构书上的,有错误请指出。

代码:

#include <stdio.h>#include <string.h>#include <limits.h>#define N 1010typedef struct point{    int vis, dis;}P;P closedge[N]; // vis 存储每个点是否已经遍历 , // dis 从已经遍历过的点 到未遍历点的最短距离 int a[N][N]; // 存储图 , a[i][j] 点 i 到点 j 距离 const int INF = 1000000007;int lct(int n,int sta) //n 总点数;sta 为最小生成树的初始点 {int lowcost = 0; // 初始最小的花费     for(int i=1; i<=n; i++)      {       if(i != sta)       {closedge[i].vis = 0; // 标记未遍历 closedge[i].dis = a[sta][i]; // 更新       }  }   closedge[sta].vis = 1;for(int i=2; i<=n; i++){int min = INF, minj;    // min 找到最短的一条边的长度, minj 加入最短边的点的标号 for(int j=1; j<=n; j++){if(closedge[j].vis == 0 && closedge[j].dis < min)     {        minj = j;        min = closedge[j].dis; }} lowcost += min;closedge[minj].vis = 1;printf("%d\n",minj); // 遍历过程中经过的点 for(int j=1; j<=n; j++){if(closedge[j].vis == 0 && a[minj][j] <= closedge[j].dis){closedge[j].dis = a[minj][j];} }}return lowcost;}int main(){int n;scanf("%d",&n); // n 个点 memset(a, INF, sizeof(a)); // 初始图  int x, y, dis;for(int i=1; i<=n; i++){ scanf("%d %d %d",&x,&y,dis); a[x][y] = dis;} printf("%d\n",lct(n, 1));return 0; }

用下面的一道题,验证下代码。

                                                       Outlets

                                                  
Problem Description
In China, foreign brand commodities are often much more expensive than abroad. The main reason is that we Chinese people tend to think foreign things are better and we are willing to pay much for them. The typical example is, on the United Airline flight, they give you Haagendazs ice cream for free, but in China, you will pay $10 to buy just a little cup.
So when we Chinese go abroad, one of our most favorite activities is shopping in outlets. Some people buy tens of famous brand shoes and bags one time. In Las Vegas, the existing outlets can't match the demand of Chinese. So they want to build a new outlets in the desert. The new outlets consists of many stores. All stores are connected by roads. They want to minimize the total road length. The owner of the outlets just hired a data mining expert, and the expert told him that Nike store and Apple store must be directly connected by a road. Now please help him figure out how to minimize the total road length under this condition. A store can be considered as a point and a road is a line segment connecting two stores.
 

Input
There are several test cases. For each test case: The first line is an integer N( 3 <= N <= 50) , meaning there are N stores in the outlets. These N stores are numbered from 1 to N. The second line contains two integers p and q, indicating that the No. p store is a Nike store and the No. q store is an Apple store. Then N lines follow. The i-th line describes the position of the i-th store. The store position is represented by two integers x,y( -100<= x,y <= 100) , meaning that the coordinate of the store is (x,y). These N stores are all located at different place. The input ends by N = 0.
 

Output
For each test case, print the minimum total road length. The result should be rounded to 2 digits after decimal point.
 

Sample Input
42 30 01 00 -1 1 -10
 

Sample Output
3.41

题意:

n 个 店铺,建路, 找到最小花费, 就是求最小生成树, 不过 第 p, q 个店铺之间必须需建一条路。

代码:

#include <stdio.h>#include <string.h>#include <limits.h>#include <math.h>#define N 100typedef struct point{    int vis;double dis;}P;P closedge[N];double a[N][N], pos[N][2];const double INF = 1000000007;int p, q;double lct(int n,int sta)  {double lowcost = 0;    for(int i=1; i<=n; i++)      {       if(i != sta)       {closedge[i].vis = 0;closedge[i].dis = a[sta][i];     }  }   closedge[sta].vis = 1;closedge[q].vis = 1; //先建 p 到 q 的路 lowcost += a[p][q];for(int j=1; j<=n; j++){if(closedge[j].vis == 0 && a[q][j] <= closedge[j].dis){closedge[j].dis = a[q][j];} } //先建 p 到 q 的路 for(int i=3; i<=n; i++) // 更新后面 n - 3 个点 {double min = INF;int  minj;for(int j=1; j<=n; j++){if(closedge[j].vis == 0 && closedge[j].dis < min)     {        minj = j;        min = closedge[j].dis; }} lowcost += min;closedge[minj].vis = 1;//printf("%.2f %d\n",min,minj);for(int j=1; j<=n; j++){if(closedge[j].vis == 0 && a[minj][j] <= closedge[j].dis){closedge[j].dis = a[minj][j];} }}return lowcost;}int main(){int n;while(scanf("%d",&n)){    if(n == 0)    break;      scanf("%d %d",&p,&q);        for(int i=1; i<=n; i++)  {  scanf("%lf %lf",&pos[i][0],&pos[i][1]);  }     for(int i=1; i<=n; i++) // 初始 图   {   for(int j=i; j<=n; j++)   {    if(i == j)    a[i][j] = INF;    else    {a[i][j] = sqrt(pow((pos[i][0] - pos[j][0]), 2) + pow((pos[i][1] - pos[j][1]), 2));a[j][i] = a[i][j];     } }  }    printf("%.2f\n",lct(n, p)); // 最小生成树的初始点 p  }return 0; }









原创粉丝点击