BZOJ 1003 物流运输 (dp + dijkstra)
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
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
//前三天走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)的更多相关文章
- BZoj 1003 物流运输 DP+最短路
2013-09-11 09:56 W[I]代表前I天能取得的最小花费,假设在第J天更改一次路线,那么如果有 W[I]>W[J]+第j+1到第I天的最小花费+更改路线的花费(K) 那么更新W[I] ...
- BZOJ 1003 物流运输 题解 【SPFA+DP】
BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...
- BZOJ 1003 物流运输trans dijstra+dp
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3896 Solved: 1608[Submit] ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- BZOJ 1003 - 物流运输 - [最短路+dp]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...
- bzoj 1003物流运输 区间dp+spfa
基本思路: 一开始确实没什么思路,因为觉得怎么着都会超时,然后看一下数据范围,呵,怎么都不会超时. 思路: 1.看到能改变线路,想到可以用以下区间dp,区间dp的话,先枚举长度,枚举开始位置,然后枚举 ...
- BZOJ 1003 物流运输trans
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- [BZOJ]1003 物流运输(ZJOI2006)
挖坑,日常划水. 从BZOJ上的AC人数来看这题确实不难,但做这种题的常见思路让小C决定还是mark一下. Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才 ...
- BZOJ 1003 物流运输
最短路+dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
随机推荐
- BLOB 操作
对于数据库是BLOB类型存储数据. BLOB数据插入: Oracle提供的标准方式: 先插入一个空BLOB对象,然后Update这个空对象. 首先使用empty_blob()函数插入一个空BLOB对象 ...
- form 表单排序
<html> <head></head> <body> <form class="form-inline"> ...
- pip批量更新安装的包
------------------pip批量更新库-------------------- 1)查看过期的库 pip list --outdated 更新单一的库: pip install --u ...
- Mysql InnoDB 数据更新 锁表
一.数据表结构 1 2 3 4 5 6 7 8 9 10 CREATE TABLE `jx_attach` ( `attach_id` int(11) NOT NULL AUTO_INCREMEN ...
- python,使用PIL库对图片进行操作
在做识别验证码时,需要对验证码图片进行一些处理,所以就学习了一下PIL的知识,下面是我总结的一些常用方法. 注明:图片的操作都需要Image库,所以要使用import Image导入库 1.打开图片 ...
- golang interface接口
package main import "fmt" type Shaper interface { Area() float32 } type Square struct { si ...
- Android中注解的使用
如果你是一名安卓开发者,你也一定听过大名鼎鼎的网络请求框架Retrofit.它将网络请求的方式以注解的形式展现,极大的提高了代码的可读性,同时网络请求集中写在一个interface中提高了代码的可维护 ...
- 11-matlba-bellman-ford;地杰斯特拉
求最短路: 1.bellman-ford: %求s到各点的最短距离 function Dist = Bellman_Ford(s) load cityJuli; for i = 1:154 Dist( ...
- php多进程 防止出现僵尸进程
对于用PHP进行多进程并发编程,不可避免要遇到僵尸进程的问题. 僵尸进程是指的父进程已经退出,而该进程dead之后没有进程接受,就成为僵尸进程(zombie)进程.任何进程在退出前(使用exit退出) ...
- Cannot find autoconf. Please check your autoconf installation and the $PHP_AUTOCONF environment variable. Then, rerun this scrip
在运行phpize时出现的错误 > /data/php/bin/phpize Configuring for: PHP Api Version: 20131106 Zend Module Api ...