PAT_A1018#Public Bike Management
Source:
Description:
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 (≤), always an even number, is the maximum capacity of each station; N (≤), 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 (,) where each Ci is the current number of bikes at Si respectively. Then Mlines 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. Finally after another space, output the number of bikes that we must take back to PBMC after the condition of Spis 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
Keys:
Code:
/*
Data: 2019-04-20 19:10:26
Problem: PAT_A1018#Public Bike Management
AC: 34:36 题目大意:
站点最佳状态时,有一半的自行车;
从起点选择最短路径终点,路径上的其他站点同样调整至最佳状态(补充/回收);
多条最短路径时,选择需要携带且回收数量最少的最条路径
输入:
第一行给出,最大容量Cmax,结点数N,Sp终点(默认起点为0),路径数M
第二行给出,各站点现有库存Ci
输出;
携带车辆数,路径,回收车辆数
*/ #include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
const int M=,INF=1e9;
int grap[M][M],vis[M],d[M],c[M];
int n,m,Cmax,st=,dt,optSent=INF,optBring=INF;
vector<int> temp,opt,pre[M]; void Dijskra(int s)
{
fill(vis,vis+M,);
fill(d,d+M,INF);
d[s]=;
for(int i=; i<=n; i++)
{
int u=-,Min=INF;
for(int j=; j<=n; j++)
{
if(vis[j]== && d[j]<Min)
{
u=j;
Min=d[j];
}
}
if(u==-) return;
vis[u]=;
for(int v=; v<=n; v++)
{
if(vis[v]== && grap[u][v]!=INF)
{
if(d[u]+grap[u][v] < d[v])
{
d[v]=d[u]+grap[u][v];
pre[v].clear();
pre[v].push_back(u);
}
else if(d[u]+grap[u][v]==d[v])
pre[v].push_back(u);
}
}
}
} void DFS(int v)
{
if(v == st)
{
int sent=,bring=;
for(int i=temp.size()-; i>=; i--)
{
int v = temp[i];
if(bring+(c[v]-Cmax/) > )
bring = bring + (c[v]-Cmax/);
else
{
sent += (Cmax/-bring-c[v]);
bring=;
}
}
if(sent < optSent)
{
optSent = sent;
optBring = bring;
opt = temp;
}
else if(sent==optSent && bring<optBring)
{
optBring = bring;
opt = temp;
}
return;
} temp.push_back(v);
for(int i=; i<pre[v].size(); i++)
DFS(pre[v][i]);
temp.pop_back();
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif // ONLINE_JUDGE fill(grap[],grap[]+M*M,INF);
scanf("%d%d%d%d", &Cmax,&n,&dt,&m);
for(int i=; i<=n; i++)
scanf("%d", &c[i]);
for(int i=; i<m; i++)
{
int v1,v2;
scanf("%d%d",&v1,&v2);
scanf("%d", &grap[v1][v2]);
grap[v2][v1]=grap[v1][v2];
}
Dijskra(st);
DFS(dt);
printf("%d %d", optSent,st);
for(int i=opt.size()-; i>=; i--)
printf("->%d", opt[i]);
printf(" %d", optBring); return ;
}
PAT_A1018#Public Bike Management的更多相关文章
- 1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- PAT 1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- A1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 1018 Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PTA (Advanced Level) 1018 Public Bike Management
Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
随机推荐
- zoj 1880 - Tug of War
题目:有n个人分成两组,两组人数差不能超过1,找到两组的人重量之差的最小值. 分析:dp,二维01背包. 由于必须放在两个组中的一组,直接背包全部可到状态, 取出相差不超过 1的最接近 sum/2的值 ...
- 2015 Multi-University Training Contest 2 1004 Delicious Apples(DP)
pid=5303">题目链接 题意:长度为l 的环,有n棵果树,背包容量为k,告诉你k棵苹果树的id.以及每棵树上结的果子数.背包一旦装满要返回起点(id==0) 清空,问你至少走多少 ...
- 读书笔记-APUE第三版-(7)进程环境
本章关注单进程执行环境:启动&终止.參数传递和内存布局等. 进程启动终止 如图所看到的: 启动:内核通过exec函数执行程序,在main函数执行之前.会调用启动例程(start-up rout ...
- LeetCode 557. Reverse Words in a String III (反转字符串中的单词 III)
Given a string, you need to reverse the order of characters in each word within a sentence while sti ...
- 如何的退出无响应的 SSH 连接
~. 具体操作是Shift+-,然后松开按.. tips如果无效,可以先按下Enter,然后进行上面的操作.
- 利用POI抽取word中的图片并保存在文件中
利用POI抽取word中的图片并保存在文件中 poi.apache.org/hwpf/quick-guide.html 1.抽取word doc中的图片 package parse; import j ...
- 关于OpenFileDialog的使用(转)
OpenFileDialog控件有以下基本属性 InitialDirectory 对话框的初始目录 Filter 要在对话框中显示的文件筛选器,例如,"文本文件(*.txt)|*.txt|所 ...
- 乐视云监控数据存放到influxdb中
3.9 监控.告警系统 监控报警我们分PaaS平台和业务应用两大类. PaaS平台主要聚焦在基础设施和LeEngine的各个服务组件的监控报警(比如主机CPU,内存,IO,磁盘空间,LeEng ...
- bzoj 3895 取石子 —— 博弈论
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3895 看了博客:https://blog.csdn.net/popoqqq/article/ ...
- 如何在vue项目中引入阿里巴巴的iconfont图库
1. 打开 http://www.iconfont.cn/ 2. 选择我们喜欢的图标,点击上面的小车,加入图标库,即右侧的购物车 3.点击购物车,点击下载代码 4.解压下载的文件夹,将文件夹复制到 a ...