zoukankan      html  css  js  c++  java
  • 2018南京网络赛L题:Magical Girl Haze(最短路分层图)

    题目链接:https://nanti.jisuanke.com/t/31001


    解题心得:

    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <vector>
    using namespace std;
    typedef long long ll;
    typedef pair<ll,ll> P;
    const int maxn = 1e6+100;
    const ll INF = 1e12;
    
    ll n, m, k, S, T;
    
    struct edge {
        ll to, len;
    
        edge(ll to1, ll len1):
                to(to1), len(len1){};
    };
    
    struct NODE {
        ll len, pos, c;
    
        bool friend operator < (NODE a, NODE b) {
            return a.len > b.len;
        }
    
        NODE (ll len1, ll pos1, ll c1):
                len(len1), pos(pos1), c(c1){};
    };
    
    vector <edge> ve[maxn];
    
    ll dis[11][maxn];
    
    void add_edge(ll u, ll v, ll len) {
        ve[u].push_back(edge(v, len));
    }
    
    void init() {
        for(int i=0;i<11;i++)
            for(int j=0;j<maxn;j++)
                dis[i][j] = INF;
    
        scanf("%lld%lld%lld",&n,&m,&k);
        for(int i=0;i<=n;i++)
            ve[i].clear();
        S = 1, T = n;
        for(int i=0;i<m;i++) {
            ll u, v, len;
            scanf("%lld%lld%lld",&u, &v, &len);
            add_edge(u, v, len);
        }
    }
    
    void dij() {
        priority_queue <NODE> qu;
        qu.push(NODE(0, S, 0));
        dis[0][S] = 0;
        while(!qu.empty()) {
            NODE now = qu.top(); qu.pop();
            ll u = now.pos;
            for(int i=0;i<ve[u].size();i++) {
                ll v = ve[u][i].to;
                ll len = ve[u][i].len;
                ll c = now.c;
                if(dis[c][v] > dis[c][u]+len) {
                    dis[c][v] = dis[c][u] + len;
                    qu.push(NODE(dis[c][v], v, c));
                }
            }
    
            ll c = now.c;
            if(c < k) {
                for (int i = 0; i < ve[u].size(); i++) {
                    ll v = ve[u][i].to;
                    if(dis[c+1][v] > dis[c][u]) {
                        dis[c+1][v] = dis[c][u];
                        qu.push(NODE(dis[c][u], v, c+1));
                    }
                }
            }
        }
    }
    
    void min_path() {
        dij();
        ll Min = INF;
        for(int i=0;i<=k;i++) {
            Min = min(Min, dis[i][T]);
        }
        printf("%lld
    ", Min);
    }
    
    int main() {
        int t;
        scanf("%d",&t);
        while(t--) {
            init();
            min_path();
        }
    }

  • 相关阅读:
    Markdown
    异异还原
    程序和算法
    运算符
    Java复习1
    复习总结
    为什么Byte是8位,但是却只能表示到127,而不是255?
    笔记的认识
    笔记本触摸板
    电脑热键
  • 原文地址:https://www.cnblogs.com/GoldenFingers/p/9574306.html
Copyright © 2011-2022 走看看