00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <vis.h>
00019
00020 #ifdef DMALLOC
00021 #include "dmalloc.h"
00022 #endif
00023
00024 static COORD unseen = (double) INT_MAX;
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 int *shortestPath(int root, int target, int V, array2 wadj)
00039 {
00040 int *dad;
00041 COORD *vl;
00042 COORD *val;
00043 int min;
00044 int k, t;
00045
00046
00047 dad = (int *) malloc(V * sizeof(int));
00048 vl = (COORD *) malloc((V + 1) * sizeof(COORD));
00049 val = vl + 1;
00050
00051
00052 for (k = 0; k < V; k++) {
00053 dad[k] = -1;
00054 val[k] = -unseen;
00055 }
00056 val[-1] = -(unseen + (COORD) 1);
00057 min = root;
00058
00059
00060 while (min != target) {
00061 k = min;
00062 val[k] *= -1;
00063 min = -1;
00064 if (val[k] == unseen)
00065 val[k] = 0;
00066
00067 for (t = 0; t < V; t++) {
00068 if (val[t] < 0) {
00069 COORD newpri;
00070 COORD wkt;
00071
00072
00073 if (k >= t)
00074 wkt = wadj[k][t];
00075 else
00076 wkt = wadj[t][k];
00077
00078 newpri = -(val[k] + wkt);
00079 if ((wkt != 0) && (val[t] < newpri)) {
00080 val[t] = newpri;
00081 dad[t] = k;
00082 }
00083 if (val[t] > val[min])
00084 min = t;
00085 }
00086 }
00087 }
00088
00089 free(vl);
00090 return dad;
00091 }
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 int *makePath(Ppoint_t p, int pp, COORD * pvis,
00104 Ppoint_t q, int qp, COORD * qvis, vconfig_t * conf)
00105 {
00106 int V = conf->N;
00107
00108 if (directVis(p, pp, q, qp, conf)) {
00109 int *dad = (int *) malloc(sizeof(int) * (V + 2));
00110 dad[V] = V + 1;
00111 dad[V + 1] = -1;
00112 return dad;
00113 } else {
00114 array2 wadj = conf->vis;
00115 wadj[V] = qvis;
00116 wadj[V + 1] = pvis;
00117 return (shortestPath(V + 1, V, V + 2, wadj));
00118 }
00119 }