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]: //记录每 ...
随机推荐
- Java基础知识强化102:线程间共享数据
一.每个线程执行的代码相同: 若每个线程执行的代码相同,共享数据就比较方便.可以使用同一个Runnable对象,这个Runnable对象中就有那个共享数据. public class MultiThr ...
- TextFiled 中输入金额
要求: 输入的金额不能超过六位, 小数点后面只能输入两位小数 如果 textFIled 中第一位输入的是0 ,后面必须输入小数点,否则禁止输入 用到 textfiled代理方法 #pragma ma ...
- IC卡写卡操作流程
var icData = new ICData(); var deviceResult = crd.CRDICPowerOn(); if (!deviceResult.IsSuccess) retur ...
- VIJOS P1540 月亮之眼
[题目大意] 有多个珠子,给出部分珠子之间的相对上下位置和间距,问你这些珠子在满足给出的条件下,是否能把珠子排列在一条竖直直线上,如果能,求出每个珠子距离最高的珠子的距离,珠子的位置可重叠. [分析] ...
- LeetCode 278
First Bad Version You are a product manager and currently leading a team to develop a new product. U ...
- 关于Windows下如何查看端口占用和杀掉进程
更详细博客参见: http://www.cnblogs.com/chenwenbiao/archive/2012/06/24/2559954.html 或可参考:http://www.cnblogs. ...
- Java从Jar文件中动态加载类
动态加载jar包,在实际开发中经常会需要用到,尤其涉及平台和业务的关系的时候,业务逻辑部分可以独立出去交给业务方管理,业务方只需要提供jar包,就能在平台上运行. 下面通过一个实例来直观演示: 第一: ...
- Java Concurrency - invokeAny & invokeAll
Running multiple tasks and processing the first result A common problem in concurrent programming is ...
- 面试之SQL(1)--选出选课数量>=2的学号
ID Course 1 AA 1 BB 2 AA 2 BB 2 CC 3 AA 3 BB 3 CC 3 DD 4 AA NULL NULL 选出选课数量>=2的学号 selectdis ...
- AIDL进程间调用与Binder的简单介绍
Binder是安卓中特有的一种进程间通信(IPC)方式,从Unix发展而来的手段,通信双方必须处理线程同步.内存管理等复杂问题,传统的Socket.匿名通道(Pipe).匿名管道(FIFO).信号量( ...