PAT 1018 Public Bike Management(30分)

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:

  1. 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.

  2. 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;
}

猜你喜欢

转载自www.cnblogs.com/adios/p/12602052.html