hihoCoder week23 最短路径·一
spfa 最短路
#include <bits/stdc++.h> using namespace std; #define pb push_back #define INF 1e16; typedef long long ll; typedef pair<int,ll> pil; const int N = 1e3+10; int n,m,st,ed; ll mp[N][N]; bool vis[N]; ll dis[N]; int main () { freopen("in.txt", "r", stdin); memset(mp, 0x3f, sizeof(mp)); scanf("%d %d %d %d", &n, &m, &st, &ed); for(int i=1; i<=m; i++) { int u,v; ll w; scanf("%d %d %lld", &u, &v, &w); mp[u][v]=min(mp[u][v], w); mp[v][u]=mp[u][v]; } for(int i=0; i<=n; i++) { dis[i] = INF; vis[i] = false; } dis[st] = 0; queue<int> que; que.push(st); vis[st] = true; while(!que.empty()) { // pil ans = que.front(); que.pop(); // int tmp = ans.first; // ll L = ans.second; int tmp = que.front(); que.pop(); vis[tmp] = 0; for(int i=1; i<=n; i++) { // int v = mp[tmp][i].first; if(tmp == i) continue; ll dd = mp[tmp][i]; if(dis[tmp] + dd < dis[i]) { dis[i] = dis[tmp] + dd; if(!vis[i]) { vis[i] = true; que.push(i); } } } } printf("%lld\n", dis[ed]); return 0; }