[Bzoj1003][ZJOI2006]物流运输(spfa+dp)
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003
比较简单的dp,dp[i]为1-i天最小费用,dp方程为dp[i] = min(dp[i], dp[j] + c[j + 1][i] * (i - j) + k),(0<=j<i),c[i][j]为第i天到第j天都能走的最短路。
因为数据贼小,所有求最短路的方法都可以预处理出来c数组。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 1e9 + ;
struct node {
int e, w, next;
}edge[];
int head[], len;
ll dp[];
int st[][];
ll c[][];
int dis[];
int vis[], vis2[];
void init() {
memset(head, -, sizeof(head));
len = ;
memset(dp, 0x7f, sizeof(dp));
}
void add(int s, int e, int w) {
edge[len].e = e;
edge[len].w = w;
edge[len].next = head[s];
head[s] = len++;
}
int spfa(int n) {
queue<int>q;
for (int i = ; i <= n; i++)
dis[i] = INF, vis2[i] = ;
dis[] = ;
vis2[] = ;
q.push();
while (!q.empty()) {
int x = q.front();
q.pop();
vis2[x] = ;
for (int i = head[x]; i != -; i = edge[i].next) {
int y = edge[i].e;
if (vis[y])continue;
if (dis[y] > dis[x] + edge[i].w) {
dis[y] = dis[x] + edge[i].w;
if (!vis2[y]) {
q.push(y);
vis2[y] = ;
}
}
}
}
return dis[n];
}
int main() {
int n, m, k, s, x, y, z;
scanf("%d%d%d%d", &n, &m, &k, &s);
init();
for (int i = ; i <= s; i++) {
scanf("%d%d%d", &x, &y, &z);
add(x, y, z);
add(y, x, z);
}
scanf("%d", &s);
for (int i = ; i <= s; i++) {
scanf("%d%d%d", &x, &y, &z);
for (int j = y; j <= z; j++)
st[x][j] = ;
}
for (int i = ; i <= n; i++) {
for (int j = ; j <= n; j++) {
memset(vis, , sizeof(vis));
for (int q = i; q <= j; q++)
for (int w = ; w <= m; w++)
if (st[w][q])vis[w] = ;
c[i][j] = spfa(m);
}
}
for (int i = ; i <= n; i++) {
dp[i] = (ll)c[][i] * i;
for (int j = ; j < i; j++)
dp[i] = min(dp[i], dp[j] + c[j + ][i] * (i - j) + k);
}
printf("%lld\n", dp[n]);
return ;
}
[Bzoj1003][ZJOI2006]物流运输(spfa+dp)的更多相关文章
- bzoj1003: [ZJOI2006]物流运输(DP+spfa)
1003: [ZJOI2006]物流运输 题目:传送门 题解: 可以用spfa处理出第i天到第j都走这条路的花费,记录为cost f[i]表示前i天的最小花费:f[i]=min(f[i],f[j-1] ...
- BZOJ 1003[ZJOI2006]物流运输(SPFA+DP)
Problem 1003. -- [ZJOI2006]物流运输 1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: ...
- [ZJOI2006]物流运输 SPFA+DP
题目描述 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪. ...
- 2018.09.02 bzoj1003: [ZJOI2006]物流运输(dp+最短路转移)
传送门 dp好题. 每一天要变更路线一定还是走最短路. 所以l~r天不变更路线的最优方案就是把l~r天所有不能走的点都删掉再求最短路.显然是可以dp的. 设f[i]表示第i天的最优花销.那么我们枚举在 ...
- bzoj1003 [ZJOI2006]物流运输
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 6300 Solved: 2597[Submit][Stat ...
- bzoj1003[ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常 ...
- BZOJ1003: [ZJOI2006] 物流运输 trans
物流运输--看了神犇的题解,就是dp+最短路,设f[i]为1~i天的最少花费,那么 dp[i]=min(cost[1,i],min{dp[j]+cost[j+1,i]+K,1≤j<i}) 就是从 ...
- [BZOJ1003] [ZJOI2006] 物流运输trans (最短路 & dp)
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- [bzoj1003][ZJOI2006][物流运输] (最短路+dp)
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
随机推荐
- linux Sersync 上配置客户端
1.安装 Rsync 并配置相关权限 在 SERSYNC 上配置 RSYNC 客户端相关权限认证: [root@SERSYNC /]# yum install rsync -y [root@SERSY ...
- 脚本_查找 Linux 系统中的僵尸进程
#!bin/bash#功能:查找Linux系统中的僵尸进程#作者:liusingbon#使用awk判断ps命令输出的第8列为Z时,显示该进程的 PID 和进程命令ps aux |awk '{if($8 ...
- window环境下mysql导入sql文件时报错:ERROR: ASCII '\0' appeared in the statement
错误信息: ERROR: ASCII '\0' appeared in the statement, but this is not allowed unless option --binary-mo ...
- thinkphp 响应对象response
1.可以通过修改配置文件的 default_return_type修改输出类型 // 默认输出类型 'default_return_type' => 'html', 2. 可以通过Config类 ...
- 关于python读写文件的r+方式的坑
写脚本的时候需要将文件中的一行修改,我的修改逻辑是,用r+方式打开文件,然后将原文件数据读入一个数组,修改数组的对应元素,在seek(0),然后将数组write进文件 结果: 文件文件末尾总是多出一行 ...
- Python---基础-小游戏用户猜数字
一.10 < cost < 50 的等价表达式 cost = 40 10 < cost < 50 (10 < cost) and (cost < 50) 二.使用i ...
- Proto3语法翻译
本文主要对proto3语法翻译.参考网址:https://developers.google.com/protocol-buffers/docs/proto3 defining a message t ...
- drf 的分页功能
1 settings中配置 page_size = 20 代表每页20条数据 REST_FRAMEWORK = { 'DEFAULT_PARSER_CLASSES': ( 'rest_framewor ...
- jquery用法初探
JQuery选择器 JQuery选择器用于查找满足条件的元素,比如可以用$(“#控件Id”)来根据控件id获得控件的jQuery对象,相当于getElementById: 1.id 选择器 $(“ ...
- 【CF1257E】The Contest【线段树】
题意:给定三个序列abc,问最少操作几次使得满足a<b<c 题解:将三个序列合并起来,设cnt[i][1/2/3]表示前i个数有几个是来自序列1/2/3的. 枚举第一个序列要到i,此时对于 ...