bzoj1003: [ZJOI2006] transport stream (shortest + DP)

topic:

1003: [ZJOI2006] logistics and transport

Resolution:

Shortest + DP
we \ (no [i] [j ] \) to indicate \ (I \) In the \ (J \) days after not
using \ (cost [i] [j ] \) represents \ (i \) to day \ (j \) to spend days
in the most short circuit judge for the first \ (i \) to day \ (j \) days in which the pier can not walk, doing shortest jump over

Minimum cost last set f [i] denotes the i-th day of the
transfer equation
\ (f [i] = min (f [i], f [j] + k + cost [j + 1] [i] * (ij) ) \)

Code:

#include <bits/stdc++.h>

using namespace std;

const int N = 1010;
const int INF = 0x3f3f3f3f;

int n, m, k, E, num, t;
int head[N], dis[N];

long long f[N], cost[N][N];

bool vis[N], no[N][N], mark[N];

struct node {
    int v, nx, w;
} e[N];

struct edge {
    int id, dis;
    bool operator <(const edge &oth) const {
        return dis > oth.dis;
    }
};

inline void add(int u, int v, int w) {
    e[++num] = (node) {v, head[u], w}, head[u] = num;
}

priority_queue<edge>q;
void dijkstra(int s, int from, int to) {
    memset(dis, INF, sizeof dis);
    memset(vis, 0, sizeof vis);
    memset(mark, 0, sizeof mark);
    for (int i = 1; i <= m; ++i)
        for (int j = from; j <= to; ++j)
            if (no[i][j]) mark[i] = 1;
    dis[s] = 0; 
    q.push((edge) {s, 0});
    while (!q.empty()) {
        int u = q.top().id;
        q.pop();
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u]; ~i; i = e[i].nx) {
            int v = e[i].v;
            if (mark[v]) continue;
            if (dis[v] > dis[u] + e[i].w) 
                q.push((edge) {v, dis[v] = dis[u] + e[i].w});
        }
    }
}

signed main() {
    memset(head, -1, sizeof head);
    cin >> n >> m >> k >> E;
    for (int i = 1, x, y, z; i <= E; ++i) {
        cin >> x >> y >> z;
        add(x, y, z), add(y, x, z);
    }
    cin >> t;
    for (int i = 1, x, y, z; i <= t; ++i) {
        cin >> x >> y >> z;
        for (int j = y; j <= z; ++j) no[x][j] = 1;
    }
    for (int i = 1; i <= n; ++i)
        for (int j = i; j <= n; ++j) {
            dijkstra(1, i, j);
            cost[i][j] = dis[m];
        }
    for (int i = 1; i <= n; ++i) ;
    for (int i = 1; i <= n; ++i) {
        f[i] = cost[1][i] * i;
        for (int j = 1; j <= i; ++j) {
            f[i] = min(f[i], f[j] + k + cost[j + 1][i] * (i - j));
        }
    }
    cout << f[n];
}

Guess you like

Origin www.cnblogs.com/lykkk/p/11347171.html