d(k)[i]:源点到i最多经过k条边时的最小值
d(k)[i]=min(d(k-1)[i],d(k-1)[j]+w(j,i))
无负圈,则最短路最多经过n-1条边
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1e3+20;
const ll inf=1e9;
int d[N],n;//
vector<pair<int,int> > e[2*N];
void bellman_ford()
{
fill(d,d+n+1,inf);
d[n]=0;
for(int k=1;k<n;k++)//最多经过k条边时
{
//遍历边集更新
for(int i=1;i<=n;i++)
{
for(int j=0;j<e[i].size();j++)
{
int v=e[i][j].first;
int w=e[i][j].second;
d[v]=min(d[v],d[i]+w);
}
}
}
}
int main()
{
int t;
cin>>t>>n;
for(int i=1;i<=t;i++)
{
int u,v,w;
cin>>u>>v>>w;
e[u].push_back(make_pair(v,w));
e[v].push_back(make_pair(u,w));
}
bellman_ford();
cout<<d[1]<<endl;
return 0;
}