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的玄学用法

  • 相关阅读:
    迭代器生成器
    eval()
    【剑指offer】调整数组数字位置
    【剑指offer】二进制中1的个数
    【剑指offer】斐波那契数列非递归求解第N项
    【剑指offer】两个栈实现队列
    【剑指offer】逆序输出链表
    【剑指offer】字符串替换
    【剑指offer】规则二维数组查找
    Java转型
  • 原文地址:https://www.cnblogs.com/luv-letters/p/9629584.html
Copyright © 2011-2022 走看看