Public Bike Management (30)

文章讲述了杭州的公共自行车服务如何通过PBMC实时监控各站点状态,确保每站恰好一半满。遇到问题站时,通过Dijkstra算法找到最短路径并最小化从PBMC调度的自行车数量,以调整站点至理想状态。
摘要由CSDN通过智能技术生成
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.
 

 
 Figure 1
 Figure 1 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 S3 , we have 2 different shortest paths:
 1. 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.
 2. PBMC -> S2 -> 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.

输入描述:

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 Ci (i=1,...N) where each  Ci is the current number of bikes at Si respectively.  Then M lines follow, each contains 3 numbers: Si, Sj, and Tij which describe the time Tij taken to move betwen stations Si and Sj.  All the numbers in a line are separated by a space.


 

输出描述:

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

输入例子:

10 3 3 5
6 7 0
0 1 1
0 2 1
0 3 3
1 3 1
2 3 1

输出例子:

3 0->2->3 0

1、题意:

原文大意是指在一个无向图中,从起点0出发,到达终点,对经过路径上的自行车进行调整,最终使得路径上每个站点都达到只有一半的的自行车 ,达到完美

坑点

1、只能沿着路径进行调整,举个例子,如果一个站点缺了自行车,另一个在它后面的站点多了自行车,这是不能用后面的进行补充的,只能一个一个来。

2、在进行Djstra的时候,达到终点的路径需要最短路,当到达终点最短路很多条的时候,取从0号出发需要的送到路径上的站点最少的。每次进行一次判断

3、在处理前驱数组(也就是记录上一个节点的数组),要注意一些细节

2、思路:

堆优化的Dijkstra算法,再加细节处理,当到达终点时,计算一下这个点需要送出的以及需要送回的。

3、27分代码(求大佬指出错误):

// 用堆优化的dj
#include<iostream>
#include<algorithm>
#include<cstring>
#include<queue>
#define x first
#define y second
using namespace std; 
const int N = 550,M = N * N;
int h[N],ne[M],w[M],e[M],idx;
int pre[N],a[N],c[N],d[N];
int dist[N];
bool st[N];
typedef pair<int,int> PII;

int Cmax,n,sp,m;
void add(int a,int b,int c) // 正常链式向前星 
{
	e[idx] = b,ne[idx] = h[a],w[idx] = c,h[a] = idx++; 
}
void Dijkstra() // 堆优化的Dijkstra算法 
{
	memset(dist,0x3f,sizeof dist);
	memset(pre,-1,sizeof pre);
	dist[0] = 0;
	priority_queue<PII,vector<PII>,greater<PII>> heap;
	heap.push({0,0});
	while(heap.size())
	{
		auto u = heap.top();
		heap.pop();
		int ver = u.y,dis = u.x;
		if(st[ver]) continue;
		st[ver] = true;
		
		for(int i = h[ver];~i;i = ne[i])
		{
			int j = e[i];
			if(dist[j] > dis + w[i]) // 正常的更新最短路 
			{
				dist[j] = dis + w[i];
				pre[j] = ver;
				heap.push({dist[j],j}); 
			}
			// 只有到了终点才会进行处理 
			else if(dist[j] == dis + w[i])// 新的最短路处理,判断路径上自行车数量是不是与完美值接近 
			{// 与完美值接近的就是更优的。需要求出路径上的节点数量,路径上自行车数量总和
				int cnt1 = 1,cnt2 = 2; 
				c[0] = j,d[0] = j,d[1] = ver;
				for(int k = pre[j];k!=0;k = pre[k])
					c[cnt1++] = k;
				c[cnt1++] = 0;
				for(int k = pre[ver];k!=0;k=pre[k])
					d[cnt2++] = k;
				d[cnt2++] = 0;
				reverse(c,c+cnt1);
				reverse(d,d+cnt2);
				int res1c = 0,res2c =0,res1d = 0,res2d = 0;// 分别是要送的以及要还回去的
				for(int k = 1;k<cnt1;k++)
				{
					if(a[c[k]] + res2c < Cmax/2)
					{
						res1c += Cmax/2 - a[c[k]] - res2c;
						res2c = 0;
					}
					else if(a[c[k]] + res2c >= Cmax/2)
					{
						res2c = a[c[k]] + res2c - Cmax/2;
					}					
				}
				for(int k = 1;k<cnt2;k++)
				{
					if(a[d[k]] + res2d < Cmax/2)
					{
						res1d += Cmax/2 - a[d[k]] - res2d;
						res2d = 0;
					}
					else if(a[d[k]] + res2d >= Cmax/2)
					{
						res2d = a[d[k]] + res2d - Cmax/2;
					}					
				} 
				if(res1d < res1c) pre[j] = ver;
				else if(res1d==res1c&&res2d<res2c) pre[j] = ver; 
			}
		}
	}
}

int main()
{
	std::ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
	memset(h,-1,sizeof h);
	cin >> Cmax >> n >> sp >> m;
	for(int i = 1;i<=n;i++) cin>>a[i];
	
	for(int i = 1;i<=m;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		add(a,b,c),add(b,a,c);
	}
	Dijkstra();
	int cnt = 1;
	int s[N],k = 1;
	s[0] = sp; 
	for(int i = pre[sp];i!=0;i = pre[i])
	{
		cnt ++;
		s[k++] = i;
	}
	s[k++] = 0;

	int res1 = 0,res2 = 0;// 需要送的res1,以及需归还的 res2
	reverse(s,s+k); // 转换前驱数组,使其正向。res1和res2是随着站点动态变化的 
	for(int i = 1;i<k;i++)
	{
		if(a[s[i]] + res2 < Cmax/2)
		{
			res1 += Cmax/2 - a[s[i]] - res2;
			res2 = 0;
		}
		else if(a[s[i]] + res2 >= Cmax/2)
		{
			res2 = a[s[i]] + res2 - Cmax/2;
		}
	}
	printf("%d ",res1); 
	for(int i = 0;i<k-1;i++)
		printf("%d->",s[i]);
	printf("%d ",s[k-1]);
	printf("%d",res2);
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值