时间限制
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. Linux基本命令之逻辑测试二

    1.首先介绍一个与test一样的测试方式[ expression ](千万注意expression的前后都有空格,没有空格的话会报错) 这个测试方式经常作为if的条件. /home/www这个文件名存 ...

  2. [改善Java代码]正确使用String,StringBuffer,StringBuilder

    CharSequence接口有三个实现类与字符串有关:String,StringBuffer,StringBuffer.虽然它们都与字符串有关,但是其处理机制是不同的. String类是不可改变的量, ...

  3. 【优先队列】【最近连STL都写不出来了/(ㄒoㄒ)/~~】hdu_5360/多校#6_1008

    题意:就是让你写出一个邀请朋友的顺序,朋友答应一起出去玩的条件是除他以外所有同意出去玩的人数要在[ l[i], r[i] ]范围内,否则他就不答应. 分析:这题比赛的时候想麻烦了,其实只要在左边界满足 ...

  4. (转载)HashMap工作原理

    HashMap是近些年来java面试中常问到的知识点,很多人(包括我在内)都知道HashMap的用法,也知道HashMap与HashTable之间的区别,但是却不知其所以然,于是乎,本人开始查阅相关资 ...

  5. MJViewController的view的创建

  6. MVC清除缓存设置+数据验证

    [OutputCache(Location = OutputCacheLocation.None, NoStore = true)] [Table("User")]:定义UserD ...

  7. 【hdu2896】病毒侵袭

    题目描述 当太阳的光辉逐渐被月亮遮蔽,世界失去了光明,大地迎来最黑暗的时刻....在这样的时刻,人们却异常兴奋--我们能在有生之年看到500年一遇的世界奇观,那是多么幸福的事儿啊~~但网路上总有那么些 ...

  8. Javascript之三种按钮点击事件

    学习Javascript必须要先掌握基本的事件方法和语法,这些都是我们学过的也是最基本的.以前忘了总结,所以现在回顾,综合地总结一下,温故而知新. Javascript有三种按钮点击事件,分别为ale ...

  9. JSP连接mysql 驱动包

    360网盘 https://yunpan.cn/cPxT6CV9Kydyb  访问密码 1df9

  10. T-SQL 使用链接库向mysql导数据遇到的奇葩事件一

    mysql表结构有 主键 非自增 text longtext类型字段多个 步骤 1.在T-SQL 临时表中处理好所有需要的字段 2.执行openquery语句 字段顺序完全按照mysql字段顺序插入 ...