码迷,mamicode.com
首页 > 其他好文 > 详细

bzoj 1003: [ZJOI2006]物流运输trans 最短路+dp

时间:2016-02-26 13:50:13      阅读:227      评论:0      收藏:0      [点我收藏+]

标签:

题目链接

1003: [ZJOI2006]物流运输trans

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 5246  Solved: 2157
[Submit][Status][Discuss]

Description

物流公司要把一批货物从码头A运到码头B。由于货物量比较大,需要n天才能运完。货物运输过程中一般要转停好几个码头。物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪。由于各种因素的存在,有的时候某个码头会无法装卸货物。这时候就必须修改运输路线,让货物能够按时到达目的地。但是修改路线是一件十分麻烦的事情,会带来额外的成本。因此物流公司希望能够订一个n天的运输计划,使得总成本尽可能地小。

Input

第一行是四个整数n(1<=n<=100)、m(1<=m<=20)、K和e。n表示货物运输所需天数,m表示码头总数,K表示每次修改运输路线所需成本。接下来e行每行是一条航线描述,包括了三个整数,依次表示航线连接的两个码头编号以及航线长度(>0)。其中码头A编号为1,码头B编号为m。单位长度的运输费用为1。航线是双向的。再接下来一行是一个整数d,后面的d行每行是三个整数P( 1 < P < m)、a、b(1 < = a < = b < = n)。表示编号为P的码头从第a天到第b天无法装卸货物(含头尾)。同一个码头有可能在多个时间段内不可用。但任何时间都存在至少一条从码头A到码头B的运输路线。

Output

包括了一个整数表示最小的总成本。总成本=n天运输路线长度之和+K*改变运输路线的次数。

Sample Input

5 5 10 8
1 2 1
1 3 3
1 4 2
2 3 2
2 4 4
3 4 1
3 5 2
4 5 2
4
2 2 3
3 1 1
3 3 3
4 4 5

Sample Output

32

HINT

 

前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)*3+(3+2)*2+10=32

 
 
n^2枚举出[i, j]天内, 从1到n的最短路, 然后进行dp。 dp[i] = min(dp[i], dp[j-1]+dis[j][i]+k), 具体看代码, 很好理解。
#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int maxn = 1e5+5;
int head[maxn*2], num, n, m, k, E, d, dis[maxn], val[105][105], dp[maxn], vis[maxn];
struct node
{
    int to, nextt, w;
}e[maxn*2];
struct xx
{
    int l, r, pos;
}a[maxn];
void add(int u, int v, int w) {
    e[num].to = v, e[num].nextt = head[u], e[num].w = w, head[u] = num++;
}
int can[25];
void init() {
    num = 0;
    mem1(head);
}
void check(int l, int r) {
    mem(can);
    for(int i = 0; i<d; i++) {
        if(a[i].l>r || a[i].r<l)
            continue;
        can[a[i].pos] = 1;
    }
}
int spfa() {
    queue <int> q;
    mem2(dis);
    mem(vis);
    dis[1] = 0, vis[1] = 1;
    q.push(1);
    while(!q.empty()) {
        int u = q.front(); q.pop();
        vis[u] = 0;
        for(int i = head[u]; ~i; i = e[i].nextt) {
            int v = e[i].to;
            if(can[v])
                continue;
            if(dis[v]>dis[u]+e[i].w) {
                dis[v] = dis[u]+e[i].w;
                if(!vis[v]) {
                    vis[v] = 1;
                    q.push(v);
                }
            }
        }
    }
    return dis[m];
}
int main()
{
    int x, y, z;
    cin>>n>>m>>k>>E;
    init();
    for(int i = 0; i<E; i++) {
        scanf("%d%d%d", &x, &y, &z);
        add(x, y, z);
        add(y, x, z);
    }
    cin>>d;
    for(int i = 0; i<d; i++) {
        scanf("%d%d%d", &a[i].pos, &a[i].l, &a[i].r);
    }
    mem2(val);
    for(int i = 1; i<=n; i++) {
        for(int j = i; j<=n; j++) {
            check(i, j);
            val[i][j] = spfa();
        }
    }
    mem2(dp);
    dp[0] = 0;
    for(int i = 1; i<=n; i++) {
        for(int j = 1; j<=i; j++) {
            if(val[j][i] >= inf || dp[j-1] >= inf)
                continue;
            dp[i] = min(dp[i], dp[j-1]+val[j][i]*(i-j+1)+k);
        }
    }
    cout<<dp[n]-k<<endl;
    return 0;
}

 

  

bzoj 1003: [ZJOI2006]物流运输trans 最短路+dp

标签:

原文地址:http://www.cnblogs.com/yohaha/p/5219974.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!