BZOJ 1003 物流运输

最短路+dp

数据很小,我们随便怎么乱搞。
要求最小花费,且有的时间段某个点不能用,那我们首先就可以想到暴力跑最短路求出某个时间段A到B的最短路,这样得到的值即为这个时间段不换航线从起点到终点的最小花费(如果在这个时间段所有点不能用,得到的值为INF,也就是说,当我们求出合法的最短路径的时候,应该还原[某时间段不换航线从起点到终点最小花费]的定义。INF的话不需要维护)
求出来的值我们用cost数组维护,换句话说cost[i][j]表示i到j这个时间段不换航线的情况下的最小花费
然后我门可以巧妙地发现这题就是个dp!
dp[i]表示前i天的最小花费
那么状态转移方程就是

dp[i] = min(dp[j] + cost[i+1][j] + k)

#include <bits/stdc++.h>
#define INF 0x3f3f3f3f
#define full(a, b) memset(a, b, sizeof a)
using namespace std;
typedef long long ll;
inline int lowbit(int x){ return x & (-x); }
inline int read(){
    int X = 0, w = 0; char ch = 0;
    while(!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
    while(isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
    return w ? -X : X;
}
inline int gcd(int a, int b){ return a % b ? gcd(b, a % b) : b; }
inline int lcm(int a, int b){ return a / gcd(a, b) * b; }
template<typename T>
inline T max(T x, T y, T z){ return max(max(x, y), z); }
template<typename T>
inline T min(T x, T y, T z){ return min(min(x, y), z); }
template<typename A, typename B, typename C>
inline A fpow(A x, B p, C lyd){
    A ans = 1;
    for(; p; p >>= 1, x = 1LL * x * x % lyd)if(p & 1)ans = 1LL * x * ans % lyd;
    return ans;
}
const int N = 30;
int n, m, k, e, cnt, head[N], cost[N<<5][N<<5], dist[N];
ll dp[N<<5];
bool unuser[N][N<<5], vis[N], dont[N];
struct Edge { int v, next, w; } edge[N<<4];

void addEdge(int a, int b, int w){
    edge[cnt].v = b, edge[cnt].w = w, edge[cnt].next = head[a], head[a] = cnt ++;
}

int dijkstra(int l, int r){
    full(vis, false);
    full(dist, INF);
    full(dont, false);
    for(int i = 1; i <= m; i ++){
        for(int j = l; j <= r; j ++){
            dont[i] |= unuser[i][j];
        }
    }
    priority_queue< pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
    dist[1] = 0;
    pq.push(make_pair(dist[1], 1));
    while(!pq.empty()){
        int s = pq.top().second, d = pq.top().first; pq.pop();
        if(vis[s]) continue;
        vis[s] = true;
        for(int i = head[s]; i != -1; i = edge[i].next){
            int u = edge[i].v;
            if(dont[u]) continue;
            if(dist[u] > d + edge[i].w){
                dist[u] = d + edge[i].w;
                pq.push(make_pair(dist[u], u));
            }
        }
    }
    return dist[m];
}

int main(){

    full(head, -1);
    n = read(), m = read(), k = read(), e = read();
    for(int i = 0; i < e; i ++){
        int u = read(), v = read(), w = read();
        addEdge(u, v, w), addEdge(v, u, w);
    }
    int d = read();
    for(int i = 0; i < d; i ++){
        int x = read(), l = read(), r = read();
        for(int j = l; j <= r; j ++) unuser[x][j] = true;
    }
    for(int i = 1; i <= n; i ++){
        for(int j = 1; j <= n; j ++){
            cost[i][j] = dijkstra(i, j);
            //cout << cost[i][j] << endl;
            //for(int l = 1; l <= m; l ++) cout << dist[l] << " ";
            //cout << endl;
            if(cost[i][j] != INF) cost[i][j] *= (j - i + 1);
        }
    }
    for(int i = 1; i <= n; i ++){
        dp[i] = cost[1][i];
        for(int j = 1; j < i; j ++){
            dp[i] = min(dp[i], dp[j] + k + cost[j + 1][i]);
        }
    }
    printf("%lld\n", dp[n]);
    return 0;
}
posted @ 2019-04-04 23:23  清楚少女ひなこ  阅读(91)  评论(0编辑  收藏  举报