Luo Gu P4822 [BJWC2012] Freeze
topic:
- FIG hierarchical template ah,
I really lazy, Topics are too lazy to stick up (anyway, with almost exactly the same template
- How this problem is purple title ..
We should not be green
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define N 55 * 52
#define M 2005 * 52
using namespace std;
struct Node
{
int val, pos;
friend bool operator < (Node x, Node y) {
return x.val > y.val;
}
};
priority_queue<Node> que;
struct E {int next, to, dis;} e[M];
int n, m, k, num, ans = 0x7fffffff;
int dis[N], h[N];
bool vis[N];
int read()
{
int x = 0; char c = getchar();
while(c < '0' || c > '9') c = getchar();
while(c >= '0' && c <= '9') {x = x * 10 + c - '0'; c = getchar();}
return x;
}
void add(int u, int v, int w)
{
e[++num].next = h[u];
e[num].to = v;
e[num].dis = w;
h[u] = num;
}
int main()
{
cin >> n >> m >> k;
for(int i = 1; i <= m; i++)
{
int u = read(), v = read(), w = read();
add(u, v, w), add(v, u, w);
for(int j = 1; j <= k; j++)
{
add(u + j * n, v + j * n, w);
add(v + j * n, u + j * n, w);
add(u + (j - 1) * n, v + j * n, w / 2);
add(v + (j - 1) * n, u + j * n, w / 2);
}
}
memset(dis, 0x3f, sizeof(dis));
dis[1] = 0, que.push((Node){0, 1});
while(que.size())
{
int now = que.top().pos;
que.pop();
if(vis[now]) continue;
vis[now] = 1;
for(int i = h[now]; i != 0; i = e[i].next)
if(dis[now] + e[i].dis < dis[e[i].to])
{
dis[e[i].to] = dis[now] + e[i].dis;
que.push((Node){dis[e[i].to], e[i].to});
}
}
for(int i = 0; i <= k; i++) ans = min(ans, dis[n + i * n]);
cout << ans;
return 0;
}