PTA_PAT甲級_1018 Public Bike Management (30分)

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

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

題意:
每個車站有一定容量,完美狀態是半滿,從起點到問題站點經過的站點都必須調整到完美狀態,求距離最短且需要攜帶和帶回的自行車數最少的路徑

分析:
使用Dijkstra+DFS,不能只用Dijkstra,因爲minNeed和minRemain在路徑上的傳遞不滿足最優子結構(不是簡單的相加過程)

代碼:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

#define MAXV 510
#define INF 1e9
int N, M, Cmax, Sp, numPath=0, G[MAXV][MAXV], weight[MAXV];
int d[MAXV], minNeed=INF, minRemain=INF, vis[MAXV]={0};
vector<int> pre[MAXV], tmpPath, path;//前驅,臨時路徑,最優路徑

void Dijkstra(int s);
void DFS(int v);

int main()
{
	cin>>Cmax>>N>>Sp>>M;
	fill(G[0], G[0]+MAXV*MAXV, INF);
	for(int i=1;i<=N;i++){
		cin>>weight[i];
		weight[i] -= Cmax/2;//減去最大值的一半即可根據正負判斷是否需要補充或帶走
	}
	for(int i=0;i<M;i++){
		int u,v;
		cin>>u>>v;
		cin>>G[u][v];//必須和上一步分開寫
		G[v][u] = G[u][v];
	}	
	Dijkstra(0);
	DFS(Sp);
	cout<<minNeed<<" ";
	for(int i=path.size()-1;i>=0;i--){
		cout<<path[i];
		if(i>0) cout<<"->";
	}
	cout<<" "<<minRemain;
	return 0;
} 

void Dijkstra(int s){
	fill(d, d+MAXV, INF);
	d[s] = 0;
	for(int i=0;i<=N;i++){//循環N+1次 
		int u=-1,MIN=INF;//u使d[u]最小,MIN存放該最小的d[u]
		for(int j=0;j<=N;j++){//找到未訪問的頂點中d[]最小的
			if(vis[j]==0 && d[j]<MIN){
				u = j;
				MIN = d[j];
			}
		}
		if(u==-1) return;//說明剩下的頂點和s不連通
		vis[u] = 1;
		for(int v=0;v<=N;v++){
			if(vis[v]==0 && G[u][v]!=INF){//對u的每個未訪問的鄰接點
				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 v){
	if(v==0){//遞歸邊界,葉子節點
		tmpPath.push_back(v);
		int need=0, remain=0;
		for(int i=tmpPath.size()-1;i>=0;i--){//必須倒着枚舉//這樣才能得到在起點的數量
			int id = tmpPath[i];
			if(weight[id]>0) remain += weight[id];//點權大於0說明需要帶走
			else{//否則需要補給
				if(remain>abs(weight[id])) remain -= abs(weight[id]);//當前持有量足夠補給
				else{//否則需要從PBMC攜帶
					need += abs(weight[id]) - remain;
					remain = 0;
				}
			}
		}
		if(need<minNeed){//需要從PBMC攜帶的數量更少則更新
			minNeed = need;
			minRemain = remain;
			path = tmpPath;
		}else if(need==minNeed && remain<minRemain){//攜帶數目相同但帶回數目變少也更新
			minRemain = remain;
			path = tmpPath;
		}
		tmpPath.pop_back();
		return;
	}
	tmpPath.push_back(v);
	for(int i=0;i<pre[v].size();i++)
		DFS(pre[v][i]);
	tmpPath.pop_back();
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章