P1772 [ZJOI2006] transport stream [DP + Shortest]

Title Description

Logistics company put a number of goods from the pier to pier A B. As the goods than larger, n we need days to complete operation. Transportation of goods in general to turn several parked pier. Logistics companies often design a fixed transport routes for the implementation of strict management and tracking of the entire transport process. Due to various factors, sometimes a pier will not load and unload cargo. This time we have to change transport routes, so that goods can reach their destinations on time. But revised route is - something very troublesome parts, will bring additional costs. Therefore, the logistics company hopes to set a n-day transportation plan, so that the total cost as low as possible.

Resolve

See the optimal solution is not difficult to think of dp.

We as a daily dp stage, which way to dp the state, the current route can not be exchanged for decision-making. For solving each state, we ran the shortest line. As for the decision, is easy to find every time we change the route to go, we will take this new route at least 1 day. In other words, we are from \ (i \) transfer of state day of the first \ (j \) days when the state ( \ (J> i \) ), it may be assumed \ (i \ sim j \) day we go all the same road, this road is that these days can take the shortest point constituted. These days, some point is not to go, we like to play tag.

Set \ (L \) for the first \ (i \ sim j \) days to go the shortest length, so there is a state transition equation:
\ [DP [I] = min (DP [I], DP [J-. 1] + (i-j + 1) * l + k) \]

Reference Code

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<queue>
#include<set>
#include<map>
#define N 401
#define INF 0x3f3f3f3f
using namespace std;
inline int read()
{
    int f=1,x=0;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
    return x*f;
}
struct rec{
    int next,ver,edge;
}g[N];
int head[N],tot,n,m,e,k,d[N],dp[N];
bool v[N],vis[N][N];
inline void add(int x,int y,int val)
{
    g[++tot].ver=y,g[tot].edge=val;
    g[tot].next=head[x],head[x]=tot;
}
inline int dijkstra(int l,int r)
{
    memset(v,0,sizeof(v));
    memset(d,0x3f,sizeof(d));
    priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > q;
    d[1]=0;
    q.push(make_pair(0,1));
    for(int i=l;i<=r;++i)
        for(int j=1;j<=m;++j)
            if(vis[j][i]) v[j]=1;
    while(q.size()){
        int x=q.top().second;q.pop();
        if(v[x]) continue;
        v[x]=1;
        for(int i=head[x];i;i=g[i].next){
            int y=g[i].ver,z=g[i].edge;
            if(d[y]>d[x]+z){
                d[y]=d[x]+z;
                q.push(make_pair(d[y],y));
            }
        }
    }
    return d[m];
}
int main()
{
    int p;
    n=read(),m=read(),k=read(),e=read();
    for(int i=1;i<=e;++i){
        int u,v,val;
        u=read(),v=read(),val=read();
        add(u,v,val),add(v,u,val);
    }
    p=read();
    for(int i=1;i<=p;++i){
        int x,l,r;
        x=read(),l=read(),r=read();
        for(int j=l;j<=r;++j)
            vis[x][j]=1;
    }
    for(int i=1;i<=n;++i){
        dp[i]=dijkstra(1,i);
        if(dp[i]!=INF) dp[i]=dp[i]*i;
        for(int j=1;j<=i;++j){
            int l=dijkstra(j,i);
            if(l==INF) continue;
            dp[i]=min(dp[i],dp[j-1]+(i-j+1)*l+k);
        }
    }
    printf("%d\n",dp[n]);
    return 0;
}

Guess you like

Origin www.cnblogs.com/DarkValkyrie/p/11567727.html