티스토리 뷰
#include<stdio.h>
#include<stdlib.h>
#include<limits.h>
#define MAX 8
#define TRUE 1
#define FALSE 0
int cost[][MAX] = 
 {{0,9999,9999,9999,9999,9999,9999,9999},
 {300,0,9999,9999,9999,9999,9999,9999},
 {1000,800,0,9999,9999,9999,9999,9999},
 {9999,9999,1200,0,9999,9999,9999,9999},
 {9999,9999,9999,1500,0,250,9999,9999},
 {9999,9999,9999,1000,9999,0,900,1400},
 {9999,9999,9999,9999,9999,9999,0,1000},
 {1700,9999,9999,9999,9999,9999,9999,0}};
char * name_arr[MAX] = {"LosAngeles  ", "SanFrancisco", "Denver      ", "Chicago     "
                       ,"Boston      ", "NewYork     ", "Miami       ", "NewOrleans  "};
void shortestpath(int v, int cost[][MAX], int distance[], int n, short int found[]);
int choose(int distance[], int n, short int found[]);
int main()
{
 int i;
 int input;
 int distance[MAX];
 short int found[MAX] = {FALSE};
 int n = MAX;
 
 for(i=0; i<MAX; i++)
     printf("%d : %s\n", i+1, name_arr[i]); 
 
 while(1)
 {
     printf("\n출발할 도시번호를 입력하세요(1-8) : " ); 
        scanf("%d", &input);
        if(input >=1 && input <= 8) break;
        printf("잘못된 입력입니다!!\n"); 
     }
    printf("==================================\n");
 shortestpath(input-1, cost, distance, MAX, found);
 for(i=0; i<MAX; i++)
    {
        if(distance[i] == 9999)
            printf("%s : 없음\n", name_arr[i]);
        else    
            printf("%s : %d\n", name_arr[i], distance[i]);
  
 }
    
    system("pause"); 
 return 0;
}// main
//function shortestpath
void shortestpath(int v, int cost[][MAX], int distance[], int n, short int found[])
{
 int k, u, w;
 for (k=0; k<n; k++){
  found[k] = FALSE;
  distance[k] = cost[v][k];
 }
 found[v] = TRUE;
 distance[v] = 0;
 for(k=0; k<n-2; k++){
  u = choose(distance, n, found);
  found[u] = TRUE;
  for(w=0; w<n; w++)
   if(!found[w])
    if(distance[u] + cost[u][w] < distance[w])
     distance[w] = distance[u] + cost[u][w];
 }
}// end shortestpath function
//function choose
int choose(int distance[], int n, short int found[]){
 int k, min, minpos;
 min = INT_MAX;
 minpos = -1;
 for(k=0; k<n; k++)
  if( (distance[k] < min) && (!found[k]) ){
   min = distance[k];
   minpos = k;
  }
 return minpos;
}
'기억하자정보 > 알고리즘' 카테고리의 다른 글
| [알고리즘] C로 구현한 다익스트라 최단경로.. (1) | 2006.12.07 | 
|---|---|
| 최단경로 알고리즘 C++로 작성 (0) | 2006.12.07 | 
| 최단거리 알고리즘 참고 사이트 (0) | 2006.12.07 | 
| 최단경로 알고리즘 (0) | 2006.12.07 | 
| 최단거리 알고리즘 (0) | 2006.12.07 | 
- 안내
- 궁금한 점을 댓글로 남겨주시면 답변해 드립니다.