HDU 4571 Travel in time(最短路径+DP)(2013 ACM-ICPC长沙赛区全国邀请赛)

Problem Description
  Bob gets tired of playing games, leaves Alice, and travels to Changsha alone. Yuelu Mountain, Orange Island, Window of the World, the Provincial Museum etc...are scenic spots Bob wants to visit. However, his time is very limited, he can’t visit them all. 
  Assuming that there are N scenic spots in Changsha, Bob defines a satisfaction value Si to each spot. If he visits this spot, his total satisfaction value will plus Si. Bob hopes that within the limited time T, he can start at spot S, visit some spots selectively, and finally stop at spot E, so that the total satisfaction value can be as large as possible. It's obvious that visiting the spot will also cost some time, suppose that it takes Ci units of time to visit spot i ( 0 <= i < N ).
  Always remember, Bob can choose to pass by a spot without visiting it (including S and E), maybe he just want to walk shorter distance for saving time. 
  Bob also has a special need which is that he will only visit the spot whose satisfaction value is strictly larger than that of which he visited last time. For example, if he has visited a spot whose satisfaction value is 50, he would only visit spot whose satisfaction value is 51 or more then. The paths between the spots are bi-directional, of course.
 
Input
  The first line is an integer W, which is the number of testing cases, and the W sets of data are following.
  The first line of each test data contains five integers: N M T S E. N represents the number of spots, 1 < N < 100; M represents the number of paths, 0 < M < 1000; T represents the time limitation, 0 < T <= 300; S means the spot Bob starts from. E indicates the end spot. (0 <= S, E < N)
  The second line of the test data contains N integers Ci ( 0 <= Ci <= T ), which means the cost of time if Bob visits the spot i.
  The third line also has N integers, which means the satisfaction value Si that can be obtained by visiting the spot i ( 0 <= Si < 100 ).
  The next M lines, each line contains three integers u v L, means there is a bi-directional path between spot u and v and it takes L units of time to walk from u to v or from v to u. (0 <= u, v < N, 0 <= L <= T)
 
Output
  Output case number in the first line (formatted as the sample output).
  The second line contains an integer, which is the greatest satisfaction value.
If Bob can’t reach spot E in T units of time, you should output just a “0” (without quotation marks).
 
题目大意:有n个点m条无向边,每个点有一个权值和一个花费,每条边有一个花费。现在要从S走到E,允许的最大花费为T。经过一个点的时候可以获得它的权值,但同时也要经受那个点的花费,但每次获得的点权要比上一次或者的点券要大。每个点每条边都可以重复走。问从S走到E能获得的最大点权是多少。
思路:搜素超时无误>_<,正解为DP。先作一次floyd求出每个点之间的距离(因为要用到的点点间距太多了floyd正合适,虽说边少可以用SPFA很快,不过n只有100随便啦),然后重新建一个图,从点权小的点 i 到点权大的点 j 之间连一条边,花费为从 i 到 j 的最短路径。得到的图是一个DAG可以用来做DP。DP[i][j]表示到达第i个点并参观,总花费为j,能获得的最大点权,按拓扑结构的顺序来做DP即可。答案为max{DP[i][j - mat[i][E]]}(可以认为我们呆在一个点不动直到做下一个动作,也就是呆着直到要不够时间到终点了)
PS:有重边要注意(原来不说就是有重边啊……)
PS2:记得输出那个Case啊我就忘了……
PS3:细节看代码,要注意不能到达终点的情况和能到终点权值却为0的情况。
PS4:为了方便做DP我加入了一个附加源点←_←
PS5:同一份题有两条最短路+DP大丈夫?虽说用的最短路和DP都不一样……
 
代码(843MS):
  1 #include <cstdio>
  2 #include <iostream>
  3 #include <cstring>
  4 #include <queue>
  5 using namespace std;
  6 typedef long long LL;
  7 
  8 const int MAXN = 110;
  9 const int MAXE = MAXN * MAXN;
 10 
 11 int n, m, st, ed, T;
 12 int val[MAXN], vis_c[MAXN];
 13 int mat[MAXN][MAXN];
 14 
 15 inline void update_min(int &a, const int &b) {
 16     if(a > b) a = b;
 17 }
 18 
 19 inline void update_max(int &a, const int &b) {
 20     if(a < b) a = b;
 21 }
 22 
 23 struct Solve {
 24     int head[MAXN], indeg[MAXN];
 25     int to[MAXE], next[MAXE], cost[MAXE];
 26     int ecnt;
 27 
 28     void init() {
 29         memset(head, 0, sizeof(head));
 30         memset(indeg, 0, sizeof(indeg));
 31         ecnt = 1;
 32     }
 33 
 34     void add_edge(int u, int v, int c) {
 35         ++indeg[v];
 36         to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;
 37     }
 38 
 39     int dp[MAXN][MAXN * 3];
 40 
 41     int solve() {
 42         memset(dp, -1, sizeof(dp));
 43         dp[n][0] = 0;
 44         queue<int> que; que.push(n);
 45         while(!que.empty()) {
 46             int u = que.front(); que.pop();
 47             for(int i = 1; i <= T; ++i) update_max(dp[u][i], dp[u][i - 1]);
 48             for(int p = head[u]; p; p = next[p]) {
 49                 int &v = to[p];
 50                 for(int i = 0; i <= T - cost[p]; ++i) {
 51                     if(dp[u][i] == -1) continue;
 52                     update_max(dp[v][i + cost[p]], dp[u][i] + val[v]);
 53                 }
 54                 if(--indeg[v] == 0) que.push(v);
 55             }
 56         }
 57         int ans = 0;
 58         mat[n][ed] = mat[st][ed];
 59         for(int i = 0; i <= n; ++i) if(T - mat[i][ed] >= 0)
 60             update_max(ans, dp[i][T - mat[i][ed]]);
 61         //printdp();
 62         return ans;
 63     }
 64 
 65     void printdp() {
 66         for(int i = 0; i < n; ++i) {
 67             for(int j = 0; j <= T; ++j) printf("%d ", dp[i][j]);
 68             printf("
");
 69         }
 70     }
 71 
 72 } G;
 73 
 74 struct Original {
 75     void read() {
 76         memset(mat, 0x3f, sizeof(mat));
 77         for(int i = 0; i < n; ++i) mat[i][i] = 0;
 78         int u, v, c;
 79         for(int i = 0; i < m; ++i) {
 80             scanf("%d%d%d", &u, &v, &c);
 81             update_min(mat[u][v], c);
 82             update_min(mat[v][u], c);
 83         }
 84     }
 85 
 86     void floyd() {
 87         for(int k = 0; k < n; ++k)
 88             for(int i = 0; i < n; ++i)
 89                 for(int j = 0; j < n; ++j) update_min(mat[i][j], mat[i][k] + mat[k][j]);
 90     }
 91 
 92     bool make_G() {
 93         floyd();
 94         if(mat[st][ed] > T) return false;
 95         G.init();
 96         for(int i = 0; i < n; ++i)
 97             for(int j = 0; j < n; ++j)
 98                 if(val[i] < val[j]) G.add_edge(i, j, mat[i][j] + vis_c[j]);
 99         for(int i = 0; i < n; ++i)
100             G.add_edge(n, i, mat[st][i] + vis_c[i]);
101         return true;
102     }
103 } O;
104 
105 int main() {
106     int W;
107     scanf("%d", &W);
108     for(int w = 1; w <= W; ++w) {
109         scanf("%d%d%d%d%d", &n, &m, &T, &st, &ed);
110         for(int i = 0; i < n; ++i) scanf("%d", &vis_c[i]);
111         for(int i = 0; i < n; ++i) scanf("%d", &val[i]);
112         O.read();
113         O.make_G();
114         printf("Case #%d:
%d
", w, G.solve());
115     }
116 }
View Code
原文地址:https://www.cnblogs.com/oyking/p/3274602.html