#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <cstring>
using namespace std;
#define MAXN 205
#define INF 0x3f3f3f3f
int visit[MAXN];
int Edge[MAXN][MAXN];
int dist[MAXN];
int n,m;
void Dijkstra(int v)
{
for(int i=0; i<n; i++)
{
visit[i] = 0;
dist[i] = Edge[v][i];
}
for(int i=0; i<n; i++)
{
int MIN = INF;
int u = v;
for(int j=0; j<n; j++)
if(!visit[j]&&dist[j]<MIN)
{
MIN = dist[j];
u = j;
}
visit[u] = 1;
for(int k=0; k<n; k++)
{
if(dist[u]+Edge[u][k]<dist[k])
dist[k] = dist[u]+Edge[u][k];
}
}
}
int main()
{
int u,v,w,st,en;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<n; i++)
for(int j=0; j<n; j++)
{
if(i==j)
Edge[i][j] = 0;
else
Edge[i][j] = INF;
}
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&u,&v,&w);
if(w<Edge[u][v])
Edge[u][v] = Edge[v][u] = w;
}
scanf("%d%d",&st,&en);
Dijkstra(st);
if(dist[en]==INF)
printf("-1\n");
else
printf("%d\n",dist[en]);
}
return 0;
}
hdu_1874 畅通工程续
最新推荐文章于 2019-10-27 21:09:43 发布