#include
#define INF 3200
void main()
{
FILE *fp,*fo;
fp=freopen("c:\\source.in","r",stdin);
fo=freopen("c:\\source.out","w",stdout);
int arr[10][10],n,edge,i,j,a,b,c,s,dist[10],d[10],opp,p;
int cost[10],index[10][5],small=0,count[10],k=0,m=0;
scanf("%d",&n);
for(i=0;i<10;i++)
for(j=0;j<5;j++)
index[i][j]=0;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
arr[i][j]=0;
else
arr[i][j]=INF;
}
}
scanf("%d",&edge);
for(i=1;i<=edge;i++)
{
scanf("%d%d%d",&a,&b,&c);
arr[a][b]=c;
}
scanf("%d",&s);
for(i=1;i<=n;i++)
{
d[i]=0;
dist[i]=arr[s][i];
}
d[s]=1;
if(s==1)
{
small=dist[2];
index[1][1]=s;
index[1][2]=2;
}
else
{
small=dist[1];
index[1][1]=s;
index[1][2]=1;
}
for(i=1;i<=n;i++)
{
if(dist[i]==0)
continue;
else if(dist[i]
{
small=dist[i];
index[1][1]=s;
index[1][2]=i;
}
}
d[index[1][1]]=1;
d[index[1][2]]=1;
cost[1]=arr[index[1][1]][index[1][2]];
count[1]=2;
int mn=j=2;
while(1)
{
k=0;
for(i=1;i<=n;i++)
{
if(d[i]==1)
continue;
else
k=1;
}
if(k==0)
break;
for(i=1;i<=n;i++)
{
m=0;
for(p=1;p
{
k=1;
while(index[p][k]!=0)
{
if(i==index[p][k])
m=1;
k++;
}
}
if(m==1)
continue;
else if(arr[index[j-1][count[j-1]]][i]==INF)
continue;
else if(small+arr[index[j-1][count[j-1]]][i]
{
int l=1;
while(index[j-1][l]!=0)
{
index[mn][l]=index[j-1][l];
l++;
}
index[mn][l]=i;
count[mn]=l;
cost[mn]=small+arr[index[j-1][count[j-1]]][i];
d[i]=1;
small=cost[j];
mn++;
}
else
{
index[mn][1]=s;
index[mn][2]=i;
d[i]=1;
count[mn]=2;
cost[mn]=arr[s][i];
mn++;
opp=mn;
}
}
j++;
}
for(i=1;i
{
for(k=1;k<=count[i];k++)
{
if(count[i]==1)
continue;
printf("%3d ",index[i][k]);
if(k==count[i])
printf(" %3d",cost[i]);
}
printf("\n\n");
}
//getch();
}
0 comments:
Post a Comment