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. iOS中的retainCount

    我们都知道iOS中采用引用计数的技术来管理内存,当一个对象没有任何一个地方引用的时候会自动释放,此时的retainCount为0,而且提供了一个-(NSInteger)retainCount的方法来获 ...

  2. 链表-Add Two Numbers

    第一版代码(很挫很罗嗦,不过是第一次做,记录一下成长的脚步!继续努力!) /*struct ListNode { int val; struct ListNode *next; };*/ typede ...

  3. Day01

    1.@Test函数,执行后控制台没有输出结果? 1)  不能用静态方法,控制台会没有结果. 2)  不能把类名命名为Test,@Test不识别. 2.遍历Map集合的entrySet方法不会? 3.使 ...

  4. iis 目录枚举文件枚举解决方案

    1.关闭windows的8.3格式功能. 修改注册表键值: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem 修改NtfsD ...

  5. ORACLE恢复误删除的对象(表、存储过程等)

    1.恢复存储过程 原理就是利用了oracle里所有的存储过程的源代码都是存在dba_source里,而drop某个存储过程的时候,oracle这里肯定要去dba_source里把相关的源代码给dele ...

  6. left outer join

      table   A:  Field_K,   Field_A    1                       a    3                       b    4      ...

  7. 几个SQL

    select sum(`value`) from testtable where value != 'error' AND type ='b' in (select DISTINCT(type) fr ...

  8. Hibernate学习之hibernate执行顺序

    Hibernate 执行的顺序如下:  (1) 生成一个事务的对象,并标记当前的 Session 处于事务状态(注:此时并未启动数据库级事务).  (2) 应用使用 s.save 保存对象,这个时候  ...

  9. Spring总结——控制反转,注入(配置和注解两种方式)

    一.Spring的容器: 1.什么是控制反转:传统的方法,当某个java对象A需要调用对象B时,是由调用者(对象A)通过new关键字来创建对象B的(也可以说类A依赖类B),而在Spring中,则是由s ...

  10. Java 方法调用疑问

    同一个类中同一个方法,实例化后,同时调用两次,内存分配如何?