题解:P11860 [CCC 2025 Senior] 熔岩路 / Floor is Lava
思路
M = N - 1
显然该图为树,两点之间只有一条简单路径,不存在选择,直接模拟即可。
1 \le c_i \le 10
因为到达一个点时只有有限种状态,而在这个点的状态又会影响接下来的事件,所以可以进行拆点,或者叫分层图。即把一个点的每一种状态抽象成新图上的一个点,在状态之间转移的代价抽象成新图上的边与边权。
在这里我们将在一个点时不同的冷却等级拆分成不同的点,显然一个点能拆出
于是在新图上跑最短路即可。
无特殊限制
大体思路还是拆点,只不过
但是如果在拆出的
代码实现
为了编码的简便使用了 std::map 和 std::set,由于时间限制比较宽松所以无伤大雅。
Code:
#include <bits/stdc++.h>
#define inf (0x3f3f3f3f)
#define INF (INFINITY)
#define LIMIT (INT_MAX)
// #define LL
// #define getchar getchar_unlocked
#ifdef LL
#define int long long
#undef inf
#define inf (0x3f3f3f3f3f3f3f3f)
#undef LIMIT
#define LIMIT (LLONG_MAX)
#endif
#define pb push_back
#define ps push
#define lowbit(x) ((x) & (-(x)))
#define ls (i << 1)
#define rs (i << 1 | 1)
#define fill(x, y) memset(x, y, sizeof(x))
#define heap priority_queue<int, vector<int>, greater<int>>
#define pow2(x) (1 << (x))
#define changeif(a, b, c) ((a) == (b) ? (c) : (a))
#define addedge(u, v) a[u].pb(v); a[v].pb(u)
#define addedgew(u, v, w) a[u].pb(edge(v, w)); a[v].pb(edge(u, w))
#define aminos printf("debug : in line %d\n", __LINE__)
// #define f(x, y) ((m) * ((x) - 1) + (y))
#define kth(x, y) (x & (1 << (y - 1)))
#define kth1(x) (1 << (x - 1))
#define all1(x) ((1 << (x)) - 1)
#define gcd(x, y) (1ll * x * y == 0 ? x + y : __gcd((x), (y)))
#define lcm(x, y) ((x) / gcd((x), (y)) * (y))
#define chkmax(x, y) ((x) = max((x), (y)))
#define chkmin(x, y) ((x) = min((x), (y)))
#define add(x, y) (((x) + (y)) % mod)
#define addto(x, y) ((x) = add((x), (y)))
#define minus(x, y) (((x) - (y) + (mod)) % (mod))
#define minusto(x, y) ((x) = minus((x), (y)))
#define times(x, y) ((1ll * (x) * (y)) % (mod))
#define timesto(x, y) ((x) = times((x), (y)))
#define divide(x, y) (times(x, inv(y)))
#define divideto(x, y) ((x) = divide((x), (y)))
#define isnumber(x) (((x) >= '0') && ((x) <= '9'))
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template<class T> inline T read(T &x) {
int f = 1; x = 0; char s = getchar();
while(!isnumber(s)) {if(s == '-') f = -1; s = getchar();}
while(isnumber(s)) x = (x << 1) + (x << 3) + (s ^ '0'), s = getchar();
return x *= f;
}
inline int read(char* s) {return scanf("%s", s);}
inline double read(double &x) {scanf("%lf", &x); return x;}
template<class T> inline void print(T x, char suf = 0, int base = 10) {
if(x < 0) {putchar('-'); x = -x;}
if(x >= base) print(x / base, 0, base);
putchar(48 ^ (x % base));
if(suf) putchar(suf);
}
template<class T> inline void printarr(T* a, int n, int off = 0, int base = 10) {
int i; for(i = 1 ; i + 8 <= n ; i += 8) {
print(a[i+off], ' ', base); print(a[i+1+off], ' ', base);
print(a[i+2+off], ' ', base); print(a[i+3+off], ' ', base);
print(a[i+4+off], ' ', base); print(a[i+5+off], ' ', base);
print(a[i+6+off], ' ', base); print(a[i+7+off], ' ', base);
}
while(i <= n) {print(a[i+off], ' ', base); i++;}
putchar('\n');
}
template<class T> inline void readarr(T* a, int n) {
int i; for(i = 1 ; i + 8 <= n ; i += 8) {
read(a[i]); read(a[i+1]);
read(a[i+2]); read(a[i+3]);
read(a[i+4]); read(a[i+5]);
read(a[i+6]); read(a[i+7]);
}
while(i <= n) {read(a[i]); i++;}
}
int cx = 1;
const int N = 2e5 + 5;
int n, m;
struct edge {
int to, w;
edge() {}
edge(int a, int b) {
to = a, w = b;
}
};
vector<edge> a[N], g[N<<2];
int cnt, d[N<<2];
map<int, int> mp[N];
set<int> s[N];
struct node {
int d, u;
node() {}
node(int a, int b) {
d = a, u = b;
}
};
void dijkstra(vector<edge>* a, int* dis, int sizeofdis, int s) {
auto cmp = [](node a, node b) -> bool {
return a.d > b.d;
};
priority_queue<node, vector<node>, decltype(cmp)> q(cmp);
memset(dis, 0x3f, sizeofdis);
dis[s] = 0;
q.ps(node(0, s));
while(q.size()) {
node now = q.top();
q.pop();
int u = now.u, d = now.d;
for(edge e : a[u]) {
int v = e.to, w = e.w;
if(d + w < dis[v]) {
dis[v] = d + w;
q.ps(node(dis[v], v));
}
}
}
}
void solve() {
read(n), read(m);
for(int i = 1 ; i <= m ; i++) {
int u, v, w;
read(u), read(v), read(w);
addedgew(u, v, w);
s[u].insert(w);
s[v].insert(w);
}
s[1].insert(0);
vector<int> vec;
for(int u = 1 ; u <= n ; u++) {
vec.clear();
for(int c : s[u]) {
mp[u][c] = ++cnt;
vec.pb(c);
}
for(int i = 0 ; i < vec.size() - 1 ; i++) {
int x = mp[u][vec[i]], y = mp[u][vec[i+1]], z = vec[i+1] - vec[i];
g[x].pb(edge(y, z));
g[y].pb(edge(x, z));
}
}
for(int u = 1 ; u <= n ; u++) {
for(edge e : a[u]) {
int v = e.to, w = e.w;
int x = mp[u][w], y = mp[v][w], z = 0;
g[x].pb(edge(y, z));
}
}
dijkstra(g, d, sizeof(d), 1);
int ans = inf;
for(int c : vec) {
chkmin(ans, d[mp[n][c]]);
}
print(ans, '\n');
}
signed main() {
// int st_t = clock();
// read(cx);
// init();
while(cx--) {
solve();
}
// cerr << clock() - st_t << "ms" << endl;
return 0;
}