[BZOJ 1579] Revamping Trails
[题目链接]
https://www.lydsy.com/JudgeOnline/problem.php?id=1579
[算法]
dist[u][k]表示当前在点u,升级了k条道路,最短路径的长度
dijkstra即可
[代码]
#include<bits/stdc++.h> using namespace std; #define MAXN 100010 #define MAXM 50010 #define MAXK 25 int i,n,m,k,u,v,w,tot; int dist[MAXN][MAXK]; int head[MAXN]; struct edge { int to,w,nxt; } e[MAXM << 1]; inline void addedge(int u,int v,int w) { tot++; e[tot] = (edge){v,w,head[u]}; head[u] = tot; } inline void dijkstra() { int i,v,w; static bool visited[MAXN][MAXK]; pair<int,int> cur; priority_queue< pair<int,pair<int,int> > > q; memset(visited,false,sizeof(visited)); memset(dist,0x3f,sizeof(dist)); dist[1][0] = 0; q.push(make_pair(0,make_pair(1,0))); while (!q.empty()) { cur = q.top().second; q.pop(); if (visited[cur.first][cur.second]) continue; visited[cur.first][cur.second] = true; for (i = head[cur.first]; i; i = e[i].nxt) { v = e[i].to; w = e[i].w; if (dist[cur.first][cur.second] + w < dist[v][cur.second]) { dist[v][cur.second] = dist[cur.first][cur.second] + w; q.push(make_pair(-dist[v][cur.second],make_pair(v,cur.second))); } if (cur.second < k && dist[cur.first][cur.second] < dist[v][cur.second + 1]) { dist[v][cur.second + 1] = dist[cur.first][cur.second]; q.push(make_pair(-dist[v][cur.second + 1],make_pair(v,cur.second + 1))); } } } } int main() { scanf("%d%d%d",&n,&m,&k); for (i = 1; i <= m; i++) { scanf("%d%d%d",&u,&v,&w); addedge(u,v,w); addedge(v,u,w); } dijkstra(); printf("%d\n",dist[n][k]); return 0; }