-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPrim_MST.cpp
119 lines (97 loc) · 2.63 KB
/
Prim_MST.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
//Minimum Spanning Tree
//Prims Algorithm
#include <bits/stdc++.h>
using namespace std;
#define LL long long
#define EPS 0.00000001
#define PI 2*acos(0.0)
#define MOD 1000000007
#define ck(XX) cout<<XX<<endl
#define set(XX,POS) XX|(1<<POS)
#define reset(XX,POS) XX&(~(1<<POS))
#define check(XX,POS) (bool)(XX&(1<<POS))
#define toggle(XX,POS) (XX^(1<<POS))
#define Fin freopen("input.txt","r",stdin)
#define Fout freopen("output.txt","w",stdout)
#define valid(X,Y,R,C) X>=0 && X<R && Y>=0 && Y<C
#define MS(ARRAY,VALUE) memset(ARRAY,VALUE,sizeof(ARRAY))
#define RT printf("Run Time : %0.3lf seconds\n", clock()/(CLOCKS_PER_SEC*1.0))
struct Edge{
int from;
int to;
int cost;
Edge(){};
Edge(int from_, int to_, int cost_){from=from_; to=to_; cost=cost_;}
};
bool operator < (Edge A, Edge B) {return A.cost > B.cost;}
int node, edge; //Total number of node and edge
int sow; //Sum Of Weight of MST
bool taken[100];
vector <Edge> adj[100]; //For storing given graph data
vector <Edge> tree[100]; //For creating new minimum spanning tree
void PMST(int source)
{
sow = 0;
MS(taken,0);
priority_queue <Edge> Q;
taken[source] = 1;
for(int i=0; i<adj[source].size();i++) //Pushing all edge of source node
{
Q.push(Edge(adj[source][i].from, adj[source][i].to, adj[source][i].cost));
}
while(!Q.empty())
{
Edge u = Q.top(); //taking the minimum edge from taken nodes
Q.pop();
if(taken[u.to]) continue;
taken[u.to] = 1;
sow += u.cost;
tree[u.from].push_back(u); //creating tree
tree[u.to].push_back(Edge(u.to,u.from,u.cost)); //creating tree
for(int i=0; i<adj[u.to].size();i++) //Pushing all edge of taken node
{
if(taken[adj[u.to][i].to]) continue;
Q.push(Edge(adj[u.to][i].from, adj[u.to][i].to, adj[u.to][i].cost));
}
}
return;
}
void Graph_Input(int x, int y, int cost)
{
adj[x].push_back(Edge(x,y,cost));
adj[y].push_back(Edge(y,x,cost));
return;
}
void show_tree()
{
for(int i=0; i< node; i++)
{
for(int j=0; j<tree[i].size(); j++)
{
printf("%d %d %d\n",tree[i][j].from, tree[i][j].to, tree[i][j].cost);
}
}
printf("\n");
return;
}
int main()
{
//Fin;
//Fout;
int tc, cn=0;
scanf("%d",&tc);
while(tc--){
for(int i=0; i<100; i++) {adj[i].clear(); tree[i].clear();}
scanf("%d%d",&node, &edge);
for(int i=0; i<edge; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
Graph_Input(x,y,z);
}
PMST(1);
printf("Case %d: %d\n", ++cn, sow);
show_tree();
}
return 0;
}