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 S3, we have 2 different shortest paths:
-
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.
-
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.
Input Specification:
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.
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−>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.
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
参考来自:https://blog.csdn.net/CV_Jason/article/details/81385228
题目大意:杭州市的自行车站管理系统,每个站点都有一定数量的自行车,数量处于最大值一半的状态是“perfect condition”(有车可借,有空位可还车),如果数量是0,称之为“problem condition”,需要从调度中心去运送车辆。运送路程中,如果经过自行车站点,会顺带调整该站点的状态,多的车子带走,少的顺带补齐。已知各个自行车站点的通车时间,求问最短的行车路线,如果存在多个最短路线,那么需要补送自行车最少的路径,如果补送也相同,输出带回自行车最少的路径。
解题思路:Dijkstra求最短路径,dfs求minNeed和minBack和path,dfs的时候模拟一遍需要调整的过程,求出最后得到的need和back,与minNeed和minBack比较然后根据情况更新path
注意:除了proble condition顶点之外,其余顶点的自行车数量,有可能是多的,就是最后还要带回到0顶点一些自行车。
往回走的时候是不调整自行车的。
#include <stdio.h>
#include <string.h>
#include <string>
#include <vector>
#include <stack>
#include <algorithm>
#include <iostream>
#define INF 0x3f3f3f3f
using namespace std;
int ma[1005][1005];
int bike[1005];//结点的自行车数
int dis[1005];//起点到结点的距离
int sum[1005];//携带的自行车数
int vis[1005];
vector<int> pre[1005],path,temppath;
int minNeed=INF,minBack=INF;
void dfs(int v)
{
temppath.push_back(v);
if(v==0)
{
int need=0,back=0;
for(int i=temppath.size()-1;i>=0;i--)
{
int id=temppath[i];
if(bike[id]>0)
back+=bike[id];
else
{
if(back+bike[id]>0)
back+=bike[id];
else
{
need-=back+bike[id];
back=0;
}
}
}
if(need<minNeed)
{
minNeed=need;
minBack=back;
path=temppath;
}
else if(need==minNeed&&back<minBack)
{
minBack=back;
path=temppath;
}
temppath.pop_back();
return;
}
for(int i=0;i<pre[v].size();i++)
dfs(pre[v][i]);
temppath.pop_back();
}
int main()
{
int c,n,p,m,next,i,j;//容量c,结点个数n,问题节点p,路径条数m
memset(ma,INF,sizeof ma);
memset(dis,INF,sizeof dis);
memset(vis,0,sizeof vis);
scanf("%d%d%d%d",&c,&n,&p,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&bike[i]);
bike[i]=bike[i]-c/2;
}
for(i=0;i<m;i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
ma[x][y]=ma[y][x]=z;
}
dis[0]=0;
for(i=0;i<=n;i++)
{
int d=INF;
for(j=0;j<n;j++)
{
if(vis[j]==0&&dis[j]<d)
{
d=dis[j];
next=j;
}
}
vis[next]=1;
for(j=0;j<=n;j++)
{
//printf("%d %d %d\n",dis[j],dis[next],ma[next][j]);
if(dis[j]>dis[next]+ma[next][j])
{
dis[j]=dis[next]+ma[next][j];
pre[j].clear();
pre[j].push_back(next);
}
else if(dis[j]==dis[next]+ma[next][j])
{
pre[j].push_back(next);
}
}
}
dfs(p);
printf("%d 0", minNeed);
for(i=path.size()-2;i>=0;i--)
printf("->%d",path[i]);
printf(" %d\n",minBack);
}