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. USB Key插入和移除监控

    近期在做USB Key插入和移除监控,已经做到了插入和移除USB Key时,程序能够及时感应到. 如下为源代码: private void Form1_Load(object sender, Even ...

  2. uboot 顶层makefile细节分析

    uboot的源文件众多,学习庞然大物首先找到脊椎--顶层的makfile,逐一破解.但是,uboot的makefile同样是一个庞然大物,所以也要找到它的主线.倘若过分专注部分细节,很难做到把握全局, ...

  3. makefile debug

    1. 使用warning指令 warning 是个不错的命令,可以打印出消息,来判断makefile执行的流程 2.使用ifeq ifneq 当makefile被多次调用到的时候,如果都输出warni ...

  4. iPhone不同机型适配 6/6plus --备用

     机型变化 坐标:表示屏幕物理尺寸大小,坐标变大了,表示机器屏幕尺寸变大了: 像素:表示屏幕图片的大小,跟坐标之间有个对应关系,比如1:1或1:2等: ppi:代表屏幕物理大小到图片大小的比例值,如果 ...

  5. ORACLE 常用系统函数

    1.  字符类 1.1  ASCII(c ) 函数  和CHR( i )      ASCII 返回一个字符的ASCii码,其中c表示一个字符;CHR 返回ascii码值i 所对应的字符 . 如: S ...

  6. bzoj 2852: 强大的区间 辗转相除

    2852: 强大的区间 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 45  Solved: 12[Submit][Status][Discuss] D ...

  7. [原博客] POJ 2975 Nim 统计必胜走法个数

    题目链接题意介绍了一遍Nim取石子游戏,可以看上一篇文章详细介绍.问当前状态的必胜走法个数,也就是走到必败状态的方法数. 我们设sg为所有个数的Xor值.首先如果sg==0,它不可能有必胜走法,输出0 ...

  8. DHTMLX 前端框架 建立你的一个应用程序教程(四)--添加一个工具条toolbar

    工具条例子 样本如下: 这里我们使用的是dhtmlxToolbar 组件. 添加工具栏到布局中: 1.使用attachToolbar() 方法初始化页面 添加代码到index.html中 dhtmlx ...

  9. 一个简单的DDraw应用程序2

    //------------------------------------------------------------------------- // 文件名 : 6_1.cpp// 创建者 : ...

  10. Multi-bit per cell storage

    Memories Scaling      其他的的半导体存储器的制程一般2年为一个升级周期,但是nand flash 存储器的制程升级周期和他们比起来只有1年.这种更快的制程升级导致SLC NAND ...