题链

http://www.lydsy.com/JudgeOnline/problem.php?id=1003

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

题解

a[i][j]为第i天到第j天畅通的最短路径,dp[i]为到第i天的最小成本。

\(dp[i]=min ( dp[i] , dp[j] + k + a[j+1][i]*(i-j) );\)

至于a[i][j],直接使用最短路算法预处理一下即可

参考代码

#include<queue>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define ll long long
#define inf 1000000000
#define mod 1000000007
using namespace std;
int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
void Out(ll a) {
if(a<0) putchar('-'),a=-a;
if(a>=10) Out(a/10);
putchar(a%10+'0');
}
const int N=105;
struct Edge{
ll cost;
int to,nxt;
Edge(){};
Edge(ll tc,int tt,int tn=0):cost(tc),to(tt),nxt(tn){}
bool operator < (const Edge &an) const{
return cost>an.cost;
}
}Path[1005];
int head[N],cnt;
ll dis[N],a[N][N],dp[N];
bool vis[N],flag[N][N];
void Addedge(int u,int v,int w){
Path[cnt]=(Edge){w,v,head[u]};
head[u]=cnt++;
}
bool lock[N];
void Dijkstra(int x,int y,int m)
{
priority_queue<Edge>que;
memset(lock,false,sizeof(lock));
memset(vis,false,sizeof(vis));
for(int i=1;i<=m;i++) dis[i]=inf;
for(int i=x;i<=y;i++)
for(int j=1;j<=m;j++)
if(flag[i][j]) lock[j]=true;
dis[1]=0;
que.push(Edge(0LL,1));
while(!que.empty()){
int cur=que.top().to;que.pop();
if(vis[cur])continue;
vis[cur]=true;
for(int i=head[cur];i;i=Path[i].nxt)
if(!lock[Path[i].to]&&dis[cur]+Path[i].cost<dis[Path[i].to]){
dis[Path[i].to]=dis[cur]+Path[i].cost;
que.push(Edge(dis[Path[i].to],Path[i].to));
}
}
a[x][y]=dis[m];
}
void Init(){
memset(head,0,sizeof(head));
memset(flag,false,sizeof(flag));
cnt=1;
}
int main()
{
Init();
int n=read(),m=read(),k=read(),q=read();
for(int i=0;i<q;i++){
int u=read(),v=read(),x=read();
Addedge(u,v,x);
Addedge(v,u,x);
}
int d=read();
for(int i=0;i<d;i++){
int id=read(),x=read(),y=read();
for(int j=x;j<=y;j++) flag[j][id]=true;
}
for(int i=1;i<=n;i++) for(int j=i;j<=n;j++){
Dijkstra(i,j,m);
}
for(int i=1;i<=n;i++){
dp[i]=a[1][i]*i;
for(int j=0;j<i;j++) dp[i]=min(dp[i],dp[j]+k+a[j+1][i]*(i-j));
}
Out(dp[n]);
return 0;
}

【BZOJ 1003】[ZJOI2006]物流运输(Dijkstra+DP)的更多相关文章

  1. BZOJ 1003[ZJOI2006]物流运输(SPFA+DP)

    Problem 1003. -- [ZJOI2006]物流运输 1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: ...

  2. BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路

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

  3. BZOJ 1003: [ZJOI2006]物流运输trans(最短路+dp)

    1A,爽! cost[i][j]表示从第i天到第j天不改路线所需的最小花费,这个可以用最短路预处理出.然后dp(i)=cost[j][i]+dp(j-1)+c. c为该路线的花费. --------- ...

  4. BZOJ 1003 [ZJOI2006]物流运输trans

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

  5. bzoj 1003 [ZJOI2006]物流运输(最短路+dp)

    [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8973  Solved: 3839[Submit][Status][Di ...

  6. BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1003 思路 先Dijkstra暴力求出i..j天内不变换路线的最少花费,然后dp[i] = ...

  7. BZOJ.1003.[ZJOI2006]物流运输(DP 最短路Dijkstra)

    题目链接 容易看出是个最短路+DP.既然答案和天数有关,那么就令\(f[i]\)表示前\(i\)天最小成本. 这个转移很好想: \(f[i]=\min(f[i],\ f[j]+cost(j+1,i)+ ...

  8. BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 题意: 思路: 首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a ...

  9. BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP

    题意:链接 方法:SPFA+DP 解析:挺好的题目.因为数据范围较小所以用这样的方式能够搞,只是也是挺不好想的. 我们定义cost(i,j)表示从第i天走到第j天运用同一种方式的最小花费,然后因为数据 ...

  10. bzoj 1003: [ZJOI2006]物流运输【spfa+dp】

    预处理出ans[i][j]为i到j时间的最短路,设f[i]为到i时间的最小代价,转移显然就是 f[i]=min(f[j-1]+ans[j][i]*(i-j+1)+k); #include<ios ...

随机推荐

  1. 用WEKA进行数据挖掘

    学习于IBM教学文档 数据挖掘学习与weka使用 第二部 分分类和集群 分类 vs. 群集 vs. 最近邻 在我深入探讨每种方法的细节并通过 WEKA 使用它们之前,我想我们应该先理解每个模型 - 每 ...

  2. Linux环境下源码安装Apache2.2.25

    操作环境:RedHat Enterprise Linux 5.6 一.安装准备 安装Apache一般依赖3个组件:apr.apr-util.pcre. 确保这三个组件已经安装. [root@bigsr ...

  3. Styles and Themens(1)详述

    Styles and Themes IN THIS DOCUMENT Defining Styles Inheritance Style Properties Applying Styles and ...

  4. [转]Open Data Protocol (OData) Basic Tutorial

    本文转自:http://www.odata.org/getting-started/basic-tutorial/ Basic Tutorial The Open Data Protocol (ODa ...

  5. 10.3 Implementing pointers and objects and 10.4 Representing rooted trees

    Algorithms 10.3 Implementing pointers and  objects  and 10.4 Representing rooted trees Allocating an ...

  6. spring.net应用

    经过一段时间的调试,终于把spring.net中关于aop的方面给做个了一个比较完整的Demo.包含异常日志和性能日志.spring.net和log4net配置. http://files.cnblo ...

  7. windows echo命令

    ECHO命令是大家都熟悉的DOS批处理命令的一条子命令,但它的一些功能和用法也许你并不是全都知道,不信你瞧:  1. 作为控制批处理命令在执行时是否显示命令行自身的开关 格式:ECHO [ON|OFF ...

  8. matlab中数据类型

    在MATLAB中有15种基本数据类型,分别是8种整型数据.单精度浮点型.双精度浮点型.逻辑型.字符串型.单元数组.结构体类型和函数句柄.这15种基本数据类型具体如下. 有符号整数型:int8,int1 ...

  9. 结对项目--黄金点游戏(邓乐&曾亮)

    #include<stdio.h> #include<stdlib.h> #include<Windows.h> int result[100][1000000]; ...

  10. leetcode_655. Print Binary Tree

    https://leetcode.com/problems/print-binary-tree/ 打印整棵二叉树 class Solution { public: int getTreeHeight( ...