Logistics and transport trans "ZJOI2006"

Description [title]
logistics company take shipment from the dock \ (A \) transported to the docks \ (B \) . As the goods than larger, we need to \ (n \) days to complete operation. Transportation of goods in general to turn several parked pier. Logistics companies often design a fixed transport routes for the implementation of strict management and tracking of the entire transport process. Due to various factors, sometimes a pier will not load and unload cargo. This time we have to change transport routes, so that goods can reach their destinations on time. But revised route is a very troublesome thing, will bring additional costs. Therefore, the logistics company hopes to set a \ (n \) days of the transportation plan, so that the total cost as low as possible.

[Input format
first line four integer \ (n (1 \ leq n \ leq 100), m (1 \ leq m \ leq 20), K \) and \ (E \) . \ (n-\) represents the number of days required for the transport of goods, \ (m \) represents the total pier, \ (K \) represents the cost of modifications required per transport routes. Next, \ (E \) lines each one route is described, comprising the three integers, respectively for the two terminals and the number of routes between route length \ ((> 0) \) . Wherein Harbor \ (A \) numbered \ (1 \) , the terminal \ (B \) No. \ (m \) . Transportation costs per unit length \ (1 \) . Route in both directions. The next line is an integer \ (D \) , behind \ (D \) lines each is an integer of three \ (P (1 <P < m), a, b (1 \ leq a \ leq b \ the n-Leq) \) . That number is \ (P \) pier from \ (A \) to day \ (B \)Day can not load and unload cargo (including head and tail). With a marina, possibly in multiple time periods is not available. However, any time there is at least one from the dock \ (A \) to the terminal \ (B \) transport routes.

[] Output format
includes an integer representing the minimum total cost. Total cost of the day path length = n + K * and the transport number of transportation routes change.

[Problem] Solutions
readily be seen that this problem DP.
Set \ (f [i] \) representing the forward \ (I \) minimum cost days. The enumeration \ (J \) represents the section \ (J \) days program change, there are \ (f [i] = min (f [j] + K + cost (j + 1, i) * (ij)) \) , \ (cost (i, J) \) represents the \ (ij \) days due to spend a day in the best possible solutions.
\ (cost (i, j) \) is the shortest path can be determined. Each enumeration restrictions, if a dock \ (k \) date and can not use \ (ij \) intersect, the flag can not be used as the pier (the \ (vis [k] \) is set to \ (1 \) ). You can then use the shortest calculated for each \ (cost (I, J) \) .
Note from the \ (f [0] \) do not add when transferring \ (K \) .

[Code]

#include <iostream>
#include <cstdio>
#include <queue>
#define re register
using namespace std;
typedef long long ll;

ll n, m, kk, e, d;
ll head[10007], pre[20007], to[20007], dis[20007], len, ans[20007];
ll cost[103][103], p[100007], a[100007], b[100007], dp[100007];
bool vis[10007];

ll read() {
    ll ret = 0;
    char ch = getchar();
    while (ch > '9' || ch < '0') {
        ch = getchar();
    }
    while (ch <= '9' && ch >= '0') {
        ret = ret * 10 + ch - '0';
        ch = getchar();
    }
    return ret;
}

void insert(ll u, ll v, ll w) {
    len++;
    to[len] = v, pre[len] = head[u], dis[len] = w, head[u] = len;
}

void dijkstra() {
    priority_queue< pair<ll, ll> > q;
    q.push(make_pair(0, 1));
    ans[1] = 0;
    while (!q.empty()) {
        ll c = q.top().second;
        q.pop();
        if (vis[c]) continue;
        vis[c] = true;
        for (re ll i = head[c]; i != 0; i = pre[i]) {
            if (ans[c] + dis[i] < ans[to[i]]) {
                ans[to[i]] = ans[c] + dis[i];
                q.push(make_pair(-ans[to[i]], to[i]));
            }
        }
    }
}

int main() {
    n = read(), m = read(), kk = read(), e = read();
    for (ll i = 1; i <= e; i++) {
        ll u, v, w;
        u = read(), v = read(), w = read();
        insert(u, v, w);
        insert(v, u, w);
    }
    d = read();
    for (ll i = 1; i <= d; i++) {
        p[i] = read(), a[i] = read(), b[i] = read();
    }
    for (ll i = 1; i <= n; i++) for (ll j = i; j <= n; j++) {
        for (ll k = 1; k <= m; k++) vis[k] = 0;
        for (ll k = 1; k <= m; k++) ans[k] = 0x7fffffff;
        for (ll k = 1; k <= d; k++) if ((a[k] >= i && a[k] <= j) || (b[k] >= i && b[k] <= j) || (a[k] <= i && b[k] >= j)) vis[p[k]] = 1;
        dijkstra();
        cost[i][j] = ans[m];
        //cout << i << " " << j << ": " <<cost[i][j] << endl;
    }
    for (ll i = 1; i <= n; i++) {
        dp[i] = 0x7fffffff;
        for (ll j = 0; j < i; j++) {
            if (cost[j + 1][i] == 0x7fffffff) continue;
            if (j != 0) dp[i] = min(dp[j] + cost[j + 1][i] * (i - j) + kk, dp[i]); 
            else dp[i] = min(dp[j] + cost[j + 1][i] * (i - j), dp[i]); 
        }
    }
    printf("%lld\n", dp[n]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/ak-dream/p/AK_DREAM5.html