题目链接

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)的更多相关文章

  1. BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP

    题意:链接 方法:SPFA+DP 解析:挺好的题目.因为数据范围较小所以用这样的方式能够搞,只是也是挺不好想的. 我们定义cost(i,j)表示从第i天走到第j天运用同一种方式的最小花费,然后因为数据 ...

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

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

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

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

  4. BZOJ 1003: [ZJOI2006]物流运输trans DP+最短路

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

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

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

  6. BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1003 题意: 思路: 首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a ...

  7. 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 ...

  8. BZOJ 1003 ZJOI2006 物流运输trans 动态规划+SPFA

    标题效果:给定一个无向图.输送n日,有一天的某一时刻不能去,更换行考虑k,求总成本 一阶cost[i][j]用于第一i为了天j天正在同一航线的最低消费 这种利用SPFA处理 然后就是移动的法规问题 订 ...

  9. BZOJ 1003[ZJOI2006]物流运输(SPFA+DP)

    Problem 1003. -- [ZJOI2006]物流运输 1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: ...

随机推荐

  1. ViewFlipper

    main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  2. Delphi APP 開發入門(五)GPS 定位功能

    Delphi APP 開發入門(五)GPS 定位功能 分享: Share on facebookShare on twitterShare on google_plusone_share   閲讀次數 ...

  3. Zen-cart产品页面随机调用Wordpress文章

    <?php require('./wordpress所在目录/wp-blog-header.php'); ?><?php$rand_posts = get_posts('number ...

  4. POJ 3253 Fence Repair(简单哈弗曼树_水过)

    题目大意:原题链接 锯木板,锯木板的长度就是花费.比如你要锯成长度为8 5 8的木板,最简单的方式是把21的木板割成13,8,花费21,再把13割成5,8,花费13,共计34,当然也可以先割成16,5 ...

  5. HTTPS原理解析-转

    这篇文章关于Https的讲解真的是太透彻了,转过来备忘. 来源:腾讯bugly 另附两个SSL/TLS的交互详解:一.二 基于此文章的学习总结:下一篇文章 1.HTTPS 基础 HTTPS(Secur ...

  6. Andriod实现推送的解决方案(转)

    Andriod上实现消息推送的一般解决策略 第一种解决方案:C2DM云端推送功能 在Android手机平台上,Google提供了C2DM(Cloudto Device Messaging)服务,该服务 ...

  7. 谈面向对象的编程(Python)

    (注:本文部分内容摘自互联网,由于作者水平有限,不足之处,还望留言指正.) 今天中秋节,也没什么特别的,寻常日子依旧. 谈谈面向对象吧,什么叫面向对象? 那么问题来了,你有对象吗?   嗯,,,那我可 ...

  8. 【python】win10中python3.5.2输入pip出现Fatal error in launcher: Unable to create process using '"'

    系统:windows 10 python版本:3.5.2 出现的错误如下: C:\Users\zhuxy>pip list Fatal error in launcher: Unable to ...

  9. 利用URLConnection来发送POST和GET请求

    URL的openConnection()方法将返回一个URLConnection对象,该对象表示应用程序和 URL 之间的通信链接.程序可以通过URLConnection实例向该URL发送请求.读取U ...

  10. [Android] 录音与播放录音实现

    http://blog.csdn.net/cxf7394373/article/details/8313980 android开发文档中有一个关于录音的类MediaRecord,一张图介绍了基本的流程 ...