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;
    }
  • 相关阅读:
    pexpect库学习之包装类详解
    spawn类expect方法详解
    spawn类参数command详解
    Django中如何实现数据库路由?
    Memcached中的存取命令详解
    Javascript的参数详解
    Python中Paramiko协程方式详解
    Greenlets间如何实现互相通信?
    Gevent中的同步与异步详解
    jQuery正则的使用方法步骤详解
  • 原文地址:https://www.cnblogs.com/hhhhhhhhhhhhhhhhhhhhhhhhhhh/p/3888301.html
Copyright © 2011-2022 走看看