📄 grpriml1.cpp
字号:
#include <iostream.h>
#include <stdlib.h>
#include "book.h"
#define INFINITY 65535 // Big enough for simple tests
#include "grlist.h"
void AddEdgetoMST(int v1, int v2) {
cout << "Add edge " << v1 << " to " << v2 << "\n";
}
int minVertex(Graph* G, int* D) { // Find min cost vertex
int i, v; // Initialize v to any unvisited vertex;
for (i=0; i<G->n(); i++)
if (G->getMark(i) == UNVISITED) { v = i; break; }
for (i=0; i<G->n(); i++) // Now find smallest value
if ((G->getMark(i) == UNVISITED) && (D[i] < D[v])) v = i;
return v;
}
void Prim(Graph* G, int* D, int s) { // Prim's MST algorithm
int V[G->n()]; // Who's closest
int i, w;
for (i=0; i<G->n(); i++) { // Process the vertices
int v = minVertex(G, D);
G->setMark(v, VISITED);
if (v != s) AddEdgetoMST(V[v], v); // Add edge to MST
if (D[v] == INFINITY) return; // Unreachable vertices
for (w=G->first(v); w<G->n(); w = G->next(v,w))
if (D[w] > G->weight(v,w)) {
D[w] = G->weight(v,w); // Update distance
V[w] = v; // Update who it came from
}
}
}
// Test Depth First Search:
// Version for Adjancency Matrix representation
main(int argc, char** argv) {
Graph* G;
FILE *fid;
if (argc != 2) {
cout << "Usage: grdijk1m <file>\n";
exit(-1);
}
if ((fid = fopen(argv[1], "rt")) == NULL) {
cout << "Unable to open file |" << argv[1] << "|\n";
exit(-1);
}
G = createGraph<Graphl>(fid);
if (G == NULL) {
cout << "Unable to create graph\n";
exit(-1);
}
int D[G->n()];
for (int i=0; i<G->n(); i++) // Initialize
D[i] = INFINITY;
D[0] = 0;
Prim(G, D, 0);
for(int k=0; k<G->n(); k++)
cout << D[k] << " ";
cout << endl;
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -