您的位置:首页 > 其它

【HDOJ】1385 Minimum Transport Cost

2014-05-30 18:53 281 查看
Floyd。注意字典序!!!

#include <stdio.h>
#include <string.h>

#define MAXNUM 55
#define INF 0x1fffffff

int cost[MAXNUM][MAXNUM];
int path[MAXNUM][MAXNUM];
int taxes[MAXNUM];
int que[MAXNUM];
int n;

void floyd(int n) {
int i, j, k, tmp;

for (i=1; i<=n; ++i)
for (j=1; j<=n; ++j)
path[i][j] = j;

for (k=1; k<=n; ++k) {
for (i=1; i<=n; ++i) {
for (j=1; j<=n; ++j) {
tmp = cost[i][k]+cost[k][j]+taxes[k];
if (cost[i][j] > tmp) {
cost[i][j] = tmp;
path[i][j] = path[i][k];
} else if (cost[i][j] == tmp) {
if (path[i][j] > path[i][k])
path[i][j] = path[i][k];
}
}
}
}
}

void output(int a, int b) {
int front, rear;
int x = a;
front = rear = 0;

//que[rear++] = a;
while (path[x][b] != b) {
que[rear++] = path[x][b];
x = path[x][b];
}
if (a != b)
que[rear++] = b;

printf("From %d to %d :\n", a, b);
printf("Path: %d", a);
while (front < rear)
printf("-->%d", que[front++]);
printf("\nTotal cost : %d\n\n", cost[a][b]);
}

int main() {
int a, b;
int i, j;

while (scanf("%d", &n)!=EOF && n) {
for (i=1; i<=n; ++i)
for (j=1; j<=n; ++j) {
scanf("%d", &cost[i][j]);
if (cost[i][j] < 0)
cost[i][j] = INF;
}
for (i=1; i<=n; ++i)
scanf("%d", &taxes[i]);
floyd(n);
while (1) {
scanf("%d %d", &a, &b);
if (a==-1 && b==-1)
break;
output(a, b);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: