题目描述
1001
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.
Figure 1
Figure 1 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.
输入说明
each input file contains one test case. For each case, the first line contains 4 numbers: Cmax (<= 100), always an even number, is the maximum capacity of each station; N (<= 500), 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 (i=1,...N) 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.
输出说明
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->S1->...->Sp. 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.
算法
图算法:最短路径Dijkstra、DFS
优先级由低到高如下:总时长最短、send数最少、back数最少
先使用Dijkstra算法(本题也可直接用DFS暴力枚举)确定最短用时路径,再在多条最短路径中选出send和back最少的一条路径。
图使用邻接矩阵来表示即可,顶点是自行车站、边的权重是道路用时。这里每个顶点有三种状态:处于half-full的perfect condition不需要执行任何操作、full or empty的problem condition(本题中只有一个这样的车站,题目一开始会指定)以及adjust condition需要配合自行车的调度站点。
DFS
算法思路:回溯
从图中某一顶点 v0 出发,访问该顶点,并依次从v0的所有未被访问过的邻接点中任意选取一个顶点作为新的出发点,用同样的方法访问其它所有顶点,直到所有与v0 连通的顶点均被访问到为止;若此时仍有顶点尚未被访问,则从未被访问过的顶点中任意选取一个顶点作为新的出发点,反复此过程,直至图中所有顶点均被访问过一遍为止。
伪代码:
Dijkstra
算法思路:从源点的所有邻接点中找出距源点最近的邻接点, 这即是该点的最短路径; 并以该邻接点为中间点,比较其它各顶点与源点的路径通过中间点后是否更短,若是则用新路径代替老路径,再从中找出其它各点距源点最近的顶点作为新的中间点,这也是该点的最短路径;反复此过程,直至全部顶点距源点的最短路径均找到为止。该算法是求解单源最短路径。
举例:
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int INF = 0x6FFFFFF; //设置最大值上限
int cmax; //站点的最大容量
int n; //站点总数
int sp; //问题站点下标
int m; //道路总数
int minNeed = INF, minBack = INF;
int e[501][501]; //图的邻接矩阵,INF表示边不存在
int dist[501]; //Dijkstra算法,记录临时路径长度。从起点0(PBST)到结点i的路径长度为dist[i]
bool visit[501]; //记录结点是否被访问过
int weight[501]; //记录每个顶点的权重(每个站点的容量)
vector<int> pre[501]; //pre[i]记录从起点0到结点i最短路径中,i的前一个顶点
vector<int> path, tempPath;
//DFS搜索方向:从problem station到0,这样打印时是正序
void DFS(int v) {
tempPath.push_back(v);
if (0 == v) {
int need = 0, back = 0;
for (int i = tempPath.size() - 1; i >= 0; i--) {
int id = tempPath[i];
if (weight[id] > 0) //比完美数目多,返还自行车
back += weight[id];
else {
if (back > (0 - weight[id])) {
//back数目足够支持,先从back里扣除
back += weight[id];
}
else {
//back数不够,从need里借
need += (0 - weight[id]) - back;
back = 0;
}
}
}
if (need < minNeed) {
minNeed = need;
minBack = back;
path = tempPath;
}
else if (need == minNeed && back < minBack) {
minBack = back;
path = tempPath;
}
tempPath.pop_back();
return;
}
for (int i = 0; i < pre[v].size(); i++)
DFS(pre[v][i]);
tempPath.pop_back();
}
int main(void) {
//数据初始化
fill(e[0], e[0] + 501 * 501, INF);
fill(dist, dist + 501, INF);
dist[0] = 0; //从0点到自身的距离是0
//读入数据
cin >> cmax >> n >> sp >> m;
for (int i = 1; i <= n; i++) {
cin >> weight[i];
weight[i] = weight[i] - cmax / 2; //求得站点的自行车数和完美数目的差距
}
for (int i = 1; i <= m; i++) {
int a, b;
cin >> a >> b;
cin >> e[a][b];
e[b][a] = e[a][b]; //无向图邻接矩阵对称
}
//Dijkstra算法寻找最短路径
for (int i = 0; i <= n; i++) {
int u = -1, minn = INF;
for (int j = 0; j <= n; j++) {
if (visit[j] == false && dist[j] < minn) {
minn = dist[j];
u = j;
}
}
if (u == -1) break;
visit[u] = true;
for (int v = 0; v <= n; v++) {
if (visit[v] == false && e[u][v] != INF) {
//更新u到v的最小值
if (dist[v] > dist[u] + e[u][v]) {
dist[v] = dist[u] + e[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if (dist[v] == dist[u] + e[u][v]) {
//本题要着重考虑存在多个最短路径的情况
pre[v].push_back(u);
}
}
}
}
//对problem station 进行DFS,从多个路径中选择最符合条件的一个
DFS(sp);
std::cout << minNeed << " 0";
for (int i = path.size() - 2; i >= 0; i--)
std::cout << "->" << path[i];
std::cout << " " << minBack;
//system("pause");
return 0;
}