1018. Public Bike Management (30)
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)的更多相关文章
- 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 ...
- 1018 Public Bike Management (30)(30 分)
时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...
- 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 ...
- 1018 Public Bike Management (30 分)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 1018 Public Bike Management (30分) 思路分析 + 满分代码
题目 There is a public bike service in Hangzhou City which provides great convenience to the tourists ...
- PAT A 1018. Public Bike Management (30)【最短路径】
https://www.patest.cn/contests/pat-a-practise/1018 先用Dijkstra算出最短路,然后二分答案来验证,顺便求出剩余最小,然后再从终点dfs回去求出路 ...
- 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs
前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...
- PAT (Advanced Level) 1018. Public Bike Management (30)
先找出可能在最短路上的边,图变成了一个DAG,然后在新图上DFS求答案就可以了. #include<iostream> #include<cstring> #include&l ...
- 1018 Public Bike Management (30) Dijkstra算法 + DFS
题目及题解 https://blog.csdn.net/CV_Jason/article/details/81385228 迪杰斯特拉重新认识 两个核心的存储结构: int dis[n]: //记录每 ...
随机推荐
- How to update FVDI Commander driver to latest V2015.6.2
As FVDI Commander products are upgraded to new versions, I often receive emails from customers askin ...
- jQuery插件面向对象开发
为什么要有面向对象的思维,因为如果不这样,你可能需要一个方法的时候就去定义一个function,当需要另外一个方法的时候,再去随便定义一个function,同样,需要一个变量的时候,毫无规则地定义一些 ...
- 在SSIS 的 64 位版本中不支持 Excel 连接管理器
Microsoft sql server 2008 R2——> SQL SERVER Business Intelligence Development Studio 使用EXCEL数据源或目标 ...
- 为MYPoint类写一个分类
#import <Foundation/Foundation.h> //xieyi @protocol showOn @required -(void)printOn; @end // l ...
- Java之MS SQL数据库连接
一 1.首先,到微软官方下载jdbc驱动包 Microsoft JDBC Driver 4.0 for SQL Server 2.运行sqljdbc_4.0.2206.100_chs.exe,把文件 ...
- LeetCode 292
Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the t ...
- [改善Java代码]不要让类型默默转换
建议23:不要让类型默默转换 public class Client { // 光速是30万公里/秒,常量 public static final int LIGHT_SPEED = 30 * 100 ...
- 【基础数学知识】UVa 11314 - Hardly Hard
Problem H HARDLY HARD You have been given the task of cutting out a quadrilateral slice of cake out ...
- html5 js os build
1.NodeJS6.6 install https://nodejs.org/en/ c:\nodejs>npm install -g grunt-cliC:\Users\police\Ap ...
- (转载)运行主机管理在openvswitch之上
在这篇文章里介绍了如果运行主机管理在openvswitch之上,而不是单独配置一个物理网卡用于主机管理,并且所有的vm的流量还是通过openvswitch走的. Running Host Manage ...