傻逼错误耗我1h,没给全范围坑我1A....

1003: [ZJOI2006]物流运输trans

Time Limit: 10 Sec Memory Limit: 162 MB

Submit: 5299 Solved: 2183

[Submit][Status][Discuss]

Description

物流公司要把一批货物从码头A运到码头B。由于货物量比较大,需要n天才能运完。货物运输过程中一般要转停好几个码头。物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪。由于各种因素的存在,有的时候某个码头会无法装卸货物。这时候就必须修改运输路线,让货物能够按时到达目的地。但是修改路线是一件十分麻烦的事情,会带来额外的成本。因此物流公司希望能够订一个n天的运输计划,使得总成本尽可能地小。

Input

第一行是四个整数n(1<=n<=100)、m(1<=m<=20)、K和e。n表示货物运输所需天数,m表示码头总数,K表示每次修改运输路线所需成本。接下来e行每行是一条航线描述,包括了三个整数,依次表示航线连接的两个码头编号以及航线长度(>0)。其中码头A编号为1,码头B编号为m。单位长度的运输费用为1。航线是双向的。再接下来一行是一个整数d,后面的d行每行是三个整数P( 1 < P < m)、a、b(1 < = a < = b < = n)。表示编号为P的码头从第a天到第b天无法装卸货物(含头尾)。同一个码头有可能在多个时间段内不可用。但任何时间都存在至少一条从码头A到码头B的运输路线。

Output

包括了一个整数表示最小的总成本。总成本=n天运输路线长度之和+K*改变运输路线的次数。

Sample Input

5 5 10 8

1 2 1

1 3 3

1 4 2

2 3 2

2 4 4

3 4 1

3 5 2

4 5 2

4

2 2 3

3 1 1

3 3 3

4 4 5

Sample Output

32

HINT

前三天走1-4-5,后两天走1-3-5,这样总成本为(2+2)*3+(3+2)*2+10=32

Source

SPFA求出最短路径,然后枚举i,j求出cost【i】【j】即i~j天不用换线的花费;

DP:***f【i】=min(f【i】,f【j-1】+cost【j】【i】(i-j+1)+k);

注意转移时特判下是否为极大(即不满足情况)

f【n】多了次转化,因此ans=f【n】-k

code:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
int read()
{
int x=0,f=1; char ch=getchar();
while (ch<'0' || ch>'9') {if (ch=='-') f=-1; ch=getchar();}
while (ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();}
return x*f;
} int n,m,k,e,d;
struct data{int to,next,len;}edge[100001];
struct dat{int p;int from,to;}cant[1010];
int cnt=1,head[5000];
int f[500];
bool can[500];
int cost[500][500]; void add(int u,int v,int l)
{
cnt++;
edge[cnt].next=head[u]; head[u]=cnt;
edge[cnt].len=l; edge[cnt].to=v;
}
void insert(int u,int v,int l)
{
add(u,v,l);add(v,u,l);
} void damage(int l,int r)
{
memset(can,0,sizeof(can));
for (int i=1; i<=d; i++)
if (cant[i].from>r || cant[i].to<l) continue;
else can[cant[i].p]=1;
} bool visit[5000];
int dis[5000];
#define inf 0x3f
void spfa()
{
queue <int> q;
memset(visit,0,sizeof(visit));
for (int i=1; i<=m; i++) dis[i]=inf;
q.push(1); dis[1]=0; visit[1]=1;
while (!q.empty())
{
int now=q.front(); q.pop();
for (int i=head[now]; i; i=edge[i].next)
{
if (!can[edge[i].to] && dis[now]+edge[i].len<dis[edge[i].to])
{
dis[edge[i].to]=dis[now]+edge[i].len;
if (!visit[edge[i].to])
{
q.push(edge[i].to);
visit[edge[i].to]=1;
}
}
}
visit[now]=0;
}
} int main()
{
n=read(),m=read(),k=read(),e=read();
for (int i=1; i<=e; i++)
{
int u=read(),v=read(),l=read();
insert(u,v,l);
}
d=read();
for (int i=1; i<=d; i++)
{
int p=read(),a=read(),b=read();
cant[i].p=p;
cant[i].from=a,cant[i].to=b;
}
for (int i=1; i<=n; i++)
for (int j=i; j<=n; j++)
{
damage(i,j);
spfa();
cost[i][j]=dis[m];
}
memset(f,inf,sizeof(f));
f[0]=0;
for (int i=1; i<=n; i++)
for (int j=1; j<=i; j++)
if (cost[j][i]>=0x3f3f3f3f || f[j-1]>=0x3f3f3f3f) continue;
else f[i]=min(f[i],f[j-1]+cost[j][i]*(i-j+1)+k);
printf("%d\n",f[n]-k);
return 0;
}

BZOJ-1003 物流运输trans SPFA+DP的更多相关文章

  1. BZOJ 1003 物流运输trans dijstra+dp

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

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

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

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

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

  4. BZOJ 1003 物流运输 (动态规划 SPFA 最短路)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...

  5. BZOJ 1003 物流运输trans

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

  6. BZOJ 1003 - 物流运输 - [最短路+dp]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...

  7. BZOJ 1003 物流运输 题解 【SPFA+DP】

    BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...

  8. BZOJ 1003 物流运输 (dp + dijkstra)

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 8672  Solved: 3678[Submit][Stat ...

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

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

随机推荐

  1. hydra爆破用法

    -R 根据上一次进度继续破解 -S 使用SSL协议连接 -s 指定端口 -l 指定用户名 -L 指定用户名字典(文件) -p 指定密码破解 -P 指定密码字典(文件) -e 空密码探测和指定用户密码探 ...

  2. 敏捷软件开发 原则 模式 与实践 - OCP原则

    最近在读BOB大叔的敏捷软件开发,特别是TDD那一章节,启示真的不少,从测试驱动开发,讲到驱动表明程序设计的意图,从设计意图讲到对象依赖的解耦,从解耦建立Mock对象. 其实是对每个模块都编写单元测试 ...

  3. R语言-merge和rbind

    rbind 使用方式 合并两个数据集,要求两个数据集的列数相等: rbind(parameter1,parameter2) 1 1 合并多个数据集,各个数据集的列数相等: rbind(paramete ...

  4. AndroidStudio出现“Plugin is too old, please update to a more recent”问题

    可能原因: 你AS版本不够高....能够更新的话你更新试下,不能更新删了最新的sdk,不要下载4.4以上的版本 解决方法如下 第一种,最简单,但是不推荐这么做 将build.gradle 里的类似 c ...

  5. Bitbucket免费的私有仓库

    1.官网 https://bitbucket.org/ 2.介绍 知乎:http://www.zhihu.com/question/20053312 建议同时用Bitbucket和Github,理由如 ...

  6. [资料]常用Windows CMD指令

    1. 查找80端口开放情况 netstat -aon|findstr “80″ 2. 用netstat查询端口占用程序的PID,显示列表的最后一列就是占用程序的PID,然后再利用tasklist找到这 ...

  7. mysql-advanced-5.6.23-linux-glibc2.5-x86_64安装

    0,二进制安装: mysql-advanced-5.6.23-linux-glibc2.5-x86_64.zip   1,软件包 mysql-advanced-5.6.23-linux-glibc2. ...

  8. C语言 后缀自增的优先级详解

    // ++ 后缀自增与取地址& ,提领 * (指针里的操作符)的优先级比较 #include<stdio.h> #include<stdlib.h> #include& ...

  9. web前端开发资源整理

    webpack中文文档 http://webpackdoc.com/index.html 饿了么UI http://mint-ui.github.io/#!/zh-cn http://element. ...

  10. js实现网页防止被iframe框架嵌套及几种location.href的区别

    首先我们了解一下几种location.href的区别简单的说:几种location.href的区别js实现网页被iframe框架功能,感兴趣的朋友可以了解下 首先我们了解一下:window.locat ...