原题链接:1018 Public Bike Management
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
S
S. Given that the maximum capacity of each station is 10. To solve the problem at
S
3
S_3
S3, we have 2 different shortest paths:
Each input file contains one test case. For each case, the first line contains 4 numbers: C m a x C_{max} Cmax(≤100), always an even number, is the maximum capacity of each station; N N N(≤500), the total number of stations; S p S_p Sp, the index of the problem station (the stations are numbered from 1 to N N N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers C i ( i = 1 , 2 , ⋯ , N ) C_i(i=1,2,\cdots,N) Ci(i=1,2,⋯,N)where each C i C_i Ci is the current number of bikes at S i S_i Si respectively. Then M M M lines follow, each contains 3 numbers: S i , S j , T i j S_i, S_j, T_{ij} Si,Sj,Tij which describe the time T i j T_{ij} Tij taken to move betwen stations S i S_i Si and S j S_j Sj. All the numbers in a line are separated by a space.
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
0
0 ->
S
1
S_1
S1 ->
⋯
\cdots
⋯ ->
S
p
S_p
Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of
S
p
S_p
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.
10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1
3 0->2->3 0
读题:最短路径问题的变化,需要从一个节点走最短路径到另一个节点(最短路径可能不止一条),每个节点都会有一些点数,然后要求输出满足点数大于等于某一个值,且点数尽可能小的最短路径
那么对于本题而言,可以先使用最短路算法找出最短路径,记录所有最短路径,然后寻找符合题意得答案即可
#include
using namespace std;
const int num_node=510;
struct Edge{
int to, time;
Edge() {}
Edge(int to, int time):to(to),time(time) {}
};
vector<int> ans_path, temp_path; int ans_in=INT_MAX, ans_out=INT_MAX, target_capacity, capacity[num_node];
vector<Edge> edge[num_node]; vector<int> pre[num_node];
void dfs(int station) {
if (station == 0) {
int out=0, cur=0; // 表示需要PBMC送出的自行车 过程中携带的自行车数量
for (int i=temp_path.size()-1;i>=0;i--) {
// 倒着遍历 表示从PBMC起始节点出发得过程
if (capacity[temp_path[i]] < target_capacity) {
if (cur >= target_capacity-capacity[temp_path[i]]) cur -= target_capacity-capacity[temp_path[i]];
else {
out += target_capacity-capacity[temp_path[i]]-cur; cur=0;
}
} else cur += capacity[temp_path[i]] - target_capacity;
}
if (out < ans_out) {
ans_path.assign(temp_path.begin(), temp_path.end());
ans_out = out; ans_in = cur;
} else if (out == ans_out && cur < ans_in) {
ans_path.assign(temp_path.begin(), temp_path.end());
ans_in = cur;
}
return;
}
temp_path.push_back(station);
for (int i=0;i<pre[station].size(); i++) dfs(pre[station][i]);
temp_path.pop_back();
}
int main () {
int n,sp,m; scanf("%d %d %d %d", &target_capacity, &n, &sp, &m);
target_capacity /= 2;
for (int i=1;i<=n;i++) scanf("%d", &capacity[i]);
int from, to, time;
for (int i=0;i<m;i++) {
scanf("%d %d %d", &from, &to, &time);
edge[from].push_back(Edge(to, time));
edge[to].push_back(Edge(from, time));
}
int dis[num_node] = {0}; bool vis[num_node] = {false};
memset(dis, 127, sizeof(dis)); dis[0]=0;
memset(vis, 0, sizeof(vis));
for (int count=0;count<n;count++) {
int index=num_node-1;
for (int i=0;i<=n;i++) if (!vis[i] && dis[i] < dis[index]) index=i;
vis[index] = true;
for (int i=0;i<edge[index].size();i++) {
if (dis[edge[index][i].to] > dis[index]+edge[index][i].time) {
dis[edge[index][i].to] = dis[index]+edge[index][i].time;
pre[edge[index][i].to].clear(); pre[edge[index][i].to].push_back(index);
} else if (dis[edge[index][i].to] == dis[index]+edge[index][i].time) pre[edge[index][i].to].push_back(index);
}
}
dfs(sp);
printf("%d 0", ans_out);
for (int i=ans_path.size()-1;i>=0;i--) printf("->%d", ans_path[i]);
printf(" %d\n", ans_in);
return 0;
}