码迷,mamicode.com
首页 > 其他好文 > 详细

[JLOI2011] 飞行路线

时间:2018-08-19 20:02:52      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:queue   bsp   class   second   span   str   ret   div   empty   

[题目链接]

          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\n",dijkstra());
        
        return 0;
    
}

 

[JLOI2011] 飞行路线

标签:queue   bsp   class   second   span   str   ret   div   empty   

原文地址:https://www.cnblogs.com/evenbao/p/9502237.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!