题目描述
物流公司要把一批货物从码头A运到码头B。由于货物量比较大,需要n天才能运完。货物运输过程中一般要转停好几个码头。物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格的管理和跟踪。由于各种因素的存在,有的时候某个码头会无法装卸货物。这时候就必须修改运输路线,让货物能够按时到达目的地。但是修改路线是一件十分麻烦的事情,会带来额外的成本。因此物流公司希望能够订一个n天的运输计划,使得总成本尽可能地小。
输入格式
第一行是四个整数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的运输路线。
输出格式
包括了一个整数表示最小的总成本。总成本=n天运输路线长度之和+K*改变运输路线的次数。
题目解析
这道题的数据量很小,貌似暴力可过?我们对每一个,求出
,表示在第
天到第
天中不改变方案时的花费(最短距离乘上天数,Dijkstra或SPFA)。剩下就是DP,
表示到第
天的最小花费,则有
,注意初始条件为
,因为第一个方案不需要额外的
费用。数据很小,
暴力DP就好。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
/************************************************************** Problem: 1003 User: frank_c1 Language: C++ Result: Accepted Time:56 ms Memory:1380 kb ****************************************************************/ #include <set> #include <map> #include <queue> #include <ctime> #include <cmath> #include <cstdio> #include <vector> #include <string> #include <cctype> #include <bitset> #include <cstring> #include <cstdlib> #include <utility> #include <iostream> #include <algorithm> #define REP(i,a,b) for(int i=(a);i<=(b);i++) #define PER(i,a,b) for(int i=(a);i>=(b);i--) #define RVC(i,S) for(int i=0;i<(S).size();i++) #define RAL(i,u) for(int i=fr[u];i!=-1;i=e[i].next) using namespace std; typedef long long LL; typedef pair<int,int> pii; template<class T> inline void read(T& num) { bool start=false,neg=false; char c; num=0; while((c=getchar())!=EOF) { if(c=='-') start=neg=true; else if(c>='0' && c<='9') { start=true; num=num*10+c-'0'; } else if(start) break; } if(neg) num=-num; } /*============ Header Template ============*/ const int maxn=25; const int maxd=105; const int maxm=805; const LL INF=(LL)(1e15); edge e[maxm]; int fr[maxn]; bool inq[maxn]; bool ok[maxn][maxd]; int cnt[maxn]; LL d[maxn]; LL cost[maxd][maxd]; LL f[maxd]; int n,ds,st,ed,tote=0; inline void addone(int u,int v,int d) { ++tote;e[tote].next=fr[u];fr[u]=tote; e[tote].to=v;e[tote].dist=d; } inline void addedge(int u,int v,int d) { addone(u,v,d);addone(v,u,d); } bool in_law(int u) { REP(i,st,ed) if(!ok[u][i]) return false; return true; } queue<int> Q; bool SPFA(int s) { REP(i,1,n) d[i]=INF; memset(cnt,0,sizeof(cnt)); memset(inq,0,sizeof(inq)); d[s]=0;inq[s]=1;Q.push(s); while(!Q.empty()) { int u=Q.front(); Q.pop();inq[u]=0; RAL(i,u) if(in_law(e[i].to) && d[e[i].to]>d[u]+e[i].dist) { d[e[i].to]=d[u]+e[i].dist; if(!inq[e[i].to]) { inq[e[i].to]=1; Q.push(e[i].to); if(++cnt[e[i].to]>n) return false; } } } return true; } void solve() { int k,ne,q; read(ds);read(n); read(k);read(ne); REP(i,1,n) fr[i]=-1; REP(i,1,ne) { int u,v;LL d; read(u);read(v);read(d); addedge(u,v,d); } REP(i,1,n) REP(j,1,ds) ok[i][j]=true; read(q); REP(i,1,q) { int p,a,b; read(p);read(a);read(b); REP(j,a,b) ok[p][j]=false; } for(st=1;st<=ds;st++) for(ed=st;ed<=ds;ed++) { SPFA(1); cost[st][ed]=d[n]*(ed-st+1); } REP(i,1,ds) f[i]=INF; f[0]=-k; REP(i,1,ds) REP(j,0,i-1) f[i]=min(f[i],f[j]+cost[j+1][i]+k); printf("%lld\n",f[ds]); } int main() { solve(); return 0; } |