zoukankan      html  css  js  c++  java
  • 七夕专场-D题

    一个map的超水题。

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <queue>
    #include <algorithm>
    #include <algorithm>
    #define LL long long
    using namespace std;
    const int maxn = 200010;
    const int maxm = 200100;
    typedef pair<LL,int> pii;
    int v[maxm],next[maxm],w[maxm];
    int first[maxn];
    LL d[maxn];
    int e;
    int temp[maxn];
    int re[maxn];
    
    void init()
    {
        e = 0;
        memset(first,-1,sizeof(first));
    }
    
    void add_edge(int a,int b,int c)
    {
        v[e] = b;next[e] = first[a];w[e] = c;first[a] = e++;
    }
    
    void dij(int src)
    {
        priority_queue <pii,vector<pii>,greater<pii> > q;
        memset(d,-1,sizeof(d));
        d[src] = 0;
        q.push(make_pair(0,src));
        while(!q.empty()){
            int u = q.top().second;
            q.pop();
            for(int i = first[u];i != -1;i = next[i]){
                if(d[v[i]] == -1 || d[v[i]] > d[u]+w[i]){
                    temp[v[i]] = u;
                    d[v[i]] = d[u]+w[i];
                    q.push(make_pair(d[v[i]],v[i]));
                }
            }
        }
    }
    
    int main(){
          int m,n;cin >> m >> n;
          int a ,b,c;
            init();
            for(int i = 0;i < n;i++){
             cin >> a >> b >> c;
                add_edge(a,b,c);
                add_edge(b,a,c);
            }
            dij(1);
            int p = 0,now = m;
            if(d[m] == -1)
            {
                cout << "-1" << endl;
                return 0;
            }
    
            re[p++] = m;
            while(now != 1)
            {
                now = temp[now];
                re[p++] = now;
            }
            for(int i = p-1;i >0;i--)
            {
    
                cout << re[i] << " ";
            }
            cout << re[0] << endl;
        return 0;
    }
  • 相关阅读:
    PAT 乙级 1041 考试座位号(15) C++版
    四、Shell输入、输出功能和字符颜色设置
    三、Shell变量类型和运算符
    Shell文件权限和脚本执行
    Spark Standalone
    Loadrunner安装
    kali 2.0源更新
    xmanager远程桌面连接Linux
    Linux--文件查找命令
    Linux下MySQL忘记密码
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3888307.html
Copyright © 2011-2022 走看看