时间限制
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. 利用传感器(sensor)实现微信摇一摇动画

    所需要的权限: <uses-permission android:name="android.permission.VIBRATE"></uses-permiss ...

  2. Content-Type一览

    文件扩展名 Content-Type(Mime-Type) 文件扩展名 Content-Type(Mime-Type) .*( 二进制流,不知道下载文件类型) application/octet-st ...

  3. HTML5 indexedDB数据库的入门学习(二)

    上一篇关于indexedDB的学习笔记主要写了indexedDB数据库的基本操作—增删改查:但是为什么我们要用indexedDB呢?为什么indexedDB受到了开发者们的青睐呢?最主要的就是inde ...

  4. ubuntu共享文件夹给virtualbox

    在ubuntu或者linuxmint等linux系统下安装了virtualbox,可以通过共享文件夹的方式,把文件夹共享给virtualbox下的虚拟机系统,我这里的虚拟机系统是win7,共享过程如下 ...

  5. IntellJ 13.x JPA Persistence Sample

    跟上一篇差不多,一些基本的东西. 这次是JPA + Spring MVC 3.0 1.建立Project 2.Add JPA Support 3.我们以Hibernate为例,设置JPA的Provid ...

  6. HDOJ2027统计元音

    统计元音 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  7. Javascript之spry菜单栏

    我没有添加任何东西,这是Dreamweaver原汁原味用spry创建的菜单栏,以此来学习菜单导航,哈哈. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  8. Android重力感应开发

    http://blog.csdn.net/mad1989/article/details/20848181 一.手机中常用的传感器 在Android2.3 gingerbread系统中,google提 ...

  9. CSS常用中文字体、字号

    字体(font-family): 新细明体:PMingLiU 细明体:MingLiU 标楷体:DFKai-SB 黑体:SimHei 宋体:SimSun 新宋体:NSimSun 仿宋:FangSong  ...

  10. python之平台独立的调试工具winpdb介绍

    Winpdb is a platform independent graphical GPL Python debugger with support for remote debugging ove ...