1018 Public Bike Management

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
這道題好難!!!!理解題目意思都花了好久
題意:
城市裏的一些公共自行車站,每個車站的自行車最大容量爲一個偶數Cmax,且如果一個車站中自行車的數量恰好爲Cmax/2,則這個車站處於“完美狀態”。而如果一個車站容量是滿的或是空的。則會從控制中心(PBMC)攜帶或從路上收集一定數量的自行車前往該車站。使每個問題車站及沿途所有車站都達到“完美狀態”。現在給出Cmax,車站數目N(不含控制中心PBNC),問題車站編號Sp,無向邊數M及邊權,求一條從PBMC(記爲0號)到達問題車站Sp的最短路徑,輸出需要從PBMC攜帶的自行車數目,最短路徑,到達問題車站後需要帶回的自行車數目。如果最短路徑有多條,那麼選擇從PBMC攜帶的自行車數目最少的;如果仍然有多條,那麼選擇最後從問題車站帶回的自行車數目最少的。
!!!沿途所有車站的調整過程必須在前往問題車站的過程中調整完畢。

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
const int MAXV=510;//最大頂點數
const int INF=1000000;//無窮大

//n爲頂點數,m爲邊數,Cmax爲最大容量,Sp爲問題站點
//G爲鄰接矩陣,weight爲點權,d[]記錄最短距離
//minNeed記錄最少攜帶的數目,minRemain記錄最少帶回的數目
int n,m,Cmax,Sp,numPath=0,G[MAXV][MAXV],weight[MAXV];
int d[MAXV],minNeed=INF,minRemain=INF;
bool vis[MAXV]={false};
vector<int> pre[MAXV];//前驅
vector<int> tempPath,path;//臨時路徑及最優路徑
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]==false && d[j]<MIN)
			{
				u=j;
				MIN=d[j];
			} 
		} 
		//找不到小於INF的d[u],說明剩下的頂點和起點s不連通
	  if(u==-1)return;
	  vis[u]=true;//標記u爲已訪問
	  for(int v=0;v<=n;v++)
	  {
  		//如果v未訪問&& u能到達v
		  if(vis[v]==false && G[u][v]!=INF)
		  {
  			if(d[u]+G[u][v]<d[v])
  			{
			  	d[v]=d[u]+G[u][v];//優化d[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)
	{
		//遞歸邊界,葉子結點
		tempPath.push_back(v);
		//路徑tempPath上需要攜帶的數目,需要帶回的數目
		int need=0,remain=0;
		for(int i=tempPath.size()-1;i>=0;i--)
		{
			int id=tempPath[i];//當前結點編號爲id
			if(weight[id]>0)
			{
				//點權大於0,說明需要帶走一部分自行車
				remain+=weight[id];//當前自行車持有量增加weight[id] 
			}else{
				if(remain>abs(weight[id]))
				{
					//當前持有量足夠補給
					remain-=abs(weight[id]);//當前持有量減少補給的量
						 
				}else{
					need+=abs(weight[id])-remain;//不夠的部分從PBMC攜帶
					remain=0;//當前持有的自行車全部用來補給 
				} 
			}
		} 
		if(need<minNeed)
		{
			//需要從PBMC攜帶的自行車數目更少
			minNeed=need;//優化minNeed
			minRemain=remain;//覆蓋minRemain
			path=tempPath;//覆蓋最優路徑 
		}else if(need==minNeed && remain<minRemain)
		{
			//攜帶數目相同,帶回數目變少
			minRemain=remain;//優化minRemain
			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()
{
	scanf("%d%d%d%d",&Cmax,&n,&Sp,&m);
	int u,v;
	fill(G[0],G[0]+MAXV*MAXV,INF);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&weight[i]);
		weight[i]-=Cmax/2;//點權減去容量的一半 
	}
	for(int i=0;i<m;i++)
	{
		scanf("%d%d",&u,&v);
		scanf("%d",&G[u][v]);
		G[v][u]=G[u][v];
	} 
	Dijkstra(0);
	DFS(Sp);
	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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章