10-08-2011, 11:42 AM
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char nod[10],st,ed;
int wts[10][10],n,i,j,k,x=0,y,s=0,pos=-1,max=100;
clrscr();
printf("Enter the no of nodes:");
scanf("%d",&n);
printf("Enter nodes:");
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c",&nod[i]);
}
printf("Enter start & end node:");
fflush(stdin);
scanf("%c",&st);
fflush(stdin);
scanf("%c",&ed);
for(i=0;i<n;i++)
{
y=0;
for(j=0;j<n;j++)
{
printf("Wt between%c-%c:",nod[x],nod[y]);
scanf("%d",&wts[i][j]);
y++;
}
x++;
}
printf("\n");
//printing table
for(x=0;x<n;x++)
printf("\t %c",nod[x]);
printf("\n----------------------------\n");
for(i=0;i<n;i++)
{
printf("%c",nod[i]);
for(j=0;j<n;j++)
printf("\t %d",wts[i][j]);
printf("\n");
}
printf("-------------------------------------");
//finding pos of first node
for(i=0;i<n;i++)
{
pos++;
if(nod[i]==st)
break;
}
printf("\n pos=%c",nod[pos]);
//implement spa
for(k=0;k<n;k++)
{
for(x=0;x<n;x++)
if(wts[pos][x]!=0&&max>wts[pos][x])
{
max=wts[pos][x];
y=x;
}
printf("\n pos=%c",nod[y]);
s=s+max;
max=100;
if(nod[y]!=ed)
{
pos=-1;
for(i=0;i<n;i++)
{
pos++;
if(nod[i]==nod[y])
break;
}
}
else
break;
}
printf("\n Shortest path:%d",s);
getch();
}