PAT 1018. Public Bike Management
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:
1. 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.
2. 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
// 1018pat2.cpp : 定义控制台应用程序的入口点。
// #include <iostream>
#include <map>
#include <vector>
#include <math.h>
using namespace std;
const int INF=0x7fffffff;
const int N=; struct Edge
{
int next;
int dis;
int cost;
}; struct Edges
{
vector<Edge> list;
}edges[N];
map<int,int> cmap;
int dist[N];
int cost[N];
int parent[N];
bool mark[N];
int cmax,n,sp,M; void print_path(int x)
{
if(x==)
cout<<x<<"->";
else
{
print_path(parent[x]);
if(x==sp)
cout<<x;
else
cout<<x<<"->";
}
} int main()
{
while(cin>>cmax>>n>>sp>>M)
{
for(int i=;i<=n;++i)
{
mark[i]=false;
dist[i]=INF;
cost[i]=;
parent[i]=-;
edges[i].list.clear();
}
cmap[]=;
int tmp;
for(int i=;i<=n;++i)
{
cin>>tmp;
cmap[i]=tmp;
}
Edge edge;
int a,b,dis;
for(int i=;i<=M;++i)
{
cin>>a>>b>>dis;
edge.dis=dis;
edge.cost=cmap[b];
edge.next=b;
edges[a].list.push_back(edge);
edge.cost=cmap[a];
edge.next=a;
edges[b].list.push_back(edge);
}
mark[]=true;
dist[]=;
cost[]=;
int newP=;
int next;
int costs;
int d;
for(int i=;i<=n;++i)
{
if(newP==)
newP=newP;
for(int j=;j<edges[newP].list.size();++j)
{
next=edges[newP].list[j].next;
if(mark[next])
continue;
costs=edges[newP].list[j].cost;
d=edges[newP].list[j].dis;
if(dist[next]==-||(dist[next]>dist[newP]+d)||(dist[next]==dist[newP]+d&&abs(cost[next])>abs(cost[newP]+costs-cmax/)))
{
dist[next]=dist[newP]+d;
cost[next]=cost[newP]+costs-cmax/;
parent[next]=newP;
}
}
int min=INF;
for(int i=;i<=n;++i)
{
if(mark[i]||dist[i]==-)
continue;
if(dist[i]<min)
{
min=dist[i];
newP=i;
}
}
mark[newP]=true;
}
int res=cost[sp];
if(res<)
{
cout<<abs(res)<<" ";
print_path(sp);
cout<<" "<<<<endl;
}
else
{
cout<<<<" ";
print_path(sp);
cout<<" "<<res<<endl;
}
}
return ;
}
PAT 1018. Public Bike Management的更多相关文章
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- PAT 甲级 1018 Public Bike Management (30 分)(dijstra+dfs,dfs记录路径,做了两天)
1018 Public Bike Management (30 分) There is a public bike service in Hangzhou City which provides ...
- PAT Advanced 1018 Public Bike Management (30) [Dijkstra算法 + DFS]
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- 1018. Public Bike Management (30)
时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...
- 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 ...
- PTA (Advanced Level) 1018 Public Bike Management
Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...
随机推荐
- 商务通简单弹窗样式 V1.0
代码为: document.writeln('<style>*{margin:0; padding:0;}</style>');//创建中间弹框 document.wri ...
- jquery插件----文件上传uploadfile
使用了一款jquery上传的插件,ajax上传,可以显示上传的进度,高可配性,简要记录. 插件地址http://hayageek.com/docs/jquery-upload-file.php git ...
- session阻塞机制,解决方法
session从生成到读取,或从生成到写入都出现锁定的情况. 1.session_start();session_commit(); 2.session_start();session_write_c ...
- eval 如何定义函数
eval(compile('''def fun(): print 'bbb' ''', '<string>', 'exec')) fun()
- Python深入学习笔记(一)
写在前面的话 从08年接触Python到现在,断断续续地使用,到如今Python已经成为日常事物处理.科研实验,甚至工程项目的主力语言,主要因为其敏捷性和快速实现的能力.虽然看了一些Python的教程 ...
- 【WPF】逻辑树和视觉树
WPF中提供了遍历逻辑树和视觉树的辅助类:System.Windows.LogicalTreeHelper和 System.Windows.Media.VisualTreeHelper. 注意遍历的位 ...
- AppStore IPv6-only审核被拒原因分析及解决方案-b
自2016年6月1日起,苹果要求所有提交App Store的iOS应用必须支持IPv6-only环境,背景也是众所周知的,IPv4地址已基本分配完毕,同时IPv6比IPv4也更加高效,向IPv6过渡是 ...
- but has failed to stop it. This is very likely to create a memory leak(c3p0在Spring管理中,连接未关闭导致的内存溢出)
以下是错误日志信息: 严重: The web application [/news] registered the JDBC driver [com.mysql.jdbc.Driver] but fa ...
- python 处理cookie简单很多啊 httpclient版本是4.3.3
模拟登录流程: 1 请求host_url 2 从host_url中解析出 隐藏表单 的值 添加到POST_DATA中 3 添加账户,密码到POST_DATA中 4 编码后,发送POST请求 要点 ...
- Sprint5
进展:今天开始进行了登录界面的编写及实现. 燃尽图: 工作照: