#include<stdio.h>
#include<string.h>
int map[101][101];
int mark[101];
int dis[101];
int main()
{
int n,m,a,b,i,j,t,ta,tb,min,idx;
while(scanf("%d", &n) != EOF)
{
scanf("%d", &m);
scanf("%d%d", &a, &b);
for(i=0; i<=n; i++)
{
for(j=0; j<=n; j++)
{
map[i][j] = 999999;
}
map[i][i] = 0;
}
memset(mark,0,sizeof(mark));
for(i=0; i<m; i++)
{
scanf("%d%d%d", &ta, &tb, &t);
if(t < map[ta][tb])
map[ta][tb] = map[tb][ta] = t;
}
for(i=1; i<=n; i++)
{
dis[i] = map[a][i];
}
for(j=1; j<=n; j++)
{
min = 999999;
idx = -1;
for(i = 1; i<=n; i++)
{
if(dis[i] < min && mark[i] == 0) //判断是否已经加入集合
{
min = dis[i];
idx = i;
}
}
if(idx == -1)
break;
mark[idx] = 1;
if(idx == b)
break;
for(i=1; i<=n; i++)
{
if(mark[i] == 0 && dis[idx] + map[idx][i] < dis[i]) //是对dis进行赋值,而不是map
dis[i] = dis[idx] + map[idx][i];
}
}
printf("%d\n",dis[b]);
}
return 0;
}
九度oj-1341:艾薇儿的演唱会
最新推荐文章于 2017-05-19 21:08:12 发布