PAT 1018 Public Bike Management

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_36032963/article/details/84065553

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

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

解题思路:

注意 all the stations on the way will be adjusted as well. 判断优先级为:时间最短、send最少、back最少。

先找到起点到终点最短的路线,再从这些路线里找出send和back最少的路线 ,所以只用一次Dijkstra是不行的。因为send、back和时间不同,时间的局部最短可以得到全局最短,但send、back的局部最少不一定得到全局最少。

比如可以看下面这个测试用例:

10 4 4 5

4 8 9 0

0 1 1

1 2 1

1 3 2

2 3 1

3 4 1

#include <iostream>  
#include <algorithm>
#include <vector>
using namespace std;
#define INF 0x3f3f3f3f
#define MAX 502

int Data[MAX][MAX],dist[MAX],DIF[MAX],visit[MAX];
vector<int> path,tempath;
vector<int> per[MAX];

int Cm,N,Sp,minSend=INF,minBack=INF;
void DFS(int end) {
	tempath.push_back(end);
	if(end==0) {
		int send=0,back=0;
		for(int i=tempath.size()-2;i>=0;i--) {
			int id=tempath[i];
			if(DIF[id]>back) {
				send+=(DIF[id]-back);
				back=0;
			}
			else 
				back-=DIF[id];
		}
		if(send<minSend) {
			minSend=send;
			minBack=back;
			path=tempath;
		}
		else if(send==minSend&&back<minBack) {
			minBack=back;
			path=tempath;
		}		
		tempath.pop_back();
		return;
	}
	for(int i=0;i<per[end].size();i++) 
		DFS(per[end][i]);
	tempath.pop_back();
}
int main() {
	int i,E,start,end,time,bikenum;
	scanf("%d %d %d %d",&Cm,&N,&Sp,&E);
	Cm=Cm/2;
	for(i=1;i<=N;i++) {
		scanf("%d",&bikenum);
		DIF[i]=Cm-bikenum;
	}
	fill(Data[0],Data[0]+MAX*MAX,INF);
	fill(dist,dist+MAX,INF);
	fill(visit,visit+MAX,0);
	for(i=0;i<E;i++) {
		scanf("%d %d %d",&start,&end,&time);
		Data[start][end]=Data[end][start]=time;
	}
	dist[0]=0;
	while(true) {
		int index=0,minDist=INF;
		for(i=0;i<=N;i++) {
			if(!visit[i]&&dist[i]<minDist) {
				index=i;
				minDist=dist[i];
			}
		}
		if(minDist==INF) break;
		visit[index]=1;
		for(i=1;i<=N;i++) {
			if(!visit[i]&&Data[index][i]<INF) {
				if(dist[i]>dist[index]+Data[index][i]) {
					dist[i]=dist[index]+Data[index][i];
					per[i].clear();
					per[i].push_back(index);
				}
				else if(dist[i]==dist[index]+Data[index][i]) {
					per[i].push_back(index);
				}
			}
		}
	}
	DFS(Sp);
	printf("%d 0",minSend);
	for(i=path.size()-2;i>=0;i--)
		printf("->%d",path[i]);
	printf(" %d",minBack);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_36032963/article/details/84065553