1018 Public Bike Management (30 分)

There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city.

The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations. A station is said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC will collect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will be adjusted as well.

When a problem station is reported, PBMC will always choose the shortest path to reach that station. If there are more than one shortest path, the one that requires the least number of bikes sent from PBMC will be chosen.

The above figure illustrates an example. The stations are represented by vertices and the roads correspond to the edges. The number on an edge is the time taken to reach one end station from another. The number written inside a vertex S is the current number of bikes stored at S. Given that the maximum capacity of each station is 10. To solve the problem at S3​​, we have 2 different shortest paths:

  1. PBMC -> S1​​ -> S3​​. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1​​ and then take 5 bikes to S3​​, so that both stations will be in perfect conditions.

  2. PBMC -> S2​​ -> S3​​. This path requires the same time as path 1, but only 3 bikes sent from PBMC and hence is the one that will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cmax​​ (≤), always an even number, is the maximum capacity of each station; N (≤), the total number of stations; Sp​​, the index of the problem station (the stations are numbered from 1 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers Ci​​ (,) where each Ci​​ is the current number of bikes at Si​​ respectively. Then M lines follow, each contains 3 numbers: Si​​, Sj​​, and Tij​​ which describe the time Tij​​ taken to move betwen stations Si​​ and Sj​​. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print your results in one line. First output the number of bikes that PBMC must send. Then after one space, output the path in the format: 0. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Sp​​ is adjusted to perfect.

Note that if such a path is not unique, output the one that requires minimum number of bikes that we must take back to PBMC. The judge's data guarantee that such a path is unique.

Sample Input:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

Sample Output:

3 0->2->3 0

题目分析:用Dijkstra算法求解后不对 看了柳神的博客才知道要使用dijkstra与dfs结合的方式
因为minneed和minback在求解过程中不满足最优子结构 换言之 无法在求解过程中就知道那条路是最优的,因此,将最短路径求出后在利用dfs进行遍历判断最优解
 1 #define _CRT_SECURE_NO_WARNINGS
 2 #include<iostream>
 3 #include<vector>
 4 #include<queue>
 5 #include<algorithm>
 6 #define INIFITY 65535
 7 using namespace std;
 8 int g[520][520];
 9 int dist[520];
10 int collected[520];
11 int weight[520];
12 int minNeed = INIFITY;
13 int minBack = INIFITY;
14 vector<int> pre[520], path, tempath;
15 int C, N, D, M;
16 void dfs(int v){
17     tempath.push_back(v);
18     if (v == 0) {
19         int need = 0;
20         int back = 0;
21         for (int i = tempath.size()-1; i>=0; i--){
22             int id = tempath[i];
23             if (weight[id] > 0)
24                 back += weight[id];
25             else if (back>(0-weight[id]))
26                 back += weight[id];
27             else{
28                 need+= (0 - weight[id]) - back;
29                 back = 0;
30             }
31         }
32         if (need < minNeed){
33             minNeed = need;
34             minBack = back;
35             path = tempath;
36         }
37         else if (need == minNeed && back < minBack){
38             minBack=back;
39             path = tempath;
40         }
41         tempath.pop_back();
42         return;
43     }
44     for (int i = 0; i<pre[v].size(); i++)
45         dfs(pre[v][i]);
46     tempath.pop_back();
47 }
48 int main()
49 {
50     fill(g[0], g[0] + 520 * 520, INIFITY);
51     fill(dist, dist + 520, INIFITY);
52     cin >> C >> N >> D >> M;
53     for (int i = 1; i <=N; i++)
54     {
55         int w;
56         cin >> w;
57         weight[i] = w - C / 2;
58     }
59     for (int i = 0; i < M; i++){
60         int v1, v2, length;
61         cin >> v1 >> v2 >> length;
62         g[v1][v2]=g[v2][v1]= length;
63     }
64     //Dijkstra
65     dist[0] = 0;
66     for (int i = 0; i <= N; i++){
67         int Min = INIFITY;
68         int Minp = -1;
69         for (int v = 0; v <= N; v++){
70             if (!collected[v]&&dist[v] < Min){
71                 Min = dist[v];
72                 Minp = v;
73             }
74         }
75         collected[Minp] = 1;
76         for (int u = 0; u <= N; u++){
77             if(!collected[u]&&g[Minp][u]!=INIFITY)
78                 if (dist[Minp] + g[Minp][u] < dist[u]){
79                     dist[u] = dist[Minp] + g[Minp][u];
80                     pre[u].clear();
81                     pre[u].push_back(Minp);
82                 }
83                 else if (dist[u] == dist[Minp] + g[Minp][u])
84                     pre[u].push_back(Minp);
85         }
86     }
87     dfs(D);
88     cout << minNeed << " 0";
89     for (int i = path.size() - 2; i >= 0; i--)
90         cout << "->" << path[i];
91     cout << " " << minBack;
92     return 0;
93 }
View Code
原文地址:https://www.cnblogs.com/57one/p/11922878.html