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

    这个题目意思我给弄错了。真桑心没好好听TK的指导,只要在松弛操作里头记录前驱节点就可以了,还要注意long long,其实我有很多时候测试错了可以猜到要用long long才可以,但是不知道缘故,就单单怎么算,不是很了解。

    #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;
    }
  • 相关阅读:
    VS2010/MFC编程入门之四(MFC应用程序框架分析)
    VS2010/MFC编程入门之三(VS2010应用程序工程中文件的组成结构)
    shell脚本学习一
    docker安装rabbitmq
    docker安装activemq
    docker搭建数据库主从复制
    在docker容器中安装vim命令进行编辑文件
    docker的常用操作
    docker上安装eslaticsearch
    centos7上面关闭防火墙
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3888301.html
Copyright © 2011-2022 走看看