#include <iostream>
#include <vector>
using namespace std;
#define MAX_INT 0x7fffffff;
int map[1500];
int processed[1500];
int path[10001][10001];
int N, M, a, b, t;
void init_single_source(int *map, int size, int source)
{
int i;
for (i=0 ; i<size+1 ; i++)
{
map[i] = MAX_INT;
}
map[source] = 0;
}
/*抽取最小值位置,已使用的需要做标记*/
int extract_min(int *map, int size)
{
int i, min = MAX_INT;
int min_index;
for (i=1 ; i<=size ; i++)
{
if (map[i] < min && 0 == processed[i])
{
min = map[i];
min_index = i;
}
}
processed[min_index] = 1;
return min_index;
}
int main(int argc, char *argv[])
{
int i, j, min_index;
scanf("%d", &N);
scanf("%d", &M);
for (i=0 ; i<M ; i++)
{
scanf("%d %d %d ", &a, &b, &t);
path[a][b] = t;
path[b][a] = t;
}
scanf("%d %d", &a, &b);
init_single_source(map, N, a);
/*release*/
for (i=1 ; i<=N ; i++)
{
min_index = extract_min(map, N);
for (j=1 ; j<=N ; j++)
{
if (0 != path[min_index][j])
{
if (map[min_index] + path[min_index][j] < map[j])
{
map[j] = map[min_index] + path[min_index][j];
}
}
}
}
cout << map[b] << endl;
}
|