PAT 1018 Public Bike Management [dijkstra+DFS]

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

--------------------------------------这是题目和解题的分割线--------------------------------------

有两个容易错的地方。一个是0作为PBMC,所以读取要从下标1开始。还有一个是最短路径的选取,别忘了sent from单车数量相等的情况,此时应该再选取take back更小的路径。不然会错一半的测试点= =

#include<cstdio>
#include<vector>
#include<algorithm>
const int maxN = 540;
const int INF = 0x3fffffff;

using namespace std;

int visited[maxN] = {},dis[maxN] = {},bike[maxN];
int G[maxN][maxN];
int maxC,numV,proV,numE;

vector<int> minPath[maxN],tmpPath,bestPath;

//最短路径 
void dijkstra(int index)
{
	fill(dis,dis+maxN,INF);
	dis[index] = 0;
	int i,j;
	for(i=0;i<numV;i++)
	{
        //minV必须有初始值,下标从1开始读,第一次的循环中找不到满足dis[j]<minE要求的点
		int minV = index,minE = INF;
		for(j=1;j<=numV;j++) //下标1开始 
		{
			//离起点距离最小的点 
			if(!visited[j]&&dis[j]<minE)
			{
				minV = j;
				minE = dis[j];
			}
		}
		visited[minV] = 1; //访问标记 
		for(j=1;j<=numV;j++)
		{
			if(!visited[j]&&G[minV][j]!=INF)
			{
				//更短路径 
				if(dis[minV]+G[minV][j]<dis[j])
				{
					dis[j] = dis[minV]+G[minV][j];
					minPath[j].clear(); //先清空 
					minPath[j].push_back(minV); //再加入j的前驱结点minV 
				}
				//多条路径 
				else if(dis[minV]+G[minV][j]==dis[j])
					minPath[j].push_back(minV);	//j的前驱结点+1				
			}
		}
	}
}

int minSent = INF,minBack = INF;

//最佳路径 
void findBestPath(int index)
{
	int i,j;
	if(index==0) //递归边界 
	{
		tmpPath.push_back(index); 
		int bikeBack = 0,bikeSent = 0;	
		for(i=tmpPath.size()-1;i>=0;i--)
		{
			//如果(减去完美数量后)点权大于零,说明该点需要送回 
			if(bike[tmpPath[i]]>0)
				bikeBack += bike[tmpPath[i]];
			//否则,需要补给 
			else
			{
				//如果之前需要送回的车可以满足该点的空缺 
				if(bikeBack>-bike[tmpPath[i]])
					bikeBack += bike[tmpPath[i]]; //负数,bikeBack可以直接+ 
				//否则先将需要送回的车全部用来弥补空缺,再记录需要补给的数量 
				else
				{
					bikeSent += -(bike[tmpPath[i]]+bikeBack); //负号用来将补给数量变为正数,方便后面比较 
					bikeBack = 0; //记得清空 
				}		
			}		
		}
		//挑选补给数量更小的路径 
		if(bikeSent<minSent)
		{
			minSent = bikeSent;
			minBack = bikeBack; 
			bestPath = tmpPath;
		}
		//如果相等,选取送回车辆更小的路径 
		else if(bikeSent==minSent&&bikeBack<minBack)
		{
			minBack = bikeBack;
			bestPath = tmpPath;
		}
		tmpPath.pop_back();  
		return;
	}
	tmpPath.push_back(index); //加入该点 
	for(i=0;i<minPath[index].size();i++)
		findBestPath(minPath[index][i]); //遍历该点的前驱结点 
	tmpPath.pop_back(); //删除该点,恢复原状 
}

int main()
{
	int i;
	scanf("%d%d%d%d",&maxC,&numV,&proV,&numE);
	fill(G[0],G[0]+maxN*maxN,INF);
	for(i=1;i<=numV;i++) //下标1读取 
	{
		scanf("%d",&bike[i]);
		//单车数量减掉完美数量,这样方便从正负看出是需要补给还是送回 
		bike[i] -= maxC/2; 
	}	
	for(i=0;i<numE;i++)
	{
		int x,y,z;
		scanf("%d%d%d",&x,&y,&z);
		G[x][y] = G[y][x] = z;
	}
	dijkstra(0); //最短路径 
	findBestPath(proV); //最佳路径 
	printf("%d ",minSent); //最小补给车辆 
	for(i=bestPath.size()-1;i>=0;i--) //输出路径 
	{ 
		printf("%d",bestPath[i]);
		if(i!=0) printf("->");
	}
	printf(" %d\n",minBack); //最小送回车辆 
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值