1A,爽!

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

-------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
 
#define rep(i,n) for(int i=0;i<n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<=r;++i)
 
using namespace std;
 
const int inf=0x7fffffff;
const int maxn=25,maxday=105;
 
int cost[maxday][maxday];
 
struct DIJKSTRA {
struct Edge {
int u,v,d;
Edge(int u,int v,int d):u(u),v(v),d(d) {}
};
struct node {
int u,d;
node(int u,int d):u(u),d(d) {}
bool operator < (const node &o) const {
return d>o.d;
}
};
int n,s,t,day[2];
int d[maxn];
int ok[maxn][maxday];
vector<int> g[maxn];
vector<Edge> edges;
void init(int n) {
this->n=n;
rep(i,n) g[i].clear();
edges.clear();
clr(ok,-1);
}
void addEdge(int u,int v,int d) {
edges.push_back( (Edge) {u,v,d} );
g[u].push_back(edges.size()-1);
g[v].push_back(edges.size()-1);
}
inline void add(int a,int b,int p) {
Rep(i,a,b) ok[p][i]=0;
}
inline bool jud(int o) {
Rep(i,day[0],day[1]) 
   if(!ok[o][i]) return 0;
return 1;
}
int Dijkstra(int s,int t,int a,int b) {
this->s=s; this->t=t;
day[0]=a; day[1]=b;
rep(i,n) d[i]=inf; d[s]=0;
priority_queue<node> q;
if(jud(s)) q.push( (node) {s,0} );
while(!q.empty()) {
node x=q.top(); q.pop();
if(d[x.u]!=x.d) continue;
rep(i,g[x.u].size()) {
Edge &e=edges[g[x.u][i]];
int u=e.u,v=e.v;
if(x.u!=u) swap(u,v);
if(!jud(v)) continue;
if(d[v]>d[u]+e.d) {
d[v]=d[u]+e.d;
q.push( (node) {v,d[v]} );
}
}
}
return d[t];
}
} dijkstra;
 
struct DP {
int d[maxday];
int n;
void init(int n) {
this->n=n;
d[0]=0;
Rep(i,1,n) 
   d[i]= cost[1][i]==inf ? inf : cost[1][i];
}
int work(int c) {
Rep(i,1,n) 
   Rep(j,2,i) if(cost[j][i]!=inf)
       d[i]=min(d[i],d[j-1]+cost[j][i]+c);
return d[n];
}
} dp;
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
int n,m,c,e,a,b,w;
scanf("%d%d%d%d",&n,&m,&c,&e);
dijkstra.init(m);
while(e--) {
scanf("%d%d%d",&a,&b,&w);
--a; --b;
dijkstra.addEdge(a,b,w);
}
int d;
scanf("%d",&d);
while(d--) {
scanf("%d%d%d",&w,&a,&b);
dijkstra.add(a,b,--w);
}
Rep(i,1,n)
Rep(j,i,n) {
cost[i][j]=dijkstra.Dijkstra(0,m-1,i,j);
if(cost[i][j]!=inf) cost[i][j]*=(j-i+1);
}
dp.init(n);
printf("%d\n",dp.work(c));
return 0;
}

-------------------------------------------------------------------------------

1003: [ZJOI2006]物流运输trans

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3924  Solved: 1625
[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

BZOJ 1003: [ZJOI2006]物流运输trans(最短路+dp)的更多相关文章

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

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

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

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

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

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

  4. 【BZOJ】1003: [ZJOI2006]物流运输trans(SPFA+DP)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 这题一开始看是不会的额,,,还是看题解了..一开始我觉得不能用最短路啥的,,看了题解发现这是d ...

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

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

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

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

  7. BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA

    标题效果:给定一个无向图.输送n日,有一天的某一时刻不能去,更换行考虑k,求总成本 一阶cost[i][j]用于第一i为了天j天正在同一航线的最低消费 这种利用SPFA处理 然后就是移动的法规问题 订 ...

  8. [BZOJ1003] [ZJOI2006] 物流运输trans (最短路 & dp)

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

  9. BZOJ_1003_[ZJOI2006]物流运输_最短路+dp

    BZOJ_1003_[ZJOI2006]物流运输_最短路+dp 题意:http://www.lydsy.com/JudgeOnline/problem.php?id=1003 分析: 这种一段一段的显 ...

随机推荐

  1. C语言入门(12)——递归

    一个函数在它的函数体内调用它自身称为递归调用.有递归调用操作的函数被称为递归函数.递归调用可以是直接调用,也可以是间接调用.也可以理解为函数的嵌套调用是函数本身. 例如实现一个求阶乘的函数: long ...

  2. poj2509---抽k根烟就换一支,求能抽烟的总数

    #include <stdio.h> #include <stdlib.h> int main() { int now,k; while(scanf("%d %d&q ...

  3. js完美继承代码示例

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 加入收藏夹的js代码(求兼容chrome浏览器的代码)

    从网上找了加入收藏夹的js代码,但不兼容chrome,不知道有没有兼容chrome的相关代码,希望有知道的告诉一下,谢谢! 代码如下 $("#id").click(function ...

  5. 在iOS当中发送电子邮件和短信

    iOS实现发送电子邮件的方法很简单,首先导入MessageUI.framework框架,然后代码如下: #import "RPViewController.h" //添加邮件头文件 ...

  6. 在cmd中输入ls命令出现“ls不是内部或外部命令解决

    今天在学习sass查看目录遇到cmd输入ls提示不是内部命令 解决方法: 新建一个ls.bat文件 内容为: @echo off dir

  7. SurfaceView 和 View 区别

    android.view.View 和 android.view.SurfaceView SurfaceView 是从 View 基类中派生出来的显示类,直接子类有 GLSurfaceView和Vid ...

  8. _视图控制对象生命周期-init、viewDidLoad、viewWillAppear、viewDidAppear、viewWillDisappear等的区别及用途

    iOS视图控制对象生命周期-init.viewDidLoad.viewWillAppear.viewDidAppear.viewWillDisappear.viewDidDisappear的区别及用途 ...

  9. Rabbit and Grass(杭电1849)(尼姆博弈)

    Rabbit and Grass Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  10. 使用 AngularJS 从零构建大型应用

    0.导言 1.准备工作 2.构建框架 3.丰富你的directives 4.公用的services 5.用controllers组织业务 导言 纵览线上各种AngularJS教程,大部分都是基础与一些 ...