swust oj 1075

求最小生成树(Prim算法)

1000(ms)
10000(kb)
2251 / 4487
Tags: 生成树

求出给定无向带权图的最小生成树。图的定点为字符型,权值为不超过100

的整形。在提示中已经给出了部分代码,你只需要完善Prim算法即可。

#include< iostream> 
using namespace std; 

typedef struct 

int n; 
int e; 
char data[500]; 
int edge[500][500]; 
}Graph; 

typedef struct 

int index; 
int cost; 
}mincost; 

typedef struct 

int x; 
int y; 
int weight;    
}EDGE; 


typedef struct 

int index; 
int flag; 
}F; 

void create(Graph &G,int n ,int e) 

int i,j,k,w; 
char a,b; 
for(i=0;i< n;i++) 
cin>>G.data[i]; 
for(i=0;i< n;i++) 
for(j=0;j< n;j++) 

if(i==j) 
G.edge[i][j]=0; 
else 
G.edge[i][j]=100; 

for(k=0;k< e;k++) 

cin>>a; 
cin>>b; 
cin>>w; 
for(i=0;i< n;i++) 
if(G.data[i]==a) break; 
for(j=0;j< n;j++) 
if(G.data[j]==b) break; 

G.edge[i][j]=w; 
G.edge[j][i]=w; 

G.n=n; 
G.e=e; 

void Prim(Graph &G,int k) 

//完成Prim算法 

int main() 

Graph my; 
int n,e; 
cin>>n>>e; 
create(my,n,e); 
Prim(my,0);    
return 0; 

输入

第一行为图的顶点个数n第二行为图的边的条数e接着e行为依附于

一条边的两个顶点和边上的权值

输出

最小生成树中的边。

样例输入

6
10
ABCDEF
A B 6
A C 1
A D 5
B C 5
C D 5
B E 3
E C 6
C F 4
F D 2
E F 6

样例输出

 (A,C)(C,F)(F,D)(C,B)(B,E)
 1 #include<iostream>
 2 #include<cstring>
 3 using namespace std;
 4 typedef struct
 5 {
 6     int n;           //点的个数
 7     int e;           //边的条数
 8     char data[500];    //接收点的字符串
 9     int edge[500][500]; //图的邻接矩阵
10 } Graph;
11 typedef struct
12 {
13     int index;
14     int cost;
15 } mincost;
16 void create(Graph &G,int n,int e)  //建立图
17 {
18     int i,j,k,w;
19     char a,b;
20     for(i=0; i< n; i++)
21         cin>>G.data[i];
22     for(i=0; i< n; i++)           //给图的邻接矩阵赋初值
23         for(j=0; j< n; j++)
24         {
25             if(i==j)
26                 G.edge[i][j]=0;
27             else
28                 G.edge[i][j]=100;
29         }
30     for(k=0; k< e; k++)
31     {
32         cin>>a;
33         cin>>b;
34         cin>>w;
35         for(i=0; i< n; i++)    //填充邻接矩阵
36             if(G.data[i]==a)
37                 break;
38         for(j=0; j< n; j++)
39             if(G.data[j]==b)
40                 break;
41         G.edge[i][j]=w;
42         G.edge[j][i]=w;
43     }
44     G.n=n;
45     G.e=e;
46 }
47 void Prim(Graph &G,int v)
48 {
49     int lowcost[1000];
50     memset(lowcost,0,sizeof(lowcost));
51     int MIN;
52     int closest[1000],i,j,k;
53     for(i=0; i<G.n; i++)      //根据初始点来记录该点能到的点连的边的权
54     {
55         lowcost[i]=G.edge[v][i];
56         closest[i]=v;
57     }
58     for(i=1; i<G.n; i++)
59     {
60         MIN=100;
61         for(j=0; j<G.n; j++)
62             if(lowcost[j]!=0&&lowcost[j]<MIN)      //找最小的边
63             {
64                 MIN=lowcost[j];
65                 k=j;
66             }
67         cout<<"("<<G.data[closest[k]]<<","<<G.data[k]<<")";
68         lowcost[k]=0;
69         for(j=0; j<G.n; j++)
70             if(lowcost[j]&&G.edge[k][j]<lowcost[j])     //以k为初点来初始化
71             {
72                 lowcost[j]=G.edge[k][j];
73                 closest[j]=k;
74             }
75     }
76 }
77 int main()
78 {
79     Graph my;
80     int n,e;
81     cin>>n>>e;
82     create(my,n,e);
83     Prim(my,0);
84     return 0;
85 }

猜你喜欢

转载自www.cnblogs.com/Iwpml-595/p/10673037.html