物流运输……看了神犇的题解,就是dp+最短路,设f[i]为1~i天的最少花费,那么

dp[i]=min(cost[1,i],min{dp[j]+cost[j+1,i]+K,1≤j<i})

就是从第一天到第i天不变或者从某一个之前的状态转移过来。


具体实现比较简单了。有个细节,就是如何表示某个码头从da天到db天开不开放,用了一个s数组。设f(i)开放为0,不开放为1,那么当仅∑i=dadbf(i)=0 时,从da天到db天码头i畅通无阻。所以用一次区间累加,再用一次求前缀和总计两次求和,得到s数组。(应该就是离散的积分)

画个图:

贴代码:

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <algorithm>
using namespace std; const int MAXN = 110, MAXV = 25, MAXE = 500, INF = 0x3f3f3f3f;
struct edge{int to, cost, next;} es[MAXE];
int head[MAXV];
int s[MAXV][MAXN];
int dp[MAXN]; //1~i天总最小花费
int N, K, M, E, tot; inline void add2(const int &a, const int &b, const int &cost){
es[tot] = (edge){b, cost, head[a]}; head[a] = tot++;
es[tot] = (edge){a, cost, head[b]}; head[b] = tot++;
} int d[MAXV], vis[MAXV];
int SPFA(int da, int db){
queue<int> que;
memset(vis, 0, sizeof(vis));
memset(d, 0x3f, sizeof(d));
que.push(1); vis[1] = 1; d[1] = 0;
while(!que.empty()){
int u = que.front(); que.pop(); vis[u] = 0;
for(int i = head[u]; i != -1; i = es[i].next){
int v = es[i].to;
if(s[v][db] - s[v][da-1]) continue;
if(d[v] > d[u] + es[i].cost){
d[v] = d[u] + es[i].cost;
if(!vis[v]){
que.push(v);
vis[v] = 1;
}
}
}
}
// printf("day%d~day%d cost %d\n", da, db, d[M]);
return d[M];
} int main(){
freopen("in.txt", "r", stdin);
scanf("%d%d%d%d", &N, &M, &K, &E); memset(head, -1, sizeof(head));
int a, b, cost;
for(int i = 0; i < E; ++i){
scanf("%d%d%d", &a, &b, &cost);
add2(a, b, cost);
}
int d, v;
scanf("%d", &d);
for(int i = 0; i < d; ++i){
scanf("%d%d%d", &v, &a, &b);
++s[v][a];
--s[v][b+1];
}
for(int k = 0; k < 2; ++k){
for(v = 1; v <= M; ++v){
for(int i = 1; i <= N; ++i){
s[v][i] += s[v][i-1];
}
}
}
// for(v = 1; v <= M; ++v){
// for(int i = 1; i <= N; ++i){
// printf("s[%d][%d]=%d\n", v, i, s[v][i]);
// }
// }
for(int i = 1; i <= N; ++i){
dp[i] = SPFA(1, i);
if(dp[i] < INF) dp[i] *= i;
for(int j = 1; j < i; ++j){
int cost = SPFA(j + 1, i);
if(cost < INF)
dp[i] = min(dp[i], dp[j] + K + cost * (i - j));
}
}
printf("%d\n", dp[N]);
return 0;
}

BZOJ1003: [ZJOI2006] 物流运输 trans的更多相关文章

  1. bzoj1003[ZJOI2006]物流运输trans

    1003: [ZJOI2006]物流运输trans Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常 ...

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

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

  3. 【动态规划】【spfa】【最短路】bzoj1003 [ZJOI2006]物流运输trans

    预处理cost[a][b] 表示第a天到第b天用同一条线路的成本. 具体转移看代码. #include<cstdio> #include<algorithm> #include ...

  4. BZOJP1003 [ZJOI2006]物流运输trans

    BZOJP1003 [ZJOI2006]物流运输trans 1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec  Memory Limit: 162 MB Sub ...

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

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

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

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

  7. bzoj1003 [ZJOI2006]物流运输

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 6300  Solved: 2597[Submit][Stat ...

  8. bzoj1003: [ZJOI2006]物流运输(DP+spfa)

    1003: [ZJOI2006]物流运输 题目:传送门 题解: 可以用spfa处理出第i天到第j都走这条路的花费,记录为cost f[i]表示前i天的最小花费:f[i]=min(f[i],f[j-1] ...

  9. 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP

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

随机推荐

  1. [Linux] umount目录提示device is busy的解决方法

    使用sshfs等方式挂载的目录出现问题时,使用umount卸载经常提示device is busy,如果仔细阅读错误提示就可以找到命令lsof和fuser命令. 其实原因就是有进程占用当前目录,导致不 ...

  2. pexpect获取远端命令执行结果

    类比于shell的expect, python中使用pexpect模块来模拟用户和终端交互.有的时候使用pexpect.sendline发送命令后,在各种条件影响下, 可能并不能保证命令在远端服务器执 ...

  3. npm 版本问题

    STF之问题篇 https://yq.aliyun.com/articles/221602 装完成后输入stf doctor查看工具依赖是否正确,安装教程可以参考我之前写的,这里不再多说,直接说问题. ...

  4. HDU 4735 Little Wish~ lyrical step~(DLX搜索)(2013 ACM/ICPC Asia Regional Chengdu Online)

    Description N children are living in a tree with exactly N nodes, on each node there lies either a b ...

  5. Sql Express数据备份和还原

    参考文章:在SQL Server Express版本中没有代理功能如何自动备份数据库 首先用以下脚本,生成可以自动备份数据库的存储过程: USE [master] GO SET ANSI_NULLS ...

  6. (转) linux I/O优化 磁盘读写参数设置

    关于页面缓存的信息,可以用cat /proc/meminfo 看到.其中的Cached 指用于pagecache的内存大小(diskcache-SwapCache).随着写入缓存页,Dirty 的值会 ...

  7. 在vue-cli创建的项目里配置scss

    第一步,gitbash进入到项目目录 npm install node-sass --save-dev npm install sass-loader --save-dev 第二步:打开webpack ...

  8. Struts1之编码问题

    <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding=& ...

  9. Spark+Python+Pycharm在Windows下的配置

    http://blog.csdn.net/ydq1206/article/details/51922148

  10. BZOJ5340 [Ctsc2018]假面 【概率dp】

    题目链接 BZOJ5340 题解 我们能很容易维护每个人当前各种血量的概率 设\(p[u][i]\)表示\(u\)号人血量为\(i\)的概率 每次攻击的时候,讨论一下击中不击中即可转移 是\(O(Qm ...