BZOJ#1003物流运输

image

思路:

动态规划,f[i]表示以i结尾的最短时间,f[i]=min(f[j]+c[j+1][i]*(i-j)+k)
c[i][j]表示从时间i到j的最短路

代码:

#include <bits/stdc++.h>
#define int long long
int _ = 0, Case = 1;
using namespace std;
#define all(v) begin(v),end(v)
#define nline '\n'

const int N = 210;
int f[N];
int c[N][N];
struct T {
    int a, b, w;
} edge[N];
using PII = pair<int, int> ;
vector<PII> h[N];
int Time[N][N];
int n, m, e, k;
int dist[N], vis[N];

int dijkstra(){
    for(int i=1;i<=m;i++) dist[i]=1e8,vis[i]=false;
    dist[1]=0;
    priority_queue<PII,vector<PII>,greater<PII>> q;
    q.push({0,1});
    while(q.size()){
        auto [x,y]=q.top();
        q.pop();
        if(vis[y]) continue;
        vis[y]=true;
        for(auto [id,w]:h[y]){
            if(dist[id]>x+w){
                dist[id]=x+w;
                q.push({dist[id],id});
            }
        }
    }
    return dist[m];
}
void init() {
    for (int len = 1; len <= n; len++) {
        for (int l = 1; l + len - 1 <= n; l++) {
            for (int i = 1; i <= m; i++) {
                h[i].clear();
            }
            int r = l + len - 1;
            for (int k = 1; k <= e; k++) {
                auto[a, b, w] = edge[k];
                bool ok = true;
                for(int i=l;i<=r;i++){
                    if(Time[a][i] or Time[b][i]) ok=false;
                }
                if (ok) {
                    h[a].push_back({b, w});
                    h[b].push_back({a, w});
                }

            }
            c[l][r] = dijkstra();
        }
    }
}
void solve(int Case) {
    cin >> n >> m >> k >> e;
    for (int i = 1; i <= e; i++) {
        auto&[a, b, w] = edge[i];
        cin >> a >> b >> w;
    }
    int d;
    cin >> d;
    for (int i = 1; i <= d; i++) {
        int p, a, b;
        cin >> p >> a >> b;
        for(int j=a;j<=b;j++){
            Time[p][j]=1;
        }
    }
    init();
    memset(f, 0x3f, sizeof f);
    f[0] = 0;
    for (int i = 1; i <= n; i++) {
        for (int j = 0; j < i; j++) {
            f[i] = min(f[i], f[j] + c[j + 1][i] * (i - j) + k);
        }
    }
    cout << f[n] - k << nline;

}

signed main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
//   cin >> _; for (Case = 1; Case <= _; Case++)
    solve(Case);

    return 0;
}
posted @ 2022-03-29 14:11  指引盗寇入太行  阅读(18)  评论(0编辑  收藏  举报