http://www.lydsy.com/JudgeOnline/problem.php?id=1003

题意:

思路:

首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a天~第b天从1到m的最短路。

接下来就是dp了,f[i]表示前i天的最小代价,那么状态转移方程就是:

f[i]=min(f[i],f[j]+dis[j+][i]*(i-j)+k)

注意:边界条件f[0]=-k!

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<sstream>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
typedef pair<int,ll> pll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m, k, t;
int tot;
int head[maxn];
int flag[maxn][maxn];
int dis[maxn][maxn];
int d[maxn];
int inq[maxn];
ll f[maxn]; struct node
{
int v, w;
int next;
}e[maxn]; void AddEdge(int u, int v, int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].next=head[u];
head[u]=tot++;
} void spfa(int s, int a, int b)
{
for(int i=;i<=m;i++) d[i]=INF;
queue<int> Q;
Q.push(s);
d[s]=;
while(!Q.empty())
{
int u=Q.front(); inq[u]=; Q.pop();
for(int i=head[u];i!=-;i=e[i].next)
{
int v=e[i].v;
if(flag[v][b]-flag[v][a-]>) continue;
if(d[v]>d[u]+e[i].w)
{
d[v]=d[u]+e[i].w;
if(!inq[v])
{
Q.push(v);
inq[v]=;
}
}
}
}
dis[a][b]=d[m];
} int main()
{
//freopen("in.txt","r",stdin);
while(~scanf("%d%d%d%d",&n,&m,&k,&t))
{
tot=;
memset(head,-,sizeof(head));
memset(flag,,sizeof(flag));
for(int i=;i<t;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
AddEdge(u,v,w);
AddEdge(v,u,w);
} scanf("%d",&t);
while(t--)
{
int x,a,b;
scanf("%d%d%d",&x,&a,&b);
for(int i=a;i<=b;i++) flag[x][i]=;
} for(int i=;i<=m;i++)
for(int j=;j<=n;j++) flag[i][j]+=flag[i][j-]; //用前缀和可以快速判断i~j天是否可用
94
for(int i=;i<=n;i++)
for(int j=i;j<=n;j++) spfa(,i,j); f[] = -k; //注意边界条件
for(int i = ; i <= n; i ++)
{
f[i] = INF;
for(int j = ; j < i; j++)
f[i] = min(f[i], f[j] + 1LL*dis[j+][i]*(i-j) + k);
}
printf("%d\n",f[n]);
}
return ;
}

BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)的更多相关文章

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

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

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

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

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

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

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

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

  5. bzoj 1003 [ZJOI2006]物流运输(最短路+dp)

    [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8973  Solved: 3839[Submit][Status][Di ...

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

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

  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 ★(Dijkstra + DP)

    题目链接 http://www.lydsy.com/JudgeOnline/problem.php?id=1003 思路 先Dijkstra暴力求出i..j天内不变换路线的最少花费,然后dp[i] = ...

  9. BZOJ.1003.[ZJOI2006]物流运输(DP 最短路Dijkstra)

    题目链接 容易看出是个最短路+DP.既然答案和天数有关,那么就令\(f[i]\)表示前\(i\)天最小成本. 这个转移很好想: \(f[i]=\min(f[i],\ f[j]+cost(j+1,i)+ ...

随机推荐

  1. poj2524(简单并查集)

    #include <iostream>#include <stdio.h>#include <string.h>#include <stdlib.h>u ...

  2. [PAT]A+B Format[简单]

    1001 A+B Format (20)(20 分) Calculate a + b and output the sum in standard format -- that is, the dig ...

  3. 使用Python2.7 POST 数据到 onenet 平台

    功能 发送数据名称为SENSORID(这里用TEST测试),数值为VALUE(这里用49值做测试)的数据,发送到自己的onenet对应设备 效果发送成功 代码 # -*- coding: utf-8 ...

  4. [LeetCode] 694. Number of Distinct Islands

    Given a non-empty 2D array grid of 0's and 1's, an island is a group of 1's (representing land) conn ...

  5. matplotlib —— 调整坐标轴

    import matplotlib.pyplot as plt import numpy as np # 绘制普通图像 x = np.linspace(-1, 1, 50) y1 = 2 * x + ...

  6. python -- 解决If using all scalar values, you must pass an index问题

    [问题描述] 在将dict转为DataFrame时会报错:If using all scalar values, you must pass an index 例如: summary = pd.Dat ...

  7. linux命令:帮助命令

    帮助命令:man 命令名称:man 命令英文原意:manual 命令所在路径:/usr/bin/man 执行权限:所有用户 语法:man [命令或配置文件] 功能描述:获得帮助信息 范例:$man l ...

  8. UVM中Callback机制

    Callback机制,其实是使用OOP来实现的一种程序开发者向程序使用者提供的模块内部的接口.可以在Test_case的高度改变其他component的一些行为. Systemverilog中已经提供 ...

  9. cc150 --链表分割

    题目描述 编写代码,以给定值x为基准将链表分割成两部分,所有小于x的结点排在大于或等于x的结点之前 给定一个链表的头指针 ListNode* pHead,请返回重新排列后的链表的头指针.注意:分割以后 ...

  10. 完整table

    .table-bordered{ border:1px solid #cccccc; } .table { border-spacing: 0;/*设置或检索当表格边框独立时(即border-coll ...