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.
在这里插入图片描述

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:

PBMC -> S​1​​ -> S​3​​ . In this case, 4 bikes must be sent from PBMC, because we can collect 1 bike from S​1​​ and then take 5 bikes to S​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 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 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−>S
​1​​ −>⋯−>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

解题思路:本题意思是从PBMC出发到目标位置的过程中,如果某点的自行车容量少于最大容量的一半,则补充到一半,若多于,则带走。若最短路径相同选择需要从PBMC带走自行车数少的,若仍然相同选择带回PBMC数量少的路径。本题一开始我以为只有目标点自行车数会少于完美情况其余都多余,所以我将所有点的自行车数加起来然后再判断是需要从PBMC拿出还是带回自行车。这种做法是错误的。每一点的情况都可能多于或少于最大值的一半,所以必须对每一点进行判断,将路径存储再temppath中记得一定要从最后一点进行处理(该位置才是起点)。本题Dijkstra函数用于存储所有最短路径,DFS函数选出题意中最优解,用need存储每一条最短路径中需要自行车数,用remain存储需要带回的自行车树。

代码:

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int INF = 1000000000;
const int MAXV = 1000;
struct node{
	int v;
	int data;
};
vector<node> Adj[MAXV];
int d[MAXV];
int weight[MAXV];
int vis[MAXV] = {0};
int C,N,S,M;
vector<int> pre[MAXV],temppath,path;
void Dijkstra(int s){
	fill(d,d+MAXV,INF);
	d[s] = 0;
	for(int i = 0;i < N + 1;i++){
		int u = -1,MIN = INF;
		for(int j = 0;j < N + 1;j++){
			if(vis[j] == 0 && d[j] < MIN){
				MIN = d[j];
				u = j;
			}
		}
		if(u == -1) return;
		vis[u] = 1;
		for(int j = 0;j < Adj[u].size();j++){
			node v = Adj[u][j];
			if(d[u] + v.data < d[v.v]){
				d[v.v] = d[u] + v.data;
				pre[v.v].clear();
				pre[v.v].push_back(u);
			}else if(d[u] + v.data == d[v.v]){
				pre[v.v].push_back(u);
			}
		}
	}
}

int minneed = INF;
int minremain = INF;
void DFS(int v){
	if(v == 0){
		int need = 0;
		int remain = 0;
		temppath.push_back(v);
		for(int i = temppath.size() - 1;i >= 0;i--){
			int id = temppath[i];
			if(weight[id] > 0){
				remain+=weight[id];
			}else{
				if(remain > -weight[id]){
					remain += weight[id];
				}else{
					need += -weight[id] - 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(void){
	scanf("%d %d %d %d",&C,&N,&S,&M);
	weight[0] = 0;
	for(int i = 1;i <= N;i++){
		int tempweight;
		scanf("%d",&tempweight);
		weight[i] = tempweight - C / 2;
	}
	for(int i = 0;i < M;i++){
		int a,b,temp;
		scanf("%d %d %d",&a,&b,&temp);
		node Node;
		Node.v = b;
		Node.data = temp;
		Adj[a].push_back(Node);
		Node.v = a;
		Node.data = temp;
		Adj[b].push_back(Node);
	}
	Dijkstra(0);
	DFS(S);
	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;
}
发布了69 篇原创文章 · 获赞 5 · 访问量 2038

猜你喜欢

转载自blog.csdn.net/lovingcgq/article/details/104695448