思路
code
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int N = 510;
bool st[N];
int dist[N];
int g[N][N];
int n, m;
int dijstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
for (int i = 0; i < n; i ++ )
{
int t = -1; // t用来求所有st[] == false的节点中,dist[]最小的节点编号。
for (int j = 1; j <= n; j ++ )
{
if (!st[j] && (t == -1 || dist[j] < dist[t]))
t = j;
}
st[t] = true;
// 更新
for (int j = 1; j <= n; j ++ )
dist[j] = min(dist[j], dist[t] + g[t][j]);
}
if (dist[n] == 0x3f3f3f3f) return -1;
else return dist[n];
}
int main()
{
scanf("%d%d", &n, &m);
memset(g, 0x3f, sizeof g);
while(m -- )
{
int a, b, c;
scanf ("%d%d%d", &a, &b, &c);
g[a][b] = min(g[a][b], c);
}
int t = dijstra();
printf("%d\n", t);
return 0;
}
赞
啊?害hh