PAT-1018-最短路径问题+DFS

1018 Public Bike Management
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.

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 S​3 , we have 2 different shortest paths:

  1. PBMC -> S1 -> S​3. In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S1 and then take 5 bikes to S​3, so that both stations will be in perfect conditions.
  2. PBMC -> S​2 -> 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: Cmax(≤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 C​i (i=1,⋯,N) where each C​i is the current number of bikes at Si respectively. Then M lines follow, each contains 3 numbers: S​i, S​j, and T​ij which describe the time T​ij 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−>⋯−>Sp. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Spis 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)不断监视所有站点的实时容量。 如果工作站刚满一半,则称其处于理想状态。 如果一个车站已满或空了,PBMC会收集或派送自行车以将该车站的状况调整到最佳状态。 而且,途中的所有车站也会进行调整。

当报告问题站点时,PBMC将始终选择到达该站点的最短路径。 如果最短路径多于一条,则将选择需要从PBMC发送的自行车数量最少的那条路径。

  1. PBMC-> S1-> S3。 在这种情况下,必须从PBMC发送4辆自行车,因为我们可以从S1处收集1辆自行车,然后将5辆自行车运送到S3,这样两个车站都将处于理想状态。
  2. PBMC-> S2-> S3。 该路径与路径1所需的时间相同,但是PBMC仅发送了3辆自行车,因此将被选择。

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

输出规格:
对于每个测试用例,将结果打印在一行中。首先输出PBMC必须发送的自行车数量。然后,在空格之后,以以下格式输出路径:0-> S1->⋯-> Sp。最后,在另一个空间之后,将Spis的条件调整为完美后,将必须退回的自行车数量输出到PBMC。

请注意,如果这样的路径不是唯一的,请输出一个需要最少数量的自行车的路径,我们必须将它带回PBMC。法官的数据保证了这样的路径是唯一的。

思路:
首先是求出最短路径,可能有多条则用pre来存取每个点的前驱。接下来需要将整个过程模拟一遍,找出最小需要的车数与返回时的车数,越小越好。此处使用dfs()遍历,将每条最短路径的情况遍历。
注意的是可以直接将各点已有的车数与最完美状态做差,就可以知道需要还是不需要带回还是将之前的车往里放。这里参考了柳神的思路,

#include<bits/stdc++.h>
using namespace std;

const int INF = 100000000;
const int MAX_N = 510;

int e[MAX_N][MAX_N]; // 记录边权 
int dis[MAX_N];		// 记录起点到各点的最短路径 
int weight[MAX_N];	//记录点权 
bool vis[MAX_N]; 	//记录各点是否访问过 
int minNeed = INF;
int minBack = INF;

int cmax, n, sp, m; //最大容量,总站数, 问题站点, 道路数 

vector<int> pre[MAX_N]; //指该点的前驱点  因为可能会存在多条路径 
vector<int> path, temppath;
 
void init() {
	fill(e[0], e[0] + MAX_N * MAX_N, INF);
	fill(dis, dis + MAX_N, INF);
	cin >> cmax >> n >> sp >> m;
	for(int i = 1; i <= n; i++) { //注意此处weight对应的从1开始 
		cin >> weight[i];
		//在这里就已经算好了是需要车还是调出车 
		weight[i] = weight[i] - cmax / 2; 
	} 
	int x, y, z;
	for(int i = 0; i < m; i++) {
		cin >> x >> y >> z;
		//无向边 
		e[x][y] = z;
		e[y][x] = z;
	} 
}

void dijkstra(int sp) {
	dis[0] = 0;
	//同样可以改成while(true) 反正遍历完会有break信号 
	for(int i = 0; i <= n; i++) { 
		int u = -1;
		int minn = INF;
		for(int j = 0; j <= n; j++) {
			if(vis[j] == false && dis[j] < minn) {
				u = j;
				minn = dis[j];
			}
		}
		
		if(u == -1) break;
		vis[u] = true;
			
		for(int v = 0; v <= n; v++) {
			if(vis[v] == false && e[u][v] != INF) {
				if(dis[u] + e[u][v] < dis[v]) {
					dis[v] = dis[u] + e[u][v];
					pre[v].clear();
					pre[v].push_back(u);
				} else if(dis[u] + e[u][v] == dis[v]) {
					pre[v].push_back(u);
				}
			}
		}
	}	
}

void dfs(int v) {
	temppath.push_back(v);
	if(v == 0) { //到管理中心的  开始排查前面的各个和是否满足最优要求 
		int need = 0;
		int back = 0;
		for(int i = temppath.size() - 1; i >= 0; i--) {
			int id = temppath[i]; // 存放的第i个点 
			//这里的weight是已经算好了是需要车还是不需要车 
			if(weight[id] > 0) { 
				back += weight[id];
			} else { 
				if(back > (0 - weight[id])) {
					//如果现有的带回的车够用 
					back += weight[id];	
				} else {
					//现有的带回的车都不够用  就需要往need里添加 
					need += ((0 - weight[id]) - back); 
					back = 0;  
				}
			}
		}
		if(need < minNeed) {
			minNeed = need;
			minBack = back;
			path = temppath;
		} else if(need == minNeed && back < minBack) {
			minBack = back;
			path = temppath;
		}
		temppath.pop_back(); //将管理的位置去除 
		return ;
	}
	for(int i = 0; i < pre[v].size(); i++) {
		dfs(pre[v][i]);
	} 
	temppath.pop_back();
}

int main() {
	init();
	dijkstra(sp);
	dfs(sp);
	cout << minNeed << " 0";
	for(int i = path.size() - 2; i >= 0; i--) {
		cout << "->" << path[i];
	}
	cout << " " << minBack;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值