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 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204
| #include<stdio.h> #include<stdlib.h> #include<math.h>
#define ERROR 0 #define OK 1 #define Overflow 2 #define Underflow 3 #define NotPresent 4 #define Duplicate 5 #define INFTY 32657 #define FALSE 0 #define TRUE 1 typedef int ElemType; typedef int Status; typedef int BOOL;
typedef struct ENode{ int adjVex; ElemType w; struct ENode *nextArc; }ENode; typedef struct{ int n; int e; ENode **a; }LGraph;
Status Init(LGraph *lg,int nSize){ int i; lg->n = nSize; lg->e = 0; lg->a = (ENode**)malloc(nSize*sizeof(ENode*)); if(!lg->a) return ERROR; for(i = 0;i < lg->n;i ++) lg->a[i] = NULL; return OK; }
int Destory(LGraph *lg){ int i; ENode *p,*q; for(i = 0;i < lg->n;i ++){ p = lg->a[i]; q = p; while(p){ p = p->nextArc; free(q); q = p; } } free(lg->a); return OK; }
Status Exist(LGraph *lg,int u,int v){ ENode *p; if(u < 0||v < 0||u > lg->n-1 ||v > lg->n-1 ||u == v) return ERROR; p = lg->a[u]; while(p!=NULL && p->adjVex != v){ p = p->nextArc; } if(!p) return ERROR; else return OK; }
Status Insert(LGraph *lg,int u,int v,ElemType w){ ENode *p; if(u < 0||v < 0||u > lg->n-1||v > lg->n-1 ||u == v) return ERROR; if(Exist(lg,u,v)) return Duplicate; p = (ENode*)malloc(sizeof(ENode)); p->adjVex = v; p->w = w; p -> nextArc = lg->a[u]; lg->a[u] = p; lg->e ++; return OK; }
Status Remove(LGraph *lg,int u,int v){ ENode *p,*q; if(u < 0 || v < 0 || u > lg->n-1 || v > lg->n-1 || u == v) return ERROR; p = lg->a[u]; q = NULL; while(p && p->adjVex != v){ q = p; p = p->nextArc; } if(!p) return NotPresent; if(q) q->nextArc = p->nextArc; else lg->a[u] = p->nextArc; free(p); lg->e --; return OK; }
int Choose(int d[],int n,int s[]){ int minpos; int i; ElemType min; min = INFTY; minpos = -1; for(i = 0;i < n;i ++){ if( d[i] <= min && !s[i]){ min = d[i]; minpos = i; } } return minpos; }
Status Dijkstra(LGraph g,int v,int d[],int path[]){ int i,k,w,distance = 0; int *s; if(v < 0 || v > g.n-1) return ERROR; ENode *j;
s = (int*)malloc(g.n*sizeof(int)); for(i = 0;i < g.n;i ++){ s[i] = 0; for( j=g.a[v];j!=NULL; j=j->nextArc) if(j->adjVex == i ) d[i] = j->w; if(i != v && d[i] < INFTY) path[i] = v; else path[i] = -1; } s[v] = 1; printf("The order:%d ",v); d[v] = 0;
for(i = 1;i <= g.n-1;i ++){ k = Choose(d, g.n ,s); s[k] = 1; printf("%d ",k); for( j = g.a[k]; j!=NULL; j= j->nextArc){ if( !s[j->adjVex] && d[k] + j->w < d[ j->adjVex ]){ d[j->adjVex ] = d[k] + j->w; distance = d[j->adjVex ]; path[j->adjVex ] = k; } } } return OK; } int main(){ LGraph g; int nSize,edge,u,v,i; int s,t; int d[100]; for(int i=0;i<100;i++) d[i] = INFTY; int path[100]; ElemType w; printf("Enter the number of mgraph's Nodes:"); scanf("%d",&nSize); Init(&g,nSize); printf("Enter the mgraph's Edge num:"); scanf("%d",&edge);
for(i = 0;i < edge;i ++){ printf("Please enter the edge(Pu,Pv,Weight):"); scanf("%d%d%d",&u,&v,&w); Insert(&g,u,v,w); }
printf("Enter the Start Point :"); scanf("%d",&s); printf("Enter the Destination Point :"); scanf("%d",&t);
Dijkstra(g,0,d,path); printf("\nThe shortest distance from %d to Point %d:%d\n",s,t, d[t]); system("pause"); return 0; }
|