Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void one_location(int n, float g[50][50], int type)
- {
- int start, end, i, j, next, count = 0, already[10] = {}, visit[50];
- float min, shortest[10], distance[10], save;
- printf("Start location: ");
- scanf("%d", &start);
- if (type == 2)
- {
- printf("End location: ");
- scanf("%d", &end);
- }
- for (i = 0; i < n; i++)
- for (j = 0; j < n; j++)
- if (g[i][j] == 0)
- g[i][j] = infinity;
- min = infinity;
- for (i = 0; i < n; i++)
- {
- distance[i] = g[start][i];
- shortest[i] = distance[i];
- visit[i] = start;
- if (min > distance[i])
- next = i;
- }
- min = infinity;
- j = 0;
- while (count < n - 1)
- {
- for (i = 0; i < n; i++)
- {
- if (i != start)
- {
- already[next] = 1;
- save = distance[i];
- distance[i] = distance[next] + g[next][i];
- if (distance[i] < shortest[i])
- {
- shortest[i] = distance[i];
- visit[i] = next;
- }
- else
- distance[i] = save;
- if (already[i] != 1 && min > shortest[i])
- {
- next = i;
- }
- }
- }
- count++;
- }
- if (type == 1)
- {
- for (i = 0; i < n; i++)
- if (i != start)
- {
- j = i;
- printf("\nThe shortest distance from %d to %d is %f from: ", start, i, distance[i]);
- printf("%d", i);
- while (j != start)
- {
- j = visit[j];
- printf("<-%d", j);
- }
- }
- }
- else if (type == 2)
- {
- printf("The shortest distance from %d to %d is %f from: ", start, end, distance[end]);
- printf("%d", end);
- j = end;
- while (j != start)
- {
- j = visit[j];
- printf("<-%d", j);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement