这道题数据太小啦!先枚举i,j表示从第i天到第j天不更改航线的费用。

然后直接跑最短路算法(我用的是Q版男朋友算法)

动归方程显然是f[i] = min(f[i], f[j] + cost[j+1][i] + k)

(PS:这道题一开始看成了某航线是否被ban...大家要注意啊!还有n和m的问题,各种别扭...)

代码(Submit Time
2016-01-10 15:48:24):

#include <cstdio>
#include <deque>
using namespace std;
const int maxd = 105;
const int maxn = 25;
const int INF = 1000000007;
int d, n, k, m, q;
int cost[maxd][maxd];
int f[maxd];
bool ban[maxn * maxn][maxd];
bool ava[maxd];
int min(int a, int b) {
return a < b ? a : b;
}
int getint() {
int r = 0, k = 1;
char c;
for (c = getchar(); c < '0' || c > '9'; c = getchar() ) if (c == '-') k = -1;
for (; '0' <= c && c <= '9'; c = getchar() ) r = r * 10 - '0' + c;
return r * k;
}
struct edge_type {
int v, next, w;
} edge[maxn * maxn]; int cnte = 0, h[maxn];
void ins(int u, int v, int w) {
edge[++cnte].v = v;
edge[cnte].w = w;
edge[cnte].next = h[u];
h[u] = cnte;
}
deque<int> Q;
int dis[maxn];
bool inque[maxn];
void SPFA() {
Q.push_back(1);
dis[1] = 0;
inque[1] = true;
for (int i = 2; i <= n; ++i) dis[i] = INF;
int now;
while (!Q.empty()) {
now = Q.front(); Q.pop_front(); inque[now] = false;
for (int i = h[now]; i; i = edge[i].next) {
int v = edge[i].v;
if (!ava[v]) continue;
if (dis[v] > dis[now] + edge[i].w) {
dis[v] = dis[now] + edge[i].w;
if (!inque[v]) {
Q.push_back(v);
inque[v] = true;
}
}
}
}
}
int main() {
d = getint(); n = getint(); k = getint(); m = getint();
int u, v, w;
for (int i = 0; i < m; ++i) {
u = getint(); v = getint(); w = getint();
ins(u, v, w); ins(v, u, w);
}
q = getint();
for (int i = 0; i < q; ++i) {
u = getint(); v = getint(); w = getint();
for (int j = v; j <= w; ++j) ban[u][j] = true;
}
for (int i = 1; i <= d; ++i)
for (int j = i; j <= d; ++j)
{
for (int mt = 1; mt <= n; ++mt) {
ava[mt] = true;
for (int t = i; t <= j; ++t)
if (ban[mt][t]) {
ava[mt] = false;
break;
}
}
SPFA();
if (dis[n] == INF) cost[i][j] = INF;
else cost[i][j] = dis[n] * (j-i+1);
}
for (int i = 1; i <= d; ++i) {
f[i] = cost[1][i];
for (int j = 1; j <= i; ++j) f[i] = min(f[i], f[j] + cost[j+1][i] + k);
}
printf("%d\n", f[d]);
return 0;
}

BZOJ 1003 物流运输【最短路】【动态规划】的更多相关文章

  1. BZOJ 1003 - 物流运输 - [最短路+dp]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...

  2. BZOJ 1003 物流运输 题解 【SPFA+DP】

    BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...

  3. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  4. BZOJ 1003 物流运输 (dp + dijkstra)

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

  5. BZOJ 1003 物流运输trans dijstra+dp

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

  6. BZoj 1003 物流运输 DP+最短路

    2013-09-11 09:56 W[I]代表前I天能取得的最小花费,假设在第J天更改一次路线,那么如果有 W[I]>W[J]+第j+1到第I天的最小花费+更改路线的花费(K) 那么更新W[I] ...

  7. BZOJ 1003 物流运输trans

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

  8. [BZOJ]1003 物流运输(ZJOI2006)

    挖坑,日常划水. 从BZOJ上的AC人数来看这题确实不难,但做这种题的常见思路让小C决定还是mark一下. Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才 ...

  9. BZOJ 1003 物流运输

    最短路+dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...

随机推荐

  1. 【GoLang】golang 最佳实践汇总

    最佳实践 1 包管理 1.1 使用包管理对Golang项目进行管理,如:godep/vendor等工具 1.2 main/init函数使用,init函数参考python 1.2.1 main-> ...

  2. 向Maven的本地库中添加jar文件

    有时我们要用的 maven 依赖项在官方repo库中找不到,然而我们从其他渠道获得了依赖项中的所有jar文件,本文记录了如何向本地库添加jar文件. 从复杂到简单,有三种方法: 使用 maven 的仓 ...

  3. Python:如何删除文件中的空白行?

    def delblankline(infile,outfile): infopen = open(infile,'r') outfopen = open(outfile,'w') lines = in ...

  4. 2017 苹果强制https

    苹果的ATS(App Transport Security)对服务器硬性3点要求: ① ATS要求TLS1.2或者更高,TLS 是 SSL 新的别称. ② 通讯中的加密套件配置要求支持列出的正向保密. ...

  5. git mac客户端使用提交与同步

    点击file -> add local  repository 将本地已经存在的git库添加到客户端中 当项目有改变时,点击正上方居中的Uncommitted Change按钮查看改变的内容 在 ...

  6. 在hive中遇到的错误

    1:如果在将文件导入到hive表时,查询结果为null(下图) 这个是因为在创建表的时候没有指定列分隔符,hive的默认分隔符是ctrl+a(/u0001)   2.当我复制好几行sql到hive命令 ...

  7. VR系统的组成

    转载请声明转载地址:http://www.cnblogs.com/Rodolfo/,违者必究. 一个典型的虚拟现实系统主要由计算机.输入/输出设备.应用软件和数据库等部分组成. 1.计算机 在虚拟现实 ...

  8. pm2无法自动重启

    在服务器上有个上传文件的服务,之前是pm2启动,每当有文件上传会自动重启 现在为了应对服务器宕机,我把启动脚本放在了另一文件夹内,所以就无法自动重启, 原文在 http://pm2.keymetric ...

  9. Forward-backward梯度求导(tensorflow word2vec实例)

    考虑不可分的例子         通过使用basis functions 使得不可分的线性模型变成可分的非线性模型 最常用的就是写出一个目标函数 并且使用梯度下降法 来计算     梯度的下降法的梯度 ...

  10. Power BI for Office 365(八)共享查询

    在Power Query中,你不但可以搜索线上的数据,也可以把自己的数据发布到线上供其它人检索.Power Query通过Power BI站点提供了这种内置的分享功能.在Excel中,Anna打开了她 ...