题目描述
There is a public bike service in Hangzhou City which providesgreat convenience to the tourists from all over the world. One may rent a bike at any station and returnit to any other stations in the city.
The Public Bike Management Center (PBMC) keeps monitoring the real-timecapacity of all the stations. A stationis said to be in perfect condition if it is exactly half-full. If a station is full or empty, PBMC willcollect or send bikes to adjust the condition of that station to perfect. And more, all the stations on the way will beadjusted as well.
When a problem station is reported, PBMCwill always choose the shortest path to reach that station. If there are more than one shortest path, theone 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 theedges. The number on an edge is the timetaken to reach one end station from another. The number written inside a vertex S is the current number of bikesstored at S. Given that the maximumcapacity of each station is 10. To solvethe 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 1bike from S1 and then take 5 bikes to S3, so that both stations will be inperfect conditions.
2. PBMC -> S2 -> S3. This path requires the same time as path 1, but only 3 bikes sent fromPBMC and hence is the one that will be chosen.
输入描述:
Each input file contains one test case. For each case, the first line contains 4numbers: Cmax (<= 100), always an even number,is the maximum capacity of each station; N (<= 500), the total number ofstations; Sp, the index of the problem station(the stations are numbered from 1 to N, and PBMC is represented by the vertex0); and M, the number of roads. Thesecond 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 PBMCmust send. Then after one space, outputthe path in the format: 0->S1->...->Sp. Finally after anotherspace, output the number of bikes that we must take back to PBMC after thecondition of Sp is adjusted to perfect.
Note that if such a path is not unique, output the one that requires minimumnumber 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
这题使用了一种新的方法,一种我原来没有考虑到的方法,即在迪杰斯特拉算法的基础上加上保存所有最短路径的操作,然后配合DFS来找打符合要求的路径的算法,这种算是涨了知识了,
但是看答案发现貌似使用DFS也能通过,其实假如是比较难的题目的话,DFS是一定不行的,因为DFS是指数时间,而以上算法是多项式时间,时间差了好几个数量级,所以我以后在做题的时候还要考虑到考试的难度,毕竟是甲级,不可能出现这么难的算法,以后在做题的时候一定要考虑到,必要的时候还可以使用暴力算法,只要能够解决题目,管他运行时间呢,一定要注意,浙大PAT甲级也就是这样的难度!
通过这一题我明白了, 以后在做题的时候要记得,memset函数需要使用memory.h头文件才可以使用,要不然会报错的!
#include<iostream>
#include<vector>
#include<memory.h>
using namespacestd;
#define MAX200000000
int Cmax, N, Sp,M;
inttheBikes[503];
introads[503][503];
voiddfs(vector<int> &result,vector<int> &theStack, int t, int&theBestTakeNum,int &theBestTakeBackNum,vector<vector<int>> &fa)
{
if (t == 0)
{
int takeNum=0, takeBackNum=0;
for (int i = (int)theStack.size() -2; i >= 0; i--)
{
if (theBikes[theStack[i]]>= Cmax / 2)
{
takeBackNum +=theBikes[theStack[i]] - Cmax / 2;
}
else
{
takeBackNum -= Cmax / 2 -theBikes[theStack[i]];
if (takeBackNum < 0)
{
takeNum -=takeBackNum;
takeBackNum = 0;
}
}
}
if (takeNum < theBestTakeNum)
{
theBestTakeNum = takeNum;
theBestTakeBackNum =takeBackNum;
result = theStack;
return;
}
else if(takeNum == theBestTakeNum)
{
if (takeBackNum <theBestTakeBackNum)
{
theBestTakeNum = takeNum;
theBestTakeBackNum =takeBackNum;
result = theStack;
return;
}
}
return;
}
for (int i = 0; i < (int)fa[t].size();i++)
{
theStack.push_back(fa[t][i]);
dfs(result, theStack, fa[t][i],theBestTakeNum,theBestTakeBackNum, fa);
theStack.pop_back();
}
}
int main() {
int a, b, c;
cin >> Cmax >> N >>Sp>>M;
memset(roads, -1, sizeof(roads));
vector<bool> vst(N+1, false);
vector<int> preL(N + 1,MAX);
vector<vector<int>> fa(N + 1);
for (int i = 1; i <= N; i++)
cin >> theBikes[i];
for (int i = 0; i < M; i++)
{
cin >> a >> b >>c;
roads[a][b] = c;
roads[b][a] = c;
}
for (int i = 1; i <= N; i++)
{
if (roads[0][i] != -1)
{
preL[i] = roads[0][i];
fa[i].push_back(0);
}
}
int u, theMinL=MAX;//记录最小的编号和值
vst[0] = true;
preL[0] = 0;
fa[0].push_back(0);
while (1)
{
theMinL = MAX;
for (int i = 1; i <= N; i++)
{
if (!vst[i] && preL[i]< theMinL)
{
theMinL = preL[i];
u = i;
}
}
vst[u] = true;
if (u == Sp) break;
for (int i = 1; i <= N; i++)
{
if (!vst[i] &&roads[u][i] != -1 && preL[u] + roads[u][i] <= preL[i])
{
if (preL[u] + roads[u][i]< preL[i])
{
fa[i].clear();
fa[i].push_back(u);
preL[i] = preL[u] +roads[u][i];
}
else
{
fa[i].push_back(u);
}
}
}
}
vector<int> result, theStack; inttakeNum=0, takeBackNum=0;
int theBestTakeNum=MAX,theBestTakeBackNum;
theStack.push_back(Sp);
dfs(result, theStack, Sp,theBestTakeNum,theBestTakeBackNum, fa);
cout << theBestTakeNum<<"0";
for (int i = (int)result.size()-2; i>=0; i--)
cout << "->"<< result[i];
cout << " " <<theBestTakeBackNum;
return 0;
}