傻逼错误耗我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;
}

最新文章

  1. 对jQuery选择器的总结
  2. Android Paint类方法说明
  3. 一句命令快速合并 JS、CSS
  4. 使用宏定义来减少JNI的繁琐
  5. android设备之间屏幕共享
  6. 双击GridView查看详情
  7. AlarmManager的学习与实现
  8. HTML a标签 target属性作用
  9. php变量双击选择无法选择$符号
  10. [转载] 使用 Twitter Storm 处理实时的大数据
  11. Eclipse 配置scala开发环境(windows)
  12. 关于new Date()的日期格式处理
  13. python学习Day2 python 、pycharm安装及环境变量配置
  14. 23.HashMap
  15. PC高级语言与施耐德、罗克韦尔、台达等PLC的Modbus通讯源代码(ModbusTCP.DLL/ModbusRTU.DLL)
  16. dubbo环境搭建与tomcat集成、DEMO示例、常见问题(最完整版本、带管理控制台、监控中心、zookeeper)
  17. 【剑指offer】替换空格
  18. Python staticmethod() 函数
  19. JS 返回上一页并刷新,但不用重新加载整个页面(ajax实现)
  20. Labeled Faces in the Wild 人脸识别数据集 部分测试数据

热门文章

  1. php strcmp引起的问题
  2. 【转】【SEE】基于SSE指令集的程序设计简介
  3. C语言 百炼成钢17
  4. python多进程共享变量Value使用tips
  5. Java从0开始学——字符串
  6. linux内核分析 课程总结
  7. openssl实践总结
  8. 一个bug案例分析
  9. .NET Web开发初学者必知的四个网站
  10. 转 Windows server 2008 搭建VPN服务