zoukankan      html  css  js  c++  java
  • 【模板】堆优化 + dij +pair 存储

    就是短

    感谢Cptraser dalao的博客

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 struct node {
     4     int val,num;
     5     bool operator <(const node &x) const {
     6         return val>x.val; 
     7     }
     8 };
     9 priority_queue <node> dij; 
    10 vector < pair<int,int> > a[500001];
    11 int n,m,s,dist[500001],done[500001]; //dist存储边权,done记录是否已经进行了松弛操作;
    12 int main(){
    13     cin>>n>>m>>s;
    14     for(int i=1;i<=m;i++){
    15         int x,y,c;
    16         cin>>x>>y>>c;
    17         a[x].push_back(make_pair(y,c));
    18     }
    19     for(int i=1;i<=m;i++){
    20         dist [i]=0x7fffffff;//便于进行松弛 luogu的弱化版有一个测试点就是卡的这个,七个f打全。
    21     }
    22     dist [s]=0;
    23     dij.push((node){0,s});//放入堆顶元素
    24     while (!dij.empty()){//dij标准格式233
    25         int front=dij.top().num;//重复取出堆顶    如果已经拓展过就continue     松弛操作同时满足条件放入堆 这三个操作26         dij.pop();
    27         if(done[front]) continue;
    28         done[front]=true;
    29         for(int i=0;i<a[front].size();i++){
    30             int to=a[front][i].first,vl=a[front][i].second;
    31             if(!done[to] && dist[front]+vl<dist[to]){
    32                 dist[to]=dist[front]+vl;
    33                 dij.push((node){dist[to],to});
    34             }
    35         }
    36     }
    37     for(int i=1;i<=n;i++) {
    38         cout<<dist[i]<<" ";
    39     }
    40     return 0;
    41 }

    留坑以后写pair的玄学用法

  • 相关阅读:
    二叉搜索树的后序遍历序列(python)
    从上往下打印二叉树(python)
    仿 PS中的颜色填充工具
    噪点图
    另一种噪点图 (有点类似卫星云图)
    像素融解
    比较位图差异
    颜色变换
    像素拷贝及赋值
    使用(模糊)滤镜
  • 原文地址:https://www.cnblogs.com/luv-letters/p/9629584.html
Copyright © 2011-2022 走看看