您的位置:首页 > 其它

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

2015-04-30 13:37 471 查看
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

物流公司要把一批货物从码头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



/* ***********************************************
Author :CKboss
Created Time :2015年04月30日 星期四 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,-1,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\n",dp
);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: