全源最短路,floyd一下就完事了,略水
#include<stdio.h>
#include<string.h>
#include<limits.h>
static char wh[30][3];
static int map[30][30];
static int m, n;
static int get_index(char *s)
{
int i;
for (i = 0; i < m; i++)
if (strcmp(wh[i], s) == 0)
return i;
return -1;
}
static void floyd()
{
int i, j, k, a, b;
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
map[i][j] = i == j ? 0 : INT_MAX;
char s[3], t[3];
for (i = 0; i < n; i++)
{
scanf("%s %s", s, t);
a = get_index(s);
b = get_index(t);
map[a][b] = map[b][a] = 1;
}
for (k = 0; k < m; k++)
for (i = 0; i < m; i++)
for (j = 0; j < m; j++)
if (map[i][k] != INT_MAX && map[k][j] != INT_MAX
&& map[i][k] + map[k][j] < map[i][j])
map[i][j] = map[i][k] + map[k][j];
}
int main()
{
int t, p, it;
scanf("%d", &t);
puts("SHIPPING ROUTES OUTPUT\n");
for (it = 1; it <= t; it++)
{
printf("DATA SET %d\n\n", it);
scanf("%d %d %d", &m, &n, &p);
getchar();
int i, cost, a, b;
for (i = 0; i < m; i++)
scanf("%s", wh[i]);
floyd();
char s[3], t[3];
for (i = 0; i < p; i++)
{
scanf("%d %s %s", &cost, s, t);
a = get_index(s);
b = get_index(t);
if (map[a][b] != INT_MAX)
printf("$%d\n", map[a][b] * 100 * cost);
else
puts("NO SHIPMENT POSSIBLE");
}
putchar('\n');
}
puts("END OF OUTPUT");
return 0;
}