#include<stdio.h> 

void dist_vector(int n); 
void init(int n); 

// Creating structure node 
struct node { 
    unsigned int dist[20], nexthop[20]; 
} route[10]; 

int i, j; 

// Main function 
void main() { 
    int n, i, j; 
    printf("Enter the number of routers: "); 
    scanf("%d", &n); 

    init(n); 

    printf("Enter the cost matrix (999 for no link):\n"); 
    for(i = 0; i < n; i++)  
        for(j = 0; j < n; j++)  
            scanf("%d", &route[i].dist[j]); 

    dist_vector(n); 

    printf("\n------------------------------------"); 
    printf("\nUpdated Distance Vector Table\n"); 
    printf("--------------------------------------\n"); 

    for(i = 0; i < n; i++) { 
        for(j = 0; j < n; j++) { 
        }  
        printf("\n"); 
    }  

    printf("%d\t", route[i].dist[j]); 
    printf("\n----------------------------\n"); 

    for(i = 0; i < n; i++) { 
        printf("\nRouting table for node %c table\n", 65 + i); 
        printf("-------------------------\n"); 
        printf("Desti\t Cost\t Next hop\n"); 
        printf("--------------------------\n"); 

        for(int j = 0; j < n; j++)  
            if(i != j)  
                printf("%c\t%d\t%c\n", 65 + j, route[i].dist[j], 65 + route[i].nexthop[j]); 
    } 
} 

// Initialization 
void init(int n) { 
    int i, j; 
    for(i = 0; i < n; i++) { 
        for(j = 0; j < n; j++) { 
            if(i != j) { 
                route[i].dist[j] = 999; 
                route[i].nexthop[j] = -20; 
            } 
            route[i].dist[i] = 0; 
            route[i].nexthop[j] = -20; 
        } 
    } 
} 

void dist_vector(int n) { 
    int count; 
    do { 
        count = 0; 
        for (int i = 0; i < n; i++) { 
            for (int j = 0; j < n; j++) { 
                for (int k = 0; k < n; k++) { 
                    if ((route[i].dist[j]) > (route[i].dist[k] + route[k].dist[j])) { 
                        route[i].dist[j] = route[i].dist[k] + route[k].dist[j]; 
                        route[i].nexthop[j] = k; 
                        count = 1; 
                    } 
                } 
            } 
        } 
    } while (count); 
} 
