-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgit_prim.cpp
142 lines (110 loc) · 2.45 KB
/
git_prim.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//
// main.cpp
// git_Graph_Prim
//
// Created by Bonan Yang on 11/13/19.
// Copyright © 2019 Bonan Yang. All rights reserved.
//
#include <iostream>
#include <stack>
#include <cstdio>
#include <string>
#include <map>
#include <vector>
using namespace std;
static const int MAX_V_NUM = 100;
static const int MAX_W_NUM = 10000;
string s = "abcdefghijklmnopqrstuvwxyz";
class Prim
{
private:
int MAX_W_NUM = 66666;
int mapa[MAX_V_NUM] [MAX_V_NUM];
int dist[MAX_V_NUM];
bool visited[MAX_V_NUM];
int vn,en;
int start;
int st;
int sum;
int non1,non2,non3;
map<int,char> letter;
public:
void prim();
void init();
void showProcess();
void showshort();
};
void Prim::init()
{
int u,v,w;
for(int i = 1; i<=26; i++)
{
letter[i] =s[i-1];
}
cout<<"*********************************"<<endl;
cout<<"how many vertex?"<<endl;
cin>>vn;
cout<<"how many edge?"<<endl;
cin>>en;
for(int i = 1; i<=vn; i++)
{
for(int j = 1; j<= vn; j++)
mapa[i][j] = MAX_W_NUM;
}
cout<<"initialized"<<endl;
cout<<"now,input edge"<<endl;
for(int i = 0; i<en;i++)
{
cin>>u>>v>>w;
mapa[u][v] = min(w,mapa[u][v]);
mapa[v][u] = min(w,mapa[v][u]);
}
for(int i = 1 ;i<=vn;i++)
{
visited[i] =false;
dist[i] = MAX_W_NUM;
}
cout<<"input start"<<endl;
cin>>start;
cout<<"start is "<<letter[start]<<endl;
cout<<"$%$%$%$%$%$%$%$%$%%$%$%$%$%$%%"<<endl;
}
void Prim::prim()
{
sum = 0;
visited[start] = true;
st = start;
for(int i = 1; i<=vn-1;i++)
{
int min = MAX_W_NUM;
for(int j = 1; j<=vn;j++)
{
if(visited[j] == false && mapa[st][j]<dist[j])
{
dist[j] = mapa[st][j];
}
}
for(int k = 1; k<=vn;k++)
{
if(dist[k]<min&&!visited[k])
{
min = dist[k];
st = k;
}
cout<<" "<<dist[k] <<" ";
}
cout<<"st= "<<st<<endl;
visited[st] = true;
cout<<letter[st]<<endl;
sum = sum+min;
cout<<"sum = "<<sum<<endl;
if(st == start)return;
}
cout<<"MST weight is"<<sum<<endl;
}
int main()
{
Prim p;
p.init();
p.prim();
}