#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
const int N=510;
int st[N];
int g[N][N];
int dist[N];
int n,m;
int dijkstra()
{
int i,j;
memset(dist,0x3f,sizeof(dist));
dist[1]=0;
for(i=0;i<n;i++)
{
int t=-1;
for(j=1;j<=n;j++)
{
if(st[j]==0&&(t==-1||dist[t]>dist[j]))
{
t=j;
}
}
st[t]=true;
for(j=1;j<=n;j++)
{
dist[j]=min(dist[j],g[t][j]+dist[t]);
}
}
if(dist[n]==0x3f3f3f3f)
return -1;
return dist[n];
}
int main()
{
scanf("%d%d",&n,&m);
memset(g,0x3f3f,sizeof(g));
int i,j;
while(m--)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a][b]=min(g[a][b],c);
}
int t=dijkstra();
cout<<t;
return 0;
}
dijkstra模板题
于 2020-07-28 14:38:26 首次发布