#include <stdio.h>
#include <stdlib.h>
#define INP "input.inp"
#define OUT "output.out"
int main()
{
FILE *f = fopen(INP, "r");
int n, a, b, i, sum = 0;
fscanf(f,"%d%d%d", &n, &a, &b);
int G[n][n];
int s[n], Len[n], P[n];
for (i = 0; i<n; i++) // nhap ma tran
for (int j=0; j<n; j++)
{
fscanf(f, "%d", &G[i][j]);
sum += G[i][j];
}
for (int i=0; i<n; i++) // dat vo cung bang tong gia tri ma tran
{
for (int j=0; j<n; j++)
{
printf("%3d", G[i][j]);
if (G[i][j] == 0) G[i][j] = sum;
}
printf("\n");
}
for (int i=0; i<n; i++)
{
Len[i] = sum; // do dai den diem i
s[i] = 0; // danh sach cac diem da xet
P[i] = a; // dat diem bat dau cua moi diem la a
}
Len[a] = 0; // dat do dai tu a -> a la 0
while (s[b] == 0) // trong khi diem cuoi chua duoc xet
{
for (i=0; i<n; i++) // tim 1 vi tri ma khong phai la vo cung
if (!s[i] && Len[i] < sum)
break;
if (i >= n) break;
for (int j=0; j<n; j++) // tim diem co vi tri ma do dai la min
if (!s[j] && Len[i] > Len[j])
i = j;
s[i] = 1; // cho i vao danh sach xet roi
for (int j=0; j<n; j++) // dubet tinh lai do dai cua cac diem chua xet
if (!s[j] && Len[i] + G[i][j] < Len[j])
{
Len[j] = Len[i] + G[i][j]; // thay doi len
P[j] = i; // danh dau diem truoc no
}
}
printf("\nLength of %d to %d is %d\n",a, b, Len[b]);
// truy vet
i = b;
while (i != a)
{
printf("%d <-- ", i);
i = P[i];
}
printf("%d", a);
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2RlZmluZSBJTlAgImlucHV0LmlucCIKI2RlZmluZSBPVVQgIm91dHB1dC5vdXQiCmludCBtYWluKCkKewoJRklMRSAqZiA9IGZvcGVuKElOUCwgInIiKTsKCWludCBuLCBhLCBiLCBpLCBzdW0gPSAwOwoJZnNjYW5mKGYsIiVkJWQlZCIsICZuLCAmYSwgJmI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