6032. 得到要求路径的最小带权子图

dijsktra模版

  1. typedef long long ll;
  2. typedef pair<int, int> pll;
  3. struct edge {
  4. int to, cost;//目的点序号,权重
  5. };
  6. vector<ll> dijkstra(int s, int n, vector<vector<edge>> &S) {
  7. vector<ll> dist(n, INF);
  8. dist[s] = 0;
  9. priority_queue<pll, vector<pll>, greater<pll> > pque;
  10. pque.push({0, s});//分别存储 距离,节点编号
  11. while(!pque.empty()) {
  12. auto p = pque.top(); pque.pop();//取出目前离源点距离最短的点v
  13. ll v = p.second, d = p.first;//v是该点序号,d是该点到源点距离
  14. if(dist[v] < d) continue;//如果当前到该点的最短距离比之前记录的短,跳过
  15. for (auto &e : S[v]) {//对点v的连接边更新
  16. if(dist[e.to] > dist[v] + e.cost) {
  17. //如果v某一连接点到源点的最短路径 > v到源点的最短路径+v到该点路径权值
  18. //难么就替换值,并把该连接点pair入堆
  19. dist[e.to] = dist[v] + e.cost;
  20. pque.push({dist[e.to], e.to});
  21. }
  22. }
  23. }
  24. return dist;
  25. }