BZOJ 1003 物流运输【最短路】【动态规划】
这道题数据太小啦!先枚举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 物流运输【最短路】【动态规划】的更多相关文章
- BZOJ 1003 - 物流运输 - [最短路+dp]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...
- BZOJ 1003 物流运输 题解 【SPFA+DP】
BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- BZOJ 1003 物流运输 (dp + dijkstra)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8672 Solved: 3678[Submit][Stat ...
- BZOJ 1003 物流运输trans dijstra+dp
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3896 Solved: 1608[Submit] ...
- BZoj 1003 物流运输 DP+最短路
2013-09-11 09:56 W[I]代表前I天能取得的最小花费,假设在第J天更改一次路线,那么如果有 W[I]>W[J]+第j+1到第I天的最小花费+更改路线的花费(K) 那么更新W[I] ...
- BZOJ 1003 物流运输trans
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- [BZOJ]1003 物流运输(ZJOI2006)
挖坑,日常划水. 从BZOJ上的AC人数来看这题确实不难,但做这种题的常见思路让小C决定还是mark一下. Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才 ...
- BZOJ 1003 物流运输
最短路+dp. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
随机推荐
- 示例开发过程记录:meteor,react,apollo
本示例记录一个开发过程: 1)参考 Meteor React TUTORIAL教程 https://www.meteor.com/tutorials/react/creating-an-app 2). ...
- Unity3D LuaBundleLoader(基于cslua)
说明:异步加载lua的bundle,会优先加载cache目录下bundle(一般更新的资源都在cache下) using System; using UnityEngine; using System ...
- Deep Learning入门视频(下)之关于《感受神经网络》两节中的代码解释
代码1如下: #深度学习入门课程之感受神经网络(上)代码解释: import numpy as np import matplotlib.pyplot as plt #matplotlib是一个库,p ...
- java中集合的使用
集合使用: 先说数组:array :用来存同一种数组类型的容器 eg:现在想把班上所有人的信息存起来 1.每一个人的信息可以用一个对象存起来 2.可以用一个数组来接受(现在数组中要接受的是对象) ob ...
- 原生java 压缩解压zip文件
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import ...
- 深入理解MVC模式
一,什么是MVC模式 该模式是一种软件设计典范,他把软件系统划分为三个基本部分:模型层(Model).视图层(View).控制器(Controller) *Model(模型)表示应用程序核心(比如数据 ...
- 剪短的python数据结构和算法的书《Data Structures and Algorithms Using Python》
按书上练习完,就可以知道日常的用处啦 #!/usr/bin/env python # -*- coding: utf-8 -*- # learn <<Problem Solving wit ...
- 算法系列:Fibonacci
Copyright © 1900-2016, NORYES, All Rights Reserved. http://www.cnblogs.com/noryes/ 欢迎转载,请保留此版权声明. -- ...
- 3.Java异常进阶
3.JAVA异常进阶 1.Run函数中抛出的异常 1.run函数不会抛出异常 2.run函数的异常会交给UncaughtExceptionhandler处理 3.默认的UncaughtExceptio ...
- 彻底解决m2eclipse之Unable to update index for central
原文链接:https://my.oschina.net/itblog/blog/208581 maven是个好东西,eclipse上的maven插件m2eclipse也非常方便,但是最近这个东西经常无 ...