PAT甲级1018 Public Bike Management//Dijkstra+DFS

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.

翻译:杭州市有公共自行车服务,为来自世界各地的游客提供了极大的便利。你可以在任何一个车站租一辆自行车,然后把它归还给城市的任何其他车站。公共自行车管理中心(PBMC)持续监控所有车站的实时通行能力。如果一个车站刚好是半满的,那么就说它的状况是完美的。如果一个车站是满的或空的,PBMC将回收或发放自行车来调整该车站的状况,使之达到完美。而且,沿途所有的车站也将进行调整。
当报告问题站点时,PBMC将始终选择到达该站点的最短路径。如果有一条以上的最短路径,将选择从PBMC发送的自行车数量最少的路径

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

翻译:上图是一个例子。站点由顶点表示,道路与边相对应。边上的数字是从另一个终点站到达终点站所需的时间。写在顶点S内的数字是当前存储在S的自行车数量。假设每个站点的最大容量为10。为了解决S3的问题,我们有两种不同的最短路径:

  1. PBMC->S1->S3。在这种情况下,必须从PBMC发送4辆自行车,因为我们可以从S1收集1辆自行车,然后再拿5辆自行车到S3,这样两个站点都将处于完美状态。
  2. PBMC->S2->S3。此路径与路径1时间相同,但只有3辆自行车从PBMC发送,因此选择这条。

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; 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 T​ij which describe the time T​ij taken to move betwen stations Si​​ and Sj​​ . All the numbers in a line are separated by a space.

翻译:每个输入文件包含一个测试用例。对于每种情况,第一行包含4个数字:Cmax(≤100),始终为偶数,为每个车站的最大通行能力;N(≤500)为车站总数;Sp,为有问题的车站的索引(车站编号从1到N,PBMC用顶点0表示);M为道路数量。第二行包含N个非负数Ci(i=1,⋯,N),其中每个Ci分别是Si处的当前自行车数。接下来是M行,每行包含3个数字:Si、Sj和Tij,它们描述在Si和Sj之间行走的时间。一行中的所有数字都用空格隔开。

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−>S1​−>⋯−>S​p. 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.

翻译:对于每个测试用例,将结果打印在一行中。首先输出PBMC必须发送的自行车数量。然后,在一个空格后,以以下格式输出路径:0−>S1–>⋯->Sp。最后在另一个空格后,输出在Sp调整到完美后我们必须带回到PBMC的自行车数量。注意如果这样的路径不是唯一的,那就输出要我们送回到PBMC数量最少的自行车的那条路。检测数据保证了这种路径是唯一的。

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

思路

本题还是用Dijkstra+DFS的方法求解,不过个人认为难点在于分配自行车,一定要严格按照题目要求分配,不能出现题目没说的“非法操作”,比如把后面结点多出来的车送回给前面等等

#include <iostream>
#include <climits>
#include <cstdlib>
#include <map>
#include <vector>
using namespace std;
int G[502][502], d[502], weight[502];
map<int, bool> visit;
vector<int> pre[502];
vector<int> temp_path, path;
int c, n, S, m;
int send = INT_MAX, back = INT_MAX;
void dfs(int);
void Dijkstra(int);
int main()
{
	fill(G[0], G[0] + 502 * 502, INT_MAX);
	cin >> c >> n >> S >> m;
	for (int i = 1; i <= n; i++)
		cin >> weight[i];
	for (int i = 0; i < m; i++)
	{
		int s1, s2, t;
		cin >> s1 >> s2 >> t;
		G[s1][s2] = t;
		G[s2][s1] = t;
	}
	Dijkstra(0);
	dfs(S);
	if (send == INT_MAX)
		send = 0;
	if (back == INT_MAX)
		back = 0;
	cout << send << ' ';
	for (int i = path.size() - 1; i >= 0; i--)
	{
		cout << path[i];
		if (i != 0)
			cout << "->";
	}
	cout << ' ' << back << endl;
	system("pause");
	return 0;
}
void Dijkstra(int s)
{
	fill(d, d + 502, INT_MAX);
	d[s] = 0;
	for (int i = 0; i < n; i++)
	{
		int u = -1, MIN = INT_MAX;
		for (int j = 0; j <= n; j++)
		{
			if (visit[j] == false && d[j] < MIN)
			{
				u = j;
				MIN = d[j];
			}
		}
		if (u == -1)
			return;
		visit[u] = true;
		for (int v = 0; v <= n; v++)
		{
			if (visit[v] == false && G[u][v] != INT_MAX)
			{
				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);
			}
		}
	}
}
void dfs(int t)
{
	if (t == 0)
	{
		temp_path.push_back(t);
		int st = 0, distribute = 0, size = c / 2;
		for (int i = temp_path.size() - 2; i >= 0; i--)
		{
			if (weight[temp_path[i]] > size) //如果自行车大于半满
				distribute += (weight[temp_path[i]] - size); //distribute获得多出来的自行车
			else if (weight[temp_path[i]] < size) //如果不到半满
			{
				if (distribute > size - weight[temp_path[i]]) //如果distribute足够用来分配
					distribute -= (size - weight[temp_path[i]]);
				else if (distribute < size - weight[temp_path[i]]) //不够分配就需要PBMC来发放了
				{
					st += (size - weight[temp_path[i]] - distribute); //把能分配的都用掉,剩下的PBMC来补
					distribute = 0;
				}
			}
		}
		if (st < send) //如果这条路要PBMC发的车更少
		{
			send = st;
			back = distribute;
			path = temp_path;
		}else if (st == send) //在需要PBMC发的车辆数相同的情况下
		{
			if (distribute < back) //如果返回给PBMC的车更少
			{
				back = distribute;
				path = temp_path;
			}
		}
		temp_path.pop_back();
		return;
	}
	temp_path.push_back(t);
	for (int i = 0; i < pre[t].size(); i++)
		dfs(pre[t][i]);
	temp_path.pop_back();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值