zoukankan      html  css  js  c++  java
  • 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;
    dijkstra.addEdge(a,b,w);
    }
    int d;
    scanf("%d",&d);
    while(d--) {
    scanf("%d%d%d",&w,&a,&b);
    dijkstra.add(a,b,--w);
    }
    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 ",dp.work(c));
    return 0;
    }

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

    1003: [ZJOI2006]物流运输trans

    Time Limit: 10 Sec  Memory Limit: 162 MB
    Submit: 3924  Solved: 1625
    [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

  • 相关阅读:
    Leetcode Unique Binary Search Trees
    Leetcode Decode Ways
    Leetcode Range Sum Query 2D
    Leetcode Range Sum Query
    Leetcode Swap Nodes in Pairs
    Leetcode Rotate Image
    Leetcode Game of Life
    Leetcode Set Matrix Zeroes
    Leetcode Linked List Cycle II
    CF1321A
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/4374649.html
Copyright © 2011-2022 走看看