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的更多相关文章

  1. PAT 1018 Public Bike Management[难]

    链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018  Public ...

  2. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  3. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  4. 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 ...

  5. 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 ...

  6. 1018. Public Bike Management (30)

    时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue There is a public bike service i ...

  7. 1018 Public Bike Management

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  8. PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  9. PTA (Advanced Level) 1018 Public Bike Management

    Public Bike Management There is a public bike service in Hangzhou City which provides great convenie ...

随机推荐

  1. CGDataCmd

    1,"Get Inf Joint from file" 选择文件中储存的骨骼信息; 2,"Export skinWeight"   导出权重;  3," ...

  2. 聊聊Web App、Hybrid App与Native App的设计差异

    目前主流应用程序大体分为三类:Web App.Hybrid App. Native App. 一.Web App.Hybrid App.Native App 纵向对比 首先,我们来看看什么是 Web ...

  3. POJ 2531 Network Saboteur 位运算子集枚举

    题目: http://poj.org/problem?id=2531 这个题虽然是个最大割问题,但是分到dfs里了,因为节点数较少.. 我试着位运算枚举了一下,开始超时了,剪了下枝,1079MS过了. ...

  4. 加密算法 - RSA算法二

    RSA算法原理(二)  声明: 本文转自阮一峰 (http://www.ruanyifeng.com/blog/2013/07/rsa_algorithm_part_two.html) 有了这些知识, ...

  5. uva 812 Trade on Verweggistan

    题意: 给w个货架, 每个货架上有bi个货物, 每次只能拿最上面的货物, 每个货物有个价值, 所有货物的售价均为10. 问:能获得的最大利润, 以及能获得这个利润需要多少个货物. (有多种组合时只需输 ...

  6. Response.Redirect:无法在发送 HTTP 标头之后进行重定向

    URL:http://blog.163.com/asp_neter/blog/static/17510918820107258107558/ 错误出现语句:“Response.Redirect(&qu ...

  7. JQuery replace 替换全部

    天在做写个程序时遇到需要替换的功能,可是一开始用jquery的replace时,发现只替换到第一个.最后没办法,只好用正则表达式来例如下面   re = new RegExp("{thisc ...

  8. python基础知识(引用)

    文章连接:http://xianglong.me/article/how-to-code-like-a-pythonista-idiomatic-python/

  9. zigbee智能家居基础扫盲

    zigbee Zigbee是基于IEEE802.15.4标准的低功耗个域网协议.根据这个协议规定的技术是一种短距离.低功耗的无线通信技术.这一名称来源于蜜蜂的八字舞,由于蜜蜂(bee)是靠飞翔和&qu ...

  10. AFNetworking网络请求的get和post步骤

      1.首先通过第三方:CocoaPods下载AFNetworking 1.1.先找到要查找的三方库:pod search + AFNetworking 1.2.出来一堆列表页面,选择三方库最新版本命 ...