1018 DFS求最短路径+记录路径+路径结点权值的比较 Public Bike Management (30分)

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

题目分析
station半满状态下是最好的
若满或者空,PBMC要调整station到半满
选最短的路,若一样长,选需要携带最少bikes的路

输入:
C 最大容量 <=100
N statoin数量 <=500
Sp 有问题的station的下标 1——N
M road数量

Ci 各个station的当前情况

M行
Si Sj Tij 从Si到Sj要花费的时间 Tij

输出
要携带的bikes数
输出路径
要带回的bikes数

题目分析
station半满状态下是最好的
若满或者空,PBMC要调整station到半满
选最短的路,若一样长,选需要携带最少bikes的路

输入:
C 最大容量 <=100
N statoin数量 <=500
Sp 有问题的station的下标 1——N
M road数量

Ci 各个station的当前情况

M行
Si Sj Tij 从Si到Sj要花费的时间 Tij

输出
要携带的bikes数
输出路径
要带回的bikes数

题目关键点
关键点1:从路径到目的地,一路上的station全部都要调整为半满状态;
关键点2:从路径到目的地,一遍就需要将所有station都调整为版满状态,不存在回头继续调整;
故此题需要得到最短路径后,计算出按该路径走要携带的bikes数量和要带回的bikes数量,再与最少的比较,得到最佳答案;

不存在最佳子问题;
故不能用贪心算法dijkstra中添加条件得到最佳答案;
需要DFS得到路径后,计算sendbikes数量和backbikes数量比较;
难点在于上述判断后运用正确算法,再者就是如何在得到路径后计算sendbikes和backbikes数量;

1.输入操作
将到达不同车站的时间记录在Graph中;
将不同车站当前含有的车辆数放在Cur中;

#include<iostream>
#include<algorithm> 
#include<vector>
#define inf 0x3fffffff
using namespace std;
 
int C,N,Sp,M;
int Cur[501]; 
int Graph[501][501];

void input()
{
   
	fill(Graph[0],Graph[0]+501*501,inf);
	cin>>C>>N>>Sp>>M;
	Cur[0]=0;
	for(int i=1;i<=N;i++)
	{
   
		cin>>Cur[i];
	}
	for(int i=0;i<M;i++)          
	{
   
		int a,b,time;
		cin>>a>>b>>time;
		Graph[a][b]=time;          
		Graph[b][a]=time;
	}
}

2.写一个DFS函数得到最佳路径,并比较

void DFS(int S,int Time)   //初始为S,0,0 
{
   
	if(MinTime<Time) return;    //不为最短路径,直接return

	if(S==Sp)		//到达终点
	{
         
		int send = 0, back = 0, bikeSum = 0; 
		for(int i=0;i<path.size();i++)
		{
   
			bikeSum+=Cur[path[i]];
			send=max(send,(C/2)*i-bikeSum);
		} //send已求出 
		back=send+bikeSum-(path.size()-1)*(C/2);
		if(Time<MinTime){
   
			MinSend=send;
			MinBring=back;
			Repath=path;
			MinTime=Time
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值