您的位置:首页 > 其它

PAT(甲级)1018. Public Bike Management (30)

2018-02-09 21:59 567 查看
1018. Public Bike Management (30)


时间限制

400 ms

内存限制

65536 kB

代码长度限制

16000 B

判题程序

Standard

作者

CHEN, Yue

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:

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://github.com/Azure-Sky-L/PAT

杭州有一些共享单车,有 n 个停车点,每个停车点现在有 a[i] 辆车

有一个 PBMC(0点),现在需要从 PBMC 调车平衡目标车站的车辆数:即把目标车站的车辆数变为 c / 2 辆,多余的运回,不够的可以用刚刚回收的车进行补给,若仍不够,一开始需要从 PBMC 带上一定补给的车辆,最后仍多余的运回 PBMC

在满足以下三个条件的前提下,平衡从 PBMC 调车平衡车站 o:

1.距离最短,并沿途平衡 PBMC 和 o 之间所有车站的车辆

2.满足 1,需要从 PBMC 补给的车尽可能的少

3.满足 2,需要从PBMC 运回的车尽可能的少

首先 dj 建立到 PBMC 的最短路径的网络,并建立到每个车站最近的车站关系

从目标车站开始向 PBMC 搜索,并记录下沿途经过的车站

搜到 PBMC 后结束本次搜索,按照上面的条件,更新最优解

#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int MAX = 510;
const int INF = 0x3f3f3f3f / 2;
vector <int> v[MAX],vv,vp;
int d[MAX][MAX],a[MAX],dis[MAX],vis[MAX],ans,sum,c,n,o,m;
void dj(){
fill(vis,vis + MAX,0);
fill(dis,dis + MAX,INF);
dis[0] = 0;
while(true){
int u = -1;
for(int i = 0; i <= n; i++)
if(!vis[i] && (u == -1 || dis[u] > dis[i]))
u = i;
if(u == -1) break;
vis[u] = 1;
for(int i = 1; i <= n; i++)
if(dis[i] > dis[u] + d[u][i])
dis[i] = dis[u] + d[u][i],v[i].clear(),v[i].push_back(u); // 向前搜索,向 0 的方向
else if(dis[i] == dis[u] + d[u][i])
v[i].push_back(u);
}
return ;
}
void dfs(int x){
vp.push_back(x); // 沿途经过的城市
if(x == 0){
int nl = 0,pl = 0; // 最少需要派出的车辆和最少需要回收的车辆
for(int i = vp.size() - 2; i >= 0; i--){ // 不计算 PBMC
int w = vp[i];
pl += a[w] - c / 2; // c 保证为偶数,需要回收
if(pl < 0) nl -= pl,pl = 0; // 必需从 PBMC 发车
}
if(nl < sum) sum = nl,ans = pl,vv = vp;
if(nl == sum && pl < ans) ans = pl,vv = vp;
return ;
}
for(int i = 0; i < v[x].size(); i++)
dfs(v[x][i]),vp.pop_back(); // vp 清除最后一个元素
return ;
}
int main()
{
scanf("%d %d %d %d",&c,&n,&o,&m);
for(int i = 1; i <= n; i++)
scanf("%d",&a[i]);
for(int i = 0; i <= n; i++)
fill(d[i],d[i] + 1 + n,INF);
while(m--){
int x,y,z;
scanf("%d %d %d",&x,&y,&z);
d[x][y] = d[y][x] = z;
}
dj();
ans = sum = INF;
dfs(o);
printf("%d 0",sum); // 需要派出多少车辆
for(int i = vv.size() - 2; i >= 0; i--)
printf("->%d",vv[i]);
printf(" %d\n",ans); // 需要回收多少车辆
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息