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

解析:dijkstra+dfs具体解析看代码

求最短路径的问题,如果最短路径不唯一,选择使得需要送出的自行车数最少的路径,如果还是不唯一,则选择使得送回自行车最少的路径。

1.以二维矩阵储存两点之间的距离,如果两点之间不连通,则两点之间距离为INF;

2.用dijkstra算法算出起点(0)到目的地的最短距离;

3.用深度优先搜索找到全部最短路径,并在搜索过程中更新到当前的点需要送出的自行车数和经过该点之后能送到下一个点的自行车数。直到目的地,得到总的送出数和送回数,进行比较,最后得到最优的选择。
 

#include<bits/stdc++.h>
using namespace std;

#define e exp(1)
#define pi acos(-1)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define mem(a,b) memset(a,b,sizeof(a))
int gcd(int a,int b){return b?gcd(b,a%b):a;}

int c_max,n,target,m,perfect;
int min_cost;
int min_send=inf;
int min_takeback=inf;

vector<int> bikes_num;
vector<vector<int> >road;
vector<int> res;

int dijkstra()
{
	vector<int> cost(n,inf);
	vector<bool> vis(n,false);
	for(int i=1; i<n; i++)
	{
		cost[i]=road[0][i];
	}
	while(true)
	{
		int Min=inf,index;
		for(int i=1; i<n; i++)
		{
			if(!vis[i]&&Min>cost[i])
			{
				Min=cost[i];
				index=i;
			}
		}
		if(index==target)return cost[index];
		vis[index]=true;
		for(int i=1; i<n; i++)
		{
			if(!vis[i]&&road[i][index]<inf&&cost[i]>cost[index]+road[i][index])
			{
				cost[i]=cost[index]+road[i][index];
			}
		}
	}
}
void dfs(int cur,int cost,int take,int send,vector<int>path,vector<bool>vis)
{
	//take是所能带给下一个节点的自行车数,send是要送出去的自行车数 
	//搜索过程中更新到当前的点需要送出的自行车数和经过该点之后能送到下一个点的自行车数
	if(cur==0)send=take=0;
	else if(bikes_num[cur]+take-perfect>0)//说明上一个节点带下来的自行车数够填补该节点缺的车数。 
	{
		take=bikes_num[cur]+take-perfect;//更新 
	}
	else 
	{
		send=send+perfect-(bikes_num[cur]+take);//更新所需的车数 
		take=0;
	}
	if(cur==target)
	{
		if(cost==min_cost)
		{
			if(send<min_send)
			{
				min_send=send;
				min_takeback=take;
				res=path;
			}
			else if(send==min_send&&take<min_takeback)
			{
				min_takeback=take;
				res=path;
			}
		}
		return ;
	}
	for(int i=1; i<n; i++)
	{
		if(!vis[i]&&road[cur][i]<inf&&cost+road[cur][i]<=min_cost)
		{
			path.push_back(i);
			vis[i]=true;
			dfs(i,cost+road[cur][i],take,send,path,vis);
			path.pop_back();
			vis[i]=false;
		}
	}
}
int main()
{
	cin>>c_max>>n>>target>>m;
	perfect=c_max/2;
	n+=1;
	bikes_num.resize(n);
	for(int i=1; i<n; i++)
	{
		cin>>bikes_num[i];
	}
	road.resize(n,vector<int>(n,inf));
	int x,y,l;
	for(int i=0; i<m; i++)
	{
		cin>>x>>y>>l;
		road[x][y]=road[y][x]=l;
	}
	min_cost=dijkstra();
	
	vector<int> path;
	vector<bool> vis(n,false);
	dfs(0,0,0,0,path,vis);
	
	cout<<min_send<<" "<<0;
	for(int i=0; i<res.size(); i++)cout<<"->"<<res[i];
	cout<<" "<<min_takeback<<endl;
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值