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
|
#include <cstdio>
#include <iostream>
#include <list>
#include "pqueue.h"
using namespace std;
struct AdjList
{
AdjList* nextvert;
double weight;
int vertex;
AdjList(int vertexnum, double w, AdjList* v)
{
nextvert = v;
weight = w;
vertex = vertexnum;
}
};
typedef AdjList* Adj;
struct Vertex
{
AdjList* adjacents;
int next;
double distance;
bool visited;
Vertex(){
visited = false;
}
};
struct Graph
{
int numofvertices;
Vertex* vertexi;
Graph(int verts){
numofvertices = verts;
vertexi = new Vertex[verts + 1];
for (int x = 1; x <= verts; x++){
vertexi[x].visited = false;
}
}
};
bool IsEmpty(Adj x){
return x == NULL;
}
Adj cons(int vert, double d, Adj x){
return new AdjList(vert, d, x);
}
void insertadj(int vert, double d, Adj& x){
x = cons(vert, d, x);
}
int getVertex(Adj x){
return x -> vertex;
}
double getWeight(Adj x){
return x -> weight;
}
Adj nextAdj(Adj x){
return x -> nextvert;
}
void read(Graph g){
int v1, v2;
double x;
cin >> v1;
while (v1 != 0){
cout << v1 << endl;
cin >> v2;
cout << v2 << endl;
cin >> x;
insertadj(v1, x, g.vertexi[v2].adjacents);
insertadj(v2, x, g.vertexi[v1].adjacents);
cin >> v1;
}
}
void PrintVert(Adj x, int count){
while (x->nextvert != NULL)
{
int vtemp = getVertex(x);
if (vtemp > count)
{
cout << "( " << count << "," << vtemp << " ) Weight: " << getWeight(x) << endl;
}
x = nextAdj(x);
}
}
void PrintGraph(Graph G){
int count = 1;
while (count <= G.numofvertices){
PrintVert(G.vertexi[count].adjacents, count);
count++;
}
}
int main(int argc, char** argv)
{
int x,start,end;
cin >> x;
Graph G(x);
read(G);
cin >> start;
cin >> end;
PrintGraph(G);
return 0;
}
|