[JLOI2011] 飞行路线
Posted evenbao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[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; }
以上是关于[JLOI2011] 飞行路线的主要内容,如果未能解决你的问题,请参考以下文章
BZOJ 2763: [JLOI2011]飞行路线 spfa dp