时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

 #include<stdio.h>
#include<vector>
#include<cmath>
using namespace std;
#define MAX 510
int INF = ;
int wight[MAX];
int Grap[MAX][MAX];
int d[MAX]; //距离
bool visit[MAX];
vector<int> adj[MAX],tempath,path;
int MINNeed = INF;
int MINRemain = INF; void Dijkstra(int begin,int NodeNum)
{
d[begin] = ;
for(int i= ; i<= NodeNum ; i++)
{
int index = -;
int MIN = INF ;
for(int j = ; j <= NodeNum ; j ++)
{
if(visit[j] == false && d[j] < MIN)
{
index = j ;
MIN = d[j];
}
}
if(index == -) return; visit[index] = true;
for(int v = ; v<= NodeNum ;v++)
{
if(visit[v] == false && Grap[index][v] != INF)
{
if(d[index] + Grap[index][v] < d[v])
{
d[v] = d[index] + Grap[index][v];
adj[v].clear();
adj[v].push_back(index);
}
else if(d[index] + Grap[index][v] == d[v])
{
adj[v].push_back(index);
}
}
}
}
} void DFS(int Sp)
{
if(Sp == )
{
tempath.push_back(Sp);
int need= ;//需要带出去的
int remain = ;//回收的
for(int i = tempath.size() - ; i >= ;i--)
{
int index = tempath[i];
if(wight[index] > )
{
remain += wight[index];
}
else
{
if(remain > abs(wight[index]))
{
remain += wight[index];
}
else
{
need += abs(wight[index]) - remain;
remain = ;
}
} } if(need < MINNeed)
{
MINNeed = need ;
MINRemain = remain;
path = tempath;
}
else if(need == MINNeed && remain < MINRemain)
{
MINRemain = remain;
path = tempath;
} tempath.pop_back();
return ;
}
tempath.push_back(Sp);
for(int i = ;i < adj[Sp].size() ;i++)
{
DFS(adj[Sp][i]);
}
tempath.pop_back();
} int main()
{
int i,j,Cmax,N,Sp,M;
scanf("%d%d%d%d",&Cmax,&N,&Sp,&M);
for(i = ; i <= N ;i ++)
{
scanf("%d",&wight[i]);
wight[i] -= (Cmax/);
} for(i = ; i <= N ; i ++)
{
for(j = ; j <= N;j ++)
{
Grap[i][j]= INF;
} d[i] = INF;
}
int x,y;
for(i = ; i < M ; i ++)
{
scanf("%d%d",&x,&y);
scanf("%d",&Grap[x][y]);
Grap[y][x] = Grap[x][y];
} Dijkstra(,N); DFS(Sp);
printf("%d ",MINNeed);
for(i = path.size() -;i>= ; i --)
{
if(i == path.size()-)
printf("%d",path[i]);
else printf("->%d",path[i]);
}
printf(" %d\n",MINRemain);
return ;
}

1018. Public Bike Management (30)的更多相关文章

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

  2. 1018 Public Bike Management (30)(30 分)

    时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...

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

  4. 1018 Public Bike Management (30 分)

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

  5. 1018 Public Bike Management (30分) 思路分析 + 满分代码

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

  6. PAT A 1018. Public Bike Management (30)【最短路径】

    https://www.patest.cn/contests/pat-a-practise/1018 先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路 ...

  7. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  8. PAT (Advanced Level) 1018. Public Bike Management (30)

    先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...

  9. 1018 Public Bike Management (30) Dijkstra算法 + DFS

    题目及题解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪杰斯特拉重新认识 两个核心的存储结构: int dis[n]: //记录每 ...

随机推荐

  1. 关于错位动画的练习,原生js编写

    最近在网上看到一个关于错位动画的文章,感觉非常有趣,便自己练习了一下,文章连接:http://www.w3cplus.com/animation/staggering-animations.html ...

  2. TextFiled 中输入金额

    要求: 输入的金额不能超过六位, 小数点后面只能输入两位小数 如果 textFIled  中第一位输入的是0 ,后面必须输入小数点,否则禁止输入 用到 textfiled代理方法 #pragma ma ...

  3. listagg( ) within group ( order by ) 与 wm_concat

    listagg( ) within group ( order by ) 与 wm_concat --oracle 11g 及以后适合 最好 select spbywslid,listagg(xm,' ...

  4. Ehcache(2.9.x) - API Developer Guide, Cache Decorators

    About Cache Decorators Ehcache uses the Ehcache interface, of which Cache is an implementation. It i ...

  5. 【网络收集】Sql Server datetime 常用日期格式转换

    ) , sfrq, ) 我们经常出于某种目的需要使用各种各样的日期格式,当然我们可以使用字符串操作来构造各种日期格式,但是有现成的函数为什么不用呢? SQL Server中文版的默认的日期字段date ...

  6. KVC/KVO总结

    KVC(键值编码) 动态设置: setValue:属性值 forKey:属性名(用于简单路径) setValue:属性值 forKeyPath:属(用于复合路径,例如Person有一个Account类 ...

  7. Objective-C 【关于导入类(@class 和 #import的区别)】

    之前我们分析过 #include 和 #import 的区别,#import不会引起交叉编译,#import 确定一个文件只能被导入一次,使在递归包含中不会出现问题. 那么 #import 和 @cl ...

  8. (转)MongoDb的十个使用要点

    从 mongodb 阶段性技术总结 中抽取并整理了对大家有帮助的十个要点:   1.mongodb 表名和字段名统一用小写字母 mongodb 是默认区分大小写的,为了避免以前在 mysql 下遇到的 ...

  9. css3学习笔记之图片

    圆角图片 border-radius: 圆角图片: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 <!DOCTYPE html> <htm ...

  10. 腾讯QQ表情为什么如此成功呢

    本人为原创作品:e良师益友 ,转载是并且注明 e良师益友网导读:腾讯开发的QQ表情功能给中国人的聊天增添一抹幽默,很多时候图片表情比话语更好的表达我们的意思,翻开你的聊天记录就会发现夹杂这很多不同的表 ...