# BZOJ 1003: [ZJOI2006]物流运输trans(最短路+dp)

1A,爽!

cost[i][j]表示从第i天到第j天不改路线所需的最小花费,这个可以用最短路预处理出.然后dp(i)=cost[j][i]+dp(j-1)+c. c为该路线的花费.

-------------------------------------------------------------------------------

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>

#define rep(i,n) for(int i=0;i<n;i++)
#define clr(x,c) memset(x,c,sizeof(x))
#define Rep(i,l,r) for(int i=l;i<=r;++i)

using namespace std;

const int inf=0x7fffffff;
const int maxn=25,maxday=105;

int cost[maxday][maxday];

struct DIJKSTRA {
struct Edge {
int u,v,d;
Edge(int u,int v,int d):u(u),v(v),d(d) {}
};
struct node {
int u,d;
node(int u,int d):u(u),d(d) {}
bool operator < (const node &o) const {
return d>o.d;
}
};
int n,s,t,day[2];
int d[maxn];
int ok[maxn][maxday];
vector<int> g[maxn];
vector<Edge> edges;
void init(int n) {
this->n=n;
rep(i,n) g[i].clear();
edges.clear();
clr(ok,-1);
}
void addEdge(int u,int v,int d) {
edges.push_back( (Edge) {u,v,d} );
g[u].push_back(edges.size()-1);
g[v].push_back(edges.size()-1);
}
inline void add(int a,int b,int p) {
Rep(i,a,b) ok[p][i]=0;
}
inline bool jud(int o) {
Rep(i,day[0],day[1])
if(!ok[o][i]) return 0;
return 1;
}
int Dijkstra(int s,int t,int a,int b) {
this->s=s; this->t=t;
day[0]=a; day[1]=b;
rep(i,n) d[i]=inf; d[s]=0;
priority_queue<node> q;
if(jud(s)) q.push( (node) {s,0} );
while(!q.empty()) {
node x=q.top(); q.pop();
if(d[x.u]!=x.d) continue;
rep(i,g[x.u].size()) {
Edge &e=edges[g[x.u][i]];
int u=e.u,v=e.v;
if(x.u!=u) swap(u,v);
if(!jud(v)) continue;
if(d[v]>d[u]+e.d) {
d[v]=d[u]+e.d;
q.push( (node) {v,d[v]} );
}
}
}
return d[t];
}
} dijkstra;

struct DP {
int d[maxday];
int n;
void init(int n) {
this->n=n;
d[0]=0;
Rep(i,1,n)
d[i]= cost[1][i]==inf ? inf : cost[1][i];
}
int work(int c) {
Rep(i,1,n)
Rep(j,2,i) if(cost[j][i]!=inf)
d[i]=min(d[i],d[j-1]+cost[j][i]+c);
return d[n];
}
} dp;
int main()
{
freopen("test.in","r",stdin);
freopen("test.out","w",stdout);
int n,m,c,e,a,b,w;
scanf("%d%d%d%d",&n,&m,&c,&e);
dijkstra.init(m);
while(e--) {
scanf("%d%d%d",&a,&b,&w);
--a; --b;
}
int d;
scanf("%d",&d);
while(d--) {
scanf("%d%d%d",&w,&a,&b);
}
Rep(i,1,n)
Rep(j,i,n) {
cost[i][j]=dijkstra.Dijkstra(0,m-1,i,j);
if(cost[i][j]!=inf) cost[i][j]*=(j-i+1);
}
dp.init(n);
printf("%d\n",dp.work(c));
return 0;
}

-------------------------------------------------------------------------------

## 1003: [ZJOI2006]物流运输trans

Time Limit: 10 Sec  Memory Limit: 162 MB
Submit: 3924  Solved: 1625
[Submit][Status][Discuss]

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

32

## HINT

posted @ 2015-03-28 19:11  JSZX11556  阅读(288)  评论(0编辑  收藏  举报