BZOJ 1003. [ZJOI2006]物流运输
$cost[i][j]$ 表示第 $i$ 天到第 $j$ 天都走同一条路线时每天的最小花费,即为 $1$ 到 $m$ 的最短路。dijkstra即可。
然后 $dp[i]$ 表示到第 $i$ 天的最小花费
$dp[i] = min(dp[j] + cost[j + 1][i] * (i - j) + k)$
#include <bits/stdc++.h> #define pii pair<int, int> #define fi first #define se second using namespace std; const int INF = 10000000; const int N = 110; int cost[N][N], dp[N], n, m, k, e; vector<pii> G[N]; bool ban[N][N], unable[N], done[N]; int dis[N]; inline void checkmin(int &a, int b) { if (a > b) a = b; } int dijkstra(int l, int r) { for (int i = 1; i <= m; i++) { unable[i] = 0; done[i] = 0; dis[i] = INF; for (int j = l; j <= r; j++) unable[i] |= ban[j][i]; } if (unable[1] || unable[m]) return INF; priority_queue<pii, vector<pii>, greater<pii> > que; dis[1] = 0; que.push(pii(0, 1)); while (!que.empty()) { auto pp = que.top(); que.pop(); int u = pp.se; if (done[u]) continue; done[u] = 1; for (auto p: G[u]) { int v = p.se, c = p.fi; if (!unable[v] && dis[v] > dis[u] + c) { dis[v] = dis[u] + c; que.push(pii(dis[v], v)); } } } return dis[m]; } int main() { //freopen("in.txt", "r", stdin); scanf("%d%d%d%d", &n, &m, &k, &e); for (int u, v, c; e--; ) { scanf("%d%d%d", &u, &v, &c); G[u].push_back(pii(c, v)); G[v].push_back(pii(c, u)); } int q; scanf("%d", &q); for (int u, a, b; q--; ) { scanf("%d%d%d", &u, &a, &b); for (int i = a; i <= b; i++) ban[i][u] = 1; } for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) cost[i][j] = dijkstra(i, j); for (int i = 1; i <= n; i++) { dp[i] = cost[1][i] * i; for (int j = 1; j < i; j++) checkmin(dp[i], dp[j] + cost[j + 1][i] * (i - j) + k); } printf("%d\n", dp[n]); return 0; }