1003: [ZJOI2006]物流运输

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 8672  Solved: 3678
[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
//前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)*3+(3+2)*2+10=32

HINT

析:可以先把连续从第 i 天到第 j 天的最短路处理出来,只要在这些天内所有的码头都不关闭,那么所有的最短路就是一样的,然后再dp一下,就OK了。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-3;
const int maxn = 110 + 10;
const int maxm = 3e5 + 10;
const int mod = 1000000007;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} int dp[maxn]; struct Edge{
int to, cost, next;
};
Edge edges[maxn*maxn<<1];
int head[maxn], cnt; void addEdge(int u, int v, int c){
edges[cnt].to = v;
edges[cnt].cost = c;
edges[cnt].next = head[u];
head[u] = cnt++;
} int cost[maxn][maxn];
int day[maxn];
int notcan;
int d[maxn]; int dijkstra(){
priority_queue<P, vector<P>, greater<P> > pq;
ms(d, INF); d[1] = 0;
pq.push(P(0, 1)); while(!pq.empty()){
P p = pq.top(); pq.pop();
int u = p.se;
int c = p.fi;
if(c > d[u]) continue;
for(int i = head[u]; ~i; i = edges[i].next){
int v = edges[i].to;
if(notcan & 1<<v) continue;
if(d[v] > d[u] + edges[i].cost){
d[v] = edges[i].cost + d[u];
pq.push(P(d[v], v));
}
}
}
return d[m];
} int main(){
int K, E; ms(head, -1);
scanf("%d %d %d %d", &n, &m, &K, &E);
while(E--){
int u, v, c;
scanf("%d %d %d", &u, &v, &c);
addEdge(u, v, c);
addEdge(v, u, c);
}
scanf("%d", &E);
ms(day, 0);
while(E--){
int u, a, b;
scanf("%d %d %d", &u, &a, &b);
for(int i = a; i <= b; ++i) day[i] |= 1<<u;
}
for(int i = 1; i <= n; ++i)
for(int j = i; j <= n; ++j){
notcan = 0;
for(int k = i; k <= j; ++k)
notcan |= day[k];
cost[i][j] = dijkstra();
}
ms(dp, INF);
dp[0] = -K;
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= i; ++j)
if(cost[j][i] != INF) dp[i] = min(dp[i], dp[j-1] + cost[j][i] * (i - j + 1) + K);
printf("%d\n", dp[n]);
return 0;
}

  

BZOJ 1003 物流运输 (dp + dijkstra)的更多相关文章

  1. BZoj 1003 物流运输 DP+最短路

    2013-09-11 09:56 W[I]代表前I天能取得的最小花费,假设在第J天更改一次路线,那么如果有 W[I]>W[J]+第j+1到第I天的最小花费+更改路线的花费(K) 那么更新W[I] ...

  2. BZOJ 1003 物流运输 题解 【SPFA+DP】

    BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...

  3. BZOJ 1003 物流运输trans dijstra+dp

    1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3896  Solved: 1608[Submit] ...

  4. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  5. BZOJ 1003 - 物流运输 - [最短路+dp]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...

  6. bzoj 1003物流运输 区间dp+spfa

    基本思路: 一开始确实没什么思路,因为觉得怎么着都会超时,然后看一下数据范围,呵,怎么都不会超时. 思路: 1.看到能改变线路,想到可以用以下区间dp,区间dp的话,先枚举长度,枚举开始位置,然后枚举 ...

  7. BZOJ 1003 物流运输trans

    Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...

  8. [BZOJ]1003 物流运输(ZJOI2006)

    挖坑,日常划水. 从BZOJ上的AC人数来看这题确实不难,但做这种题的常见思路让小C决定还是mark一下. Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才 ...

  9. BZOJ 1003 物流运输

    最短路+dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

随机推荐

  1. python之函数的作用域

    name = "wangyue" def test1(): name= "in the test1" def bar(): name = "zhaoz ...

  2. 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值

    使用XMLHttpRequest对象分为4部完成: 1.创建XMLHttpRequest组建 2.设置回调函数 3.初始化XMLHttpRequest组建 4.发送请求 实例代码: [javascri ...

  3. linux install redis-cli

    ubuntu: sudo apt-get install redis-cli centos: sudo -i yum list redis yum install redis>>> ...

  4. spark cache table

    http://www.07net01.com/2015/11/961118.html http://www.cnblogs.com/charlotte77/p/5468968.html 文本读入和写出 ...

  5. Js 过滤emoji表情...持续补充中..

    原文来自: https://www.cnblogs.com/tsjTSJ/p/7065544.html 最全最详细的用JS过滤Emoji表情的输入   在前端页面开发过程中,总会碰到不允许输入框输入e ...

  6. Maven 基础配置

    pom.xml基础配置: maven中,最让我迷惑的还是那一堆配置! 就拿这个属性配置来说: <properties> <project.build.sourceEncoding&g ...

  7. exception keynote

    [exception keynote] Note that the parentheses around this tuple are required, because except ValueEr ...

  8. count++线程安全与 synchronized对性能影响的测试

    一个计时器,同时开启100个线程,每个线程休眠1ms钟后,将全局静态变量count加1,这100个线程创建完之后,休眠500ms,计算总耗时,程序如下: public class Counter { ...

  9. Python3 range() 函数用法

    Python3 range() 函数用法  Python3 内置函数 Python3 range() 函数返回的是一个可迭代对象(类型是对象),而不是列表类型, 所以打印的时候不会打印列表. Pyth ...

  10. N皇后问题12 · N-Queens

    [抄题]: n皇后问题是将n个皇后放置在n*n的棋盘上,皇后彼此之间不能相互攻击. 给定一个整数n,返回所有不同的n皇后问题的解决方案. 每个解决方案包含一个明确的n皇后放置布局,其中“Q”和“.”分 ...