BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)
题目链接
http://www.lydsy.com/JudgeOnline/problem.php?id=1003
思路
先Dijkstra暴力求出i..j天内不变换路线的最少花费,然后dp[i] = min(cost[1..i], dp[j]+cost[j+1][i]+K).
总结:
1. BZOJ 题目质量果然高啊……做一道一次升华……
2.自己太弱……
代码
[cpp]
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#define MID(x,y) ((x+y)/2)
#define MEM(a,b) memset(a,b,sizeof(a))
using namespace std;
struct edge{
int v, w;
edge(int _v, int _w){
v = _v, w = _w;
}
};
typedef vector <edge> VI;
priority_queue <pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > PQ;
const int maxn = 25;
VI adj[maxn];
int n, m, K, e, d;
int cost[105][105], dist[maxn], flag[25][105], dp[105];
bool vis[maxn];
void dij(int l, int r){
while(!PQ.empty()) PQ.pop();
MEM(vis, 0);
for (int i = 2; i <= m; i ++) dist[i] = 0x3fffffff;
for (int i = 1; i <= m; i ++)
for (int j = l; j <= r; j ++)
if (flag[i][j]){
vis[i] = 1;
break;
}
dist[1] = 0;
PQ.push(make_pair(0, 1));
while(!PQ.empty()){
int u = PQ.top().second;
PQ.pop();
for (int i = 0; i < (int)adj[u].size(); i ++){
int v = adj[u][i].v;
if (!vis[v] && dist[v] > dist[u] + adj[u][i].w){
dist[v] = dist[u] + adj[u][i].w;
PQ.push(make_pair(dist[v], v));
}
}
}
if (dist[m] != 0x3fffffff)
cost[l][r] = (r - l + 1) * dist[m];
else
cost[l][r] = 0x3fffffff;
//printf("l = %d r = %d cost = %d\n", l, r, cost[l][r]);
}
int main(){
//freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
scanf("%d %d %d %d", &n, &m, &K, &e);
for (int i = 0; i <= m; i ++) adj[i].clear();
for (int i = 0; i < e; i ++){
int u, v, w;
scanf("%d %d %d", &u, &v, &w);
adj[u].push_back(edge(v, w));
adj[v].push_back(edge(u, w));
}
scanf("%d", &d);
MEM(flag, 0);
for (int i = 0; i < d; i ++){
int p, l, r;
scanf("%d %d %d", &p, &l, &r);
for(int j = l; j <= r; j ++)
flag[p][j] = 1;
}
for (int i = 1; i <= n; i ++){
for (int j = i; j <= n; j ++){
dij(i, j);
}
}
for (int i = 1; i <= n; i ++) dp[i] = 0x3fffffff;
dp[0] = 0;
for (int i = 1; i <= n; i ++){
dp[i] = cost[1][i];
for (int j = 0; j < i; j ++){
if (cost[j+1][i] == 0x3fffffff) continue;
dp[i] = min(dp[i], dp[j] + cost[j+1][i] + K);
}
}
printf("%d\n", dp[n]);
return 0;
}
[/cpp]
BZOJ 1003 [ZJOI2006]物流运输trans ★(Dijkstra + DP)的更多相关文章
- BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP
题意:链接 方法:SPFA+DP 解析:挺好的题目.因为数据范围较小所以用这样的方式能够搞,只是也是挺不好想的. 我们定义cost(i,j)表示从第i天走到第j天运用同一种方式的最小花费,然后因为数据 ...
- BZOJ 1003: [ZJOI2006]物流运输trans(最短路+dp)
1A,爽! cost[i][j]表示从第i天到第j天不改路线所需的最小花费,这个可以用最短路预处理出.然后dp(i)=cost[j][i]+dp(j-1)+c. c为该路线的花费. --------- ...
- BZOJ 1003 [ZJOI2006]物流运输trans
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4242 Solved: 1765[Submit] ...
- BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1003 题意: 思路: 首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a ...
- bzoj 1003: [ZJOI2006]物流运输【spfa+dp】
预处理出ans[i][j]为i到j时间的最短路,设f[i]为到i时间的最小代价,转移显然就是 f[i]=min(f[j-1]+ans[j][i]*(i-j+1)+k); #include<ios ...
- BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA
标题效果:给定一个无向图.输送n日,有一天的某一时刻不能去,更换行考虑k,求总成本 一阶cost[i][j]用于第一i为了天j天正在同一航线的最低消费 这种利用SPFA处理 然后就是移动的法规问题 订 ...
- BZOJ 1003[ZJOI2006]物流运输(SPFA+DP)
Problem 1003. -- [ZJOI2006]物流运输 1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: ...
随机推荐
- 聊一聊python的单例模式
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- Delphi APP 開發入門(五)GPS 定位功能
Delphi APP 開發入門(五)GPS 定位功能 分享: Share on facebookShare on twitterShare on google_plusone_share 閲讀次數 ...
- 微信小程序组件loading
操作反馈loading:官方文档 Demo Code: Page({ data: { hidden: true }, loadingChange: function () { console.log( ...
- vs2010 net4.0 c# 操作 sqlite
1.百科介绍 SQLite,是一款轻型的数据库,是遵守ACID的关系型数据库管理系统,它包含在一个相对小的C库中.它是D.RichardHipp建立的公有领域项目.它的设计目标是嵌入式的,而且目前已经 ...
- Divide by Zero 2017 and Codeforces Round #399 (Div. 1 + Div. 2, combined) D. Jon and Orbs
地址:http://codeforces.com/contest/768/problem/D 题目: D. Jon and Orbs time limit per test 2 seconds mem ...
- 修改myeclipse字体与操作系统的字体一致
如果你是win7系统,想要修改Myeclipse字体,步骤如下:第一步:C:\Windows\Fonts,找到Courier New,鼠标右键-->显示第二步:Ceneral --> Ap ...
- union遇上ntext数据类型
http://www.myhack58.com/Article/html/3/7/2011/31392.htm
- 关于STM8S使用硬件SPI收发问题
源: 关于STM8S使用硬件SPI收发问题
- Send2MyKindle使用说明文档
软件下载地址为:Send2MyKindle 功能简介 该软件主要功能为在Windows下将Kindle电子书发送到亚马逊中国网站注册的Kindle账户.整个软件界面如下图所示: 使用步骤 使用前的准备 ...
- MyBatis如何返回自增的ID
<insert id="insertTable" parameterType="com.infohold.city.map.model.CheckTemplateM ...