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

dp[i]=min(dp[j]+dur[j+1][i]*(i-j)+K)  dur(i,j)表示从i天到j天不换路的最短距离

1003: [ZJOI2006]物流运输trans

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 4001  Solved: 1671
[Submit][Status][Discuss]

Description

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

Input

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

Output

包括了1个整数表示最小的总本钱。总本钱=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

前3天走1⑷⑸,后两天走1⑶⑸,这样总本钱为(2+2)*3+(3+2)*2+10=32

Source

/* ***********************************************
Author :CKboss
Created Time :2015年04月30日 星期4 10时58分56秒
File Name :BZOJ1003.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

typedef long long int LL;

const int maxn=1000;
const LL INF=0x3f3f3f3f3f3f3f3f;

int n,m,K,e,d;

struct Edge
{
int to,next,len;
}edge[maxn];

int Adj[50],Size;

void init()
{
memset(Adj,⑴,sizeof(Adj)); Size=0;
}

void add_edge(int u,int v,int len)
{
edge[Size].to=v; edge[Size].next=Adj[u];
edge[Size].len=len; Adj[u]=Size++;
}

void Add_Edge(int u,int v,int len) { add_edge(u,v,len); add_edge(v,u,len); }

bool go[50][200];
LL dur[200][200];
bool cango[50];

/*************spfa********************/

int dist[50],cq[50];
bool inq[50];

bool spfa()
{
memset(dist,63,sizeof(dist));
memset(cq,0,sizeof(cq));
memset(inq,false,sizeof(inq));
dist[1]=0; queue<int> q;
q.push(1); inq[1]=true; cq[1]=1;

while(!q.empty())
{
int u=q.front(); q.pop();
for(int i=Adj[u];~i;i=edge[i].next)
{
int v=edge[i].to;
int len=edge[i].len;
if(cango[v]==false) continue;
if(dist[v]>dist[u]+len)
{
dist[v]=dist[u]+len;
if(!inq[v])
{
inq[v]=true;
cq[v]++;
if(cq[v]>=m) return false;
q.push(v);
}
}
}
inq[u]=false;
}
return true;
}

LL dp[200];

int main()
{
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);

init();
memset(go,true,sizeof(go));
scanf("%d%d%d%d",&n,&m,&K,&e);
for(int i=0;i<e;i++)
{
int u,v,l;
scanf("%d%d%d",&u,&v,&l);
Add_Edge(u,v,l);
}
scanf("%d",&d);
for(int i=0;i<d;i++)
{
int p,a,b;
scanf("%d%d%d",&p,&a,&b);
for(int j=a;j<=b;j++) go[p][j]=false;
}
for(int i=1;i<=n;i++)
{
for(int j=i;j<=n;j++)
{
memset(cango,true,sizeof(cango));
for(int k=1;k<=m;k++)
{
bool fg=true;
for(int l=i;l<=j&&fg;l++)
if(go[k][l]==false) fg=false;
if(fg==false) cango[k]=false;
}
spfa();
dur[i][j]=1LL*dist[m];
}
}
memset(dp,63,sizeof(dp));
dp[0]=0;dp[1]=dur[1][1];
for(int i=2;i<=n;i++)
{
if(dur[1][i]!=INF) dp[i]=dur[1][i]*i;
for(int j=1;j+1<=i;j++)
{
if(dur[j+1][i]==INF) continue;
dp[i]=min(dp[i],dp[j]+dur[j+1][i]*(i-j)+K);
}
}
printf("%lld
",dp[n]);
return 0;
}

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » BZOJ 1003: [ZJOI2006]物流运输trans DP+SPFA

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡