1018 Public Bike Management (30 point(s)) PAT甲级

1018 Public Bike Management (30 point(s))

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.

img

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.

Input Specification:

Each input file contains one test case. For each case, the first line contains 4 numbers: Cma**x (≤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 to N, and PBMC is represented by the vertex 0); and M, the number of roads. The second line contains N non-negative numbers C**i (i=1,⋯,N) where each C**i is the current number of bikes at S**i respectively. Then M lines follow, each contains 3 numbers: S**i, S**j, and Tij which describe the time Tij taken to move betwen stations S**i and S**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−>S1−>⋯−>S**p. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of S**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

思路

题意

每个车站自行车数目为cmax/2的时候最完美,当车站自行车树归0或为cmax的时候需要调整。

给出cmax(偶数),车站数n(不包括出发点0),需要调整的车站sp,连接车站的路数m,求出到目标车站的最短路径。调整目标车站的时候会一并调整路过的车站自行车数到完美,当存在多个最短路径的时候选择从始发站带出车辆最少的路径。如果还有同样的答案选择从目标车站带回车辆最少的路径。

分析

因为不是求单一最短路径,还有其他判定条件,用DFS遍历每一条可能路径。shortest记录当前最短路径,当length>shortest的时候直接剪枝,节省时间。

用二维vector存储相邻结点,数组edge[i+j*500]存储i,j两点之间的路径时间,然后就可以DFS搜索了~

代码

#include <iostream>
#include <vector>
using namespace std;

int cmax, cmid, n, sp, m, a, b, tab, tbring = 0, tback = 0, bring = 9999, back = 9999;
int edge[300000] = { 0 }, bike[505] = { 0 };
int length = 0, shortest = 999999, tempnum = 0, num = 99999;
bool visit[505] = { false };
vector<vector<int>> v;
vector<int> temppath, ans;

void find(int i) {
	if (i == sp || length > shortest) {//剪枝
		if (length < shortest || (length == shortest && tbring < bring) || (length == shortest && tbring == bring && tback < back)) {
			ans = temppath;
			shortest = length;
			bring = tbring;
			back = tback;
		}
		return;
	}
	for (int j = 0; j < v[i].size(); ++j) {
		int k = v[i][j];
		if (!visit[k]) {
			visit[k] = true;
			int temp = tback;
			if (bike[k] < 0) {//bring
				if (tback >= 0 - bike[k])//tback can cover need
					tback += bike[k];
				else {
					tbring += (0 - bike[k] - temp);
					tback = 0;
				}
			}
			else if (bike[k] > 0) {//back
				tback += bike[k];
			}
			length += edge[i + k * 500];
			temppath.push_back(k);
			find(k);
			length -= edge[i + k * 500];
			temppath.pop_back();
			if (bike[k] < 0) {//bring
				if (temp >= 0 - bike[k])//tback can cover need 这里必须是temp!!!!!!
					tback -= bike[k];
				else {
					tback = temp;
					tbring -= (0 - bike[k] - temp);
				}
			}
			else if (bike[k] > 0) {//back
				tback -= bike[k];
			}
			visit[k] = false;
		}
	}
}

int main()
{
	scanf("%d%d%d%d", &cmax, &n, &sp, &m);
	cmid = cmax / 2;
	v.resize(n + 1);
	for (int i = 1; i <= n; ++i) {
		scanf("%d", &bike[i]);
		bike[i] -= cmid;
	}
	for (int i = 0; i < m; ++i) {
		scanf("%d%d%d", &a, &b, &tab);
		v[a].push_back(b);
		v[b].push_back(a);
		edge[a + b * 500] = edge[b + a * 500] = tab;
	}
	visit[0] = true;
	temppath.push_back(0);
	find(0);
	printf("%d ", bring);
	for (int i = 0; i < ans.size(); ++i)
		printf("%d%s", ans[i], i == ans.size() - 1 ? " " : "->");
	printf("%d", back);

	return 0;
}

注意

  • n是不包括起始点在内的节点数,所以bike[i]从1开始不是从0开始储存!!而且vector<vector>的大小为(n+1)
  • 判定dfs结束的条件时一定要弄清楚if()内的逻辑关系
  • 当dfs前后变量变化时需要有一个临时变量记录原变量的值才可以作为if的判断条件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值