[USACO09FEB]Revamping Trails G link
思路:
分层图最短路模板题,具体思路看我上一篇博客->link
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 5e5 + 10;
struct node {int to, val, next;} e[N * 25];
int n, m, k, s, t, a, b, c, cnt, head[N], dis[N];
bool vis[N];
void add(int x, int y, int z) {e[++cnt] = (node){y, z, head[x]}; head[x] = cnt;}
void dij()
{
memset(dis, 0x3f, sizeof(dis));
dis[s] = 0;
priority_queue < pair<int , int>, vector <pair<int , int> >, greater<pair<int , int> > > q;
q.push(make_pair(0, s));
while(!q.empty())
{
int u = q.top().second;
q.pop();
if(!vis[u])
{
vis[u] = 1;
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].to;
if(dis[v] > dis[u] + e[i].val)
{
dis[v] = dis[u] + e[i].val;
q.push(make_pair(dis[v], v));
}
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &k);
s = 1, t = n;
for(int i = 1; i <= m; i++)
{
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
add(b, a, c);
for(int j = 1; j <= k; j++)
{
add(a + (j - 1) * n, b + j * n, 0);
add(b + (j - 1) * n, a + j * n, 0);
add(a + j * n, b + j * n, c);
add(b + j * n, a + j * n, c);
}
}
for(int i = 1; i <= k; i++) add(t + (i - 1) * n, t + i * n, 0);
dij();
printf("%d\n", dis[t + k * n]);
return 0;
}