PTA甲级考试真题练习18——1018 Public Bike Management
程序员文章站
2022-06-07 13:14:14
...
题目
生词生字
- The Public Bike Management Center (PBMC) keeps monitoring the real-time capacity of all the stations
公共自行车管理中心(PBMC)持续监控所有车站的实时通行能力 - A station is said to be in perfect condition if it is exactly half-full.
一个站点如果正好处于半满状态被认为是完美的情况 - The stations are represented by vertices and the roads correspond to the edges
站点用顶点表示并且边表示路长
思路
较难的求图的单源最短路径题,难点在于题意,题意读懂就好办
题目主要是说从PBMC开始向目的地一步一步的走,如果当前站点差自行车,则立即补上,并且累加总的需求和,如果不差或者多余,把多余的自行车累加到下一个站点,直到最终站点,最终站点如果有多的则全部返回PBMC中,后面的站点如果多余是不能往前面补充的,最终按照最短路径<最小需求<最小返回进行排序,输出第一个
由题意可知,该题最佳算法是深度遍历不是Dijkstra算法,因为在深度遍历的过程中可以将每个站点的状态判断清楚,累加需求和并且将多余的传到下一个站点
#include <iostream>
#include<string.h>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<list>
using namespace std;
#define nmax 1000 //一个用作示例的小顺序表的最大长度
#define mint 999999
int graph[nmax][nmax];
int carnum[nmax];
int dest;
int capacity;
int min_dst = mint; //最短路径
int edgenum;
int vnum;
int mark[nmax];
vector<int> tmpvec;
int needSend = 0;
int needBack = 0;
int half_max;
typedef struct path
{
vector<int> vec; //存储路径
int needSend;
int needBack;
int minPath;
}path;
path tmp_path;
vector<path> pathArray;
void printPath(const vector<int>& vec)
{
cout << "0";
for (const auto& p : vec)
{
cout << "->" << p;
}
cout << " ";
}
void DFS(int src, int dst, int lastBike)
{
if (dst > min_dst) { return; }
//如果遍历到了目标结点
if (src == dest)
{
if (dst <= min_dst)
{
//如果该节点已经饱和
if (lastBike + carnum[dest] >= half_max)
{
needBack = lastBike + carnum[dest] - half_max;
}
//否则补充
else
{
needSend += half_max - (lastBike + carnum[dest]);
}
tmp_path.vec = tmpvec;
tmp_path.needSend = needSend;
tmp_path.needBack = needBack;
tmp_path.minPath = dst;
pathArray.push_back(tmp_path);
needBack = 0;
if(lastBike + carnum[dest] < half_max)
needSend -= half_max - (lastBike + carnum[dest]);
min_dst = dst;
}
return;
}
for (int i = 0; i < vnum; ++i)
{
if (graph[src][i] != mint && mark[i] == 0)
{
mark[i] = 1;
tmpvec.push_back(i);
int nextbike = 0;
if (src != 0)
{
//如果该节点已经饱和
if (lastBike + carnum[src] >= half_max)
{
nextbike = lastBike + carnum[src] - half_max;
}
//否则补充
else
{
needSend += half_max - (lastBike + carnum[src]);
}
}
DFS(i, dst + graph[src][i],nextbike);
if (src != 0)
{
if (half_max > lastBike + carnum[src])
needSend -= half_max - (lastBike + carnum[src]);
}
else
{
needSend = 0;
}
tmpvec.pop_back();
mark[i] = 0;
}
}
}
bool cmp(const path& a, const path& b)
{
if (a.minPath != b.minPath)
return a.minPath < b.minPath;
else
{
if (a.needSend != b.needSend)
return a.needSend < b.needSend;
else
return a.needBack < b.needBack;
}
}
int main()
{
cin >> capacity >> vnum >> dest >> edgenum;
vnum += 1;
half_max = capacity / 2;
for (int i = 0; i < vnum; ++i)
{
for (int j = 0; j < vnum; ++j)
{
graph[i][j] = mint;
}
}
for (int i = 1; i < vnum; ++i)
{
cin >> carnum[i];
}
for (int i = 0; i < edgenum; ++i)
{
int u, v;
cin >> u >> v;
cin >> graph[u][v];
graph[v][u] = graph[u][v];
}
fill(mark, mark + vnum, 0);
mark[0] = 1;
DFS(0,0,0);
sort(pathArray.begin(), pathArray.end(), cmp);
cout << pathArray[0].needSend << " ";
printPath(pathArray[0].vec);
cout << pathArray[0].needBack;
}
上一篇: C++ 1019 数字黑洞(20 分)
下一篇: 牛客练习赛68——A 牛牛的mex