BZOJ-1003 物流运输trans SPFA+DP
傻逼错误耗我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的更多相关文章
- BZOJ 1003 物流运输trans dijstra+dp
1003: [ZJOI2006]物流运输trans Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3896 Solved: 1608[Submit] ...
- BZOJ 1003 [ZJOI2006]物流运输trans SPFA+DP
题意:链接 方法:SPFA+DP 解析:挺好的题目.因为数据范围较小所以用这样的方式能够搞,只是也是挺不好想的. 我们定义cost(i,j)表示从第i天走到第j天运用同一种方式的最小花费,然后因为数据 ...
- 【BZOJ1003】1003: [ZJOI2006]物流运输trans SPFA+DP
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- BZOJ 1003 物流运输 (动态规划 SPFA 最短路)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MB Submit: 5590 Solved: 2293 [Submit][Stat ...
- BZOJ 1003 物流运输trans
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...
- BZOJ 1003 - 物流运输 - [最短路+dp]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1003 Time Limit: 10 Sec Memory Limit: 162 MB D ...
- BZOJ 1003 物流运输 题解 【SPFA+DP】
BZOJ 1003 物流运输 题解 Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的 ...
- BZOJ 1003 物流运输 (dp + dijkstra)
1003: [ZJOI2006]物流运输 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 8672 Solved: 3678[Submit][Stat ...
- BZOJ 1003: [ZJOI2006]物流运输(spfa+dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1003 题意: 思路: 首先用spfa计算一下任意两天之内的最短路,dis[a][b]表示的就是在第a ...
随机推荐
- Adobe Scout 使用参考说明
Adobe Scout 用于优化 Flash 内容,是一款极为强大的工具,因为它能让您看到 Flash Player 幕后正在发生的事情.但是若明白 Flash Player 为什么做这些事情,您看到 ...
- Quartz Cron 触发器 Cron Expression 的格式
转自:http://blog.csdn.net/yefengmeander/article/details/5985064 上一文中提到 Cron触发器可以接受一个表达式来指定执行JOB,下面看看这个 ...
- Java反射机制的学习
Java反射机制是Java语言被视为准动态语言的关键性质.Java反射机制的核心就是允许在运行时通过Java Reflection APIs来取得已知名字的class类的相关信息,动态地生成此类,并调 ...
- Word Ladder 未完成
Given two words (beginWord and endWord), and a dictionary, find the length of shortest transformatio ...
- Git差异比对
一. 查看变更还未载入(changed but unstaged,当前没有add 的内容)的文件比对: 只需运行不带任何参数的'git diff'命令即可 二. 查看载入(stage,即已经add)而 ...
- iOS——关于打印控件
20.UIPrintFormatterUIPrintFormatter时打印格式化的抽象基类:展示了传统的可打印的内容对象可以跨页边界.由于打印格式化,打印系统,可以自动打印与打印格式化的内容相关联的 ...
- Sublime Text 之运行 ES6 (基于babel)
本文同步自我的个人博客:http://www.52cik.com/2015/10/21/sublime-text-run-es6.html 之前在博客园里写过一篇<Sublime Text 之运 ...
- SQL Serve允许远程连接的解决方法
(一)用户需要做的第一件事是检查SQL数据库服务器中是否允许远程链接.在SQL 2008服务器中可以通过打开SQL Server 2008管理项目(SQL Server 2008 Management ...
- 翻译-微服务API Gateway
原文地址:http://microservices.io/patterns/apigateway.html,以下是使用google翻译对原文的翻译. 让我们想象一下你正在建立一个使用微服务模式的网上商 ...
- windows API 开发飞机订票系统 图形化界面 (一)
去年数据结构课程设计的作品,c语言实现,图形化界面使用windows API实现. 首发在我csdn博客:http://blog.csdn.net/u013805360/article/details ...