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 ...
随机推荐
- mongodb 非 admin 库 认证登陆失败 原因(百度好多都 是渣)db.addUser() 请走开。
首先先晒一下log 日志错误信息 2016-07-13T22:19:43.667+0800 I ACCESS [conn4] authenticate db: finddemo { aut henti ...
- WPF拖动绘制
using System; using System.Windows; using System.Windows.Controls; using System.Windows.Input; using ...
- Javascript 备忘
1遍历所有属性 var person={fname:"John",lname:"Doe",age:25}; for (x in person) { txt=tx ...
- BZOJ 1827: [Usaco2010 Mar]gather 奶牛大集会
Description Bessie正在计划一年一度的奶牛大集会,来自全国各地的奶牛将来参加这一次集会.当然,她会选择最方便的地点来举办这次集会.每个奶牛居住在 N(1<=N<=100,0 ...
- BZOJ 1642: [Usaco2007 Nov]Milking Time 挤奶时间
Description 贝茜是一只非常努力工作的奶牛,她总是专注于提高自己的产量.为了产更多的奶,她预计好了接下来的N (1 ≤ N ≤ 1,000,000)个小时,标记为0..N-1. Farmer ...
- [原博客] POI系列(1)
正规.严谨.精妙. -POI 发现POI(波兰信息学奥赛)的题都很有意思.于是开刷bzoj上的poi题目(按ac人数降序..).顺手写一写题解,加深印象. 为了防止一篇文章过于长,打算每五道题另起一篇 ...
- 李洪强iOS开发Swift篇—09_属性
李洪强iOS开发Swift篇—09_属性 一.类的定义 Swift与Objective-C定义类的区别 Objective-C:一般需要2个文件,1个.h声明文件和1个.m实现文件 Swift:只需要 ...
- zImage和uImage的区别
http://blog.csdn.net/maojudong/article/details/4178118 zImage和uImage的区别 一.vmlinuz vmlinuz是可引导的.压缩的内核 ...
- VC下载文件显示进度条
VC下载文件显示进度条 逗比汪星人2009-09-18上传 by Koma http://blog.csd.net/wangningyu http://download.csdn.net/deta ...
- Linux平台下Java调用C函数
JNI是Java native interface的简写,可以译作Java原生接口.Java可以通过JNI调用C/C++的库,这对于那些对性能要求比较高的Java程序无疑是一个 福音. 使用JNI也是 ...