迪杰斯特拉算法求旅行最短时间花费费用
今天做力扣,发现题目审错了,以为求最短时间花费费用,于是自己就写了一个算法求解该问题:
passingFees是我们到每个城市花费费用
edges是城市之间连接的边信息。
代码如下:
int minCost(int maxTime, int** edges, int edgesSize, int* edgesColSize, int* passingFees, int passingFeesSize){
int dj[passingFeesSize][passingFeesSize];
int min[passingFeesSize];
int l[passingFeesSize];
int i,j;
int lm[passingFeesSize];
int m[passingFeesSize];
int po,min_l;
int mf=0;
int r[passingFeesSize];
for (i = 0; i < passingFeesSize; i++) {
for (j = 0; j < passingFeesSize; j++) {
dj[i][j] = 90000;
}
}
for (i = 0; i < edgesSize; i++) {
// printf("%d",i);
// printf(" %d %d %d ", edges[i][0], edges[i][1], edges[i][2]);
dj[edges[i][0]][edges[i][1]] = edges[i][2];
dj[edges[i][1]][edges[i][0]] = edges[i][2];
}
for (i = 0; i < passingFeesSize; i++) {
min[i] = dj[0][i];
}
for (i = 0; i < passingFeesSize; i++) {
r[i] = 0;
}
lm[0] = 0;
l[0] = 0;
min[0] = 0;
r[0] = 1;
for (i = 1; i < passingFeesSize ; i++) {
po = 0;
min_l = 90000;
for (j = 0; j < passingFeesSize; j++) {
if(r[j]==0)
if (min[j] < min_l) {
po = j;
min_l = min[j];
}
}
l[i] = po;
lm[i] = min_l;
r[po] = 1;
// printf("--%d ", po);
for (j = 0; j < passingFeesSize; j++) {
if (min_l + dj[po][j] < min[j])
min[j] = min_l + dj[po][j];
}
}
for (i = 0; i < passingFeesSize; i++) {
// printf("%d ",lm[i]);
mf=mf+passingFees[l[i]];
if (l[i] == passingFeesSize - 1) {
return mf;
}
}
return 0;
}