PAT 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 inperfectcondition 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 vertexSis the current number of bikes stored atS. Given that the maximum capacity of each station is 10. To solve the problem atS?3??, we have 2 different shortest paths:
-
PBMC ->S?1??->S?3??. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike fromS?1??and then take 5 bikes toS?3??, so that both stations will be in perfect conditions.
-
PBMC ->S?2??->S?3??. 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:C?max??(≤100), always an even number, is the maximum capacity of each station;N(≤500), the total number of stations;S?p??, the index of the problem station (the stations are numbered from 1 toN, and PBMC is represented by the vertex 0); andM, the number of roads. The second line containsNnon-negative numbersC?i??(i=1,?,N) where eachC?i??is the current number of bikes atS?i??respectively. ThenMlines follow, each contains 3 numbers:S?i??,S?j??, andT?ij??which describe the timeT?ij??taken to move betwen stationsS?i??andS?j??. 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?>S?1???>??>S?p??. Finally after another space, output the number of bikes that we must take back to PBMC after the condition ofS?p??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
思路
- 本題題意:將自行車從
PBMC帶到sp,使這條路徑最短,并使路徑上的每個車站的自行車數達到perfect即自行車數為車站容量的一半,如果有多條最短路徑,則選擇從PBMC所帶出自行車數最少的路徑,如果這條路徑還相同,則選擇的從SP帶回到PBMC的自行車最少數量, - 步驟:先使用
Dijkstra演算法選擇出最短路徑,然后利用DFS演算法選擇出need最小的路徑, - 注意:剛開始就要把每個車站自行車數量的一半減掉,方便之后計算,如果放到
DFS內計算,可能會減掉多次,從而出錯,
代碼
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 510;
const int INF = 0x3f3f3f3f;
int G[maxn][maxn];
int d[maxn];
bool vis[maxn] = {false};
int w[maxn];
vector<int> pre[maxn]; //存放最短路徑
int c, n, sp, m;
void Dijkstra(int s)
{
fill(d, d + maxn, INF);
d[s] = 0;
for (int i = 0; i <= n; i ++)
{
int u = -1, MIN = INF;
for (int j = 0; j <= n; j ++)
{
if (vis[j] == false && d[j] < MIN)
{
u = j;
MIN = d[j];
}
}
if ( u == -1)
{
return;
}
vis[u] = true;
for (int v = 0; v <= n; v ++)
{
if (vis[v] == false && G[u][v] != INF)
{
if (d[u] + G[u][v] < d[v])
{
d[v] = d[u]+ G[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if (d[u] + G[u][v] == d[v])
{
pre[v].push_back(u);
}
}
}
}
}
int minNeed = INF; //最少帶去
int minRemain = INF; //最少帶回
vector<int> path, tempPath;
int st = 0;
void dfs(int v)
{
if (v == st)
{
tempPath.push_back(v);
int need = 0, remain = 0;
for (int i = tempPath.size() - 1; i >= 0; i --)
{
int id = tempPath[i];
int weight= w[id];
if (weight > 0) //需要帶走的自行車
{
remain += weight;
}
else
{
if (remain > abs(weight)) // 當前自行車數大于所需自行車
{
remain -= abs(weight);
}
else
{
need += abs(weight) - remain; //需要帶到目的地的自行車
remain = 0; //需要帶回的自行車置零
}
}
}
if (need < minNeed)
{
minNeed = need;
minRemain = remain;
path = tempPath;
}
else if (need == minNeed && remain < minRemain)
{
minRemain = remain;
path = tempPath;
}
tempPath.pop_back();
return;
}
tempPath.push_back(v);
for (int i = 0; i < pre[v].size(); i ++)
{
dfs(pre[v][i]);
}
tempPath.pop_back();
}
int main()
{
//freopen("test.txt","r",stdin);
scanf("%d %d %d %d", &c, &n, &sp, &m);
fill(G[0], G[0] + maxn * maxn, INF);
w[0] = 0;
for (int i = 1; i <= n; i ++)
{
scanf("%d",&w[i]);
w[i] -= c / 2; //首先點權的容量減去一半
}
int id1, id2, dis;
for (int i = 0; i < m; i ++)
{
scanf("%d %d %d",&id1, &id2, &dis);
G[id1][id2] = G[id2][id1] = dis;
}
Dijkstra(0);
dfs(sp);
printf("%d ", minNeed);
for (int i = path.size() - 1; i >= 0; i --)
{
printf("%d", path[i]);
if (i > 0) printf("->");
}
printf(" %d", minRemain);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/67626.html
標籤:其他
