zoukankan      html  css  js  c++  java
  • [JLOI2011] 飞行路线

    [题目链接]

              https://www.lydsy.com/JudgeOnline/problem.php?id=2763

    [算法]

             分层图最短路

    [代码]

           

    #include<bits/stdc++.h>
    using namespace std;
    #define MAXN 10010
    #define MAXM 50010
    #define MAXK 15
    const int inf = 2e9;
    
    struct edge
    {
            int to,w,nxt;
    } e[MAXM << 1];
    
    int n,m,k,s,t,tot;
    int head[MAXN];
    
    template <typename T> inline void read(T &x)
    {
        int f = 1; x = 0;
        char c = getchar();
        for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; 
        for (; isdigit(c); c = getchar()) x = (x << 3) + (x << 1) + c - '0';
        x *= f;
    }
    inline void addedge(int u,int v,int w)
    {
            tot++;
            e[tot] = (edge){v,w,head[u]};
            head[u] = tot;
    }
    inline int dijkstra()
    {
            priority_queue< pair<int,pair<int,int> > > q;
            pair< int,pair<int,int> > cur;
            static int dist[MAXN][MAXK];
            static bool visited[MAXN][MAXK];
            for (int i = 0; i < n; i++)
            {
                    for (int j = 0; j <= k; j++)
                    {
                            dist[i][j] = inf;
                            visited[i][j] = false;
                    }
            }
            dist[s][0] = 0;
            q.push(make_pair(0,make_pair(s,0)));
            while (!q.empty())
            {
                    cur = q.top();
                    q.pop();
                    if (visited[cur.second.first][cur.second.second]) continue;
                    visited[cur.second.first][cur.second.second] = true;
                    for (int i = head[cur.second.first]; i; i = e[i].nxt)
                    {
                          int v = e[i].to , w = e[i].w;
                          if (cur.second.second + 1 <= k && dist[cur.second.first][cur.second.second] < dist[v][cur.second.second + 1])
                          {
                                  dist[v][cur.second.second + 1] = dist[cur.second.first][cur.second.second];
                                  q.push(make_pair(-dist[v][cur.second.second + 1],make_pair(v,cur.second.second + 1)));
                            }
                            if (dist[cur.second.first][cur.second.second] + w < dist[v][cur.second.second])
                            {
                                    dist[v][cur.second.second] = dist[cur.second.first][cur.second.second] + w;
                                    q.push(make_pair(-dist[v][cur.second.second],make_pair(v,cur.second.second)));
                            }
                    }
            }
            int ans = inf;
            for (int i = 0; i <= k; i++) ans = min(ans,dist[t][i]);
            return ans;
    }
    
    int main() 
    {
            
            read(n); read(m); read(k); read(s); read(t);
            for (int i = 1; i <= m; i++)
            {
                    int u,v,w;
                    read(u); read(v); read(w);
                    addedge(u,v,w);
                    addedge(v,u,w); 
            }
            printf("%d
    ",dijkstra());
            
            return 0;
        
    }
  • 相关阅读:
    android 代码上传到jcenter
    android library打包成aar形式供别的项目引用
    使用AndroidStudio导入github项目
    使用Postman在Chrome下进行rest请求测试
    Android 编码规范
    GsonFormat根据返回值json快速构建Model
    码云git使用四(分支的创建,使用和合并)
    码云git使用三(本地代码合并)
    码云git使用二(从码云git服务器上下载到本地)
    计算最长英语单词链
  • 原文地址:https://www.cnblogs.com/evenbao/p/9502237.html
Copyright © 2011-2022 走看看