PAT-1018(Public Bike Management)最短路+额外条件+所有最短路中找出满足条件的路径+dijkstra算法
Public Bike Management
PAT-1018
- 使用一个vector来存储所有最短路的前驱结点,再通过使用dfs和一个额外的vector记录每一条路径
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
using namespace std;
const int maxn=502;
const int maxm=250004;
const int INF=0X3F3F3F3F;
int cmax,n,m,pi;
int ci[maxn];
int head[maxn];
int top;
int d[maxn];
vector<int>pre[maxn];//记录路径也就是前驱结点
int minneed=INF,minback=INF;
int temp;//当前收集了多少单车数量
vector<int>tempve;
vector<int>path;//真正符合题意的路径
struct Edge{
int to,cost,next;
};
Edge edge[maxm];
struct Node{
int to,dis;
Node(){
}
Node(int a,int b):to(a),dis(b){
}
bool operator<(const Node& node)const{
return dis>node.dis;//从大到小排序,越小的元素优先级越高
}
};
void dijkstra(int s){
priority_queue<Node>que;
que.push(Node(s,0));
for(int i=0;i<=n;i++){
d[i]=INF;
}
d[s]=0;
while(!que.empty()){
Node now=que.top();
que.pop();
int to=now.to;
int dis=now.dis;
if(d[to]<dis)
continue;
for(int i=head[to];i!=-1;i=edge[i].next){
Edge e=edge[i];
if(d[e.to]>d[to]+e.cost){
d[e.to]=d[to]+e.cost;
que.push(Node(e.to,d[e.to]));
pre[e.to].clear();
pre[e.to].push_back(to);
}else if(d[e.to]==d[to]+e.cost){
// que.push(Node(e.to,d[e.to]));
pre[e.to].push_back(to);
}
}
}
}
void dfs(int v){
if(v==0){//调度中心
tempve.push_back(v);
int takefrom=0,takeback=0;
for(int i=tempve.size()-1;i>=0;i--){//从起点开始遍历
int id=tempve[i];
// cout<<id<<" ";
if(ci[id]>0){//ci[id]>5
takeback+=ci[id];
} else{
if(takeback>-ci[id]){
takeback+=ci[id];
}else{
takefrom+=(-ci[id]-takeback);//缺失的比需要带回的更多
takeback=0;
}
}
}
// cout<<endl;
if(takefrom<minneed){
minneed=takefrom,minback=takeback,path=tempve;
}else if(takefrom==minneed&&takeback<minback){
minback=takeback,path=tempve;
}
tempve.pop_back();
return;
}
tempve.push_back(v);
for(int i=0;i<pre[v].size();i++){
dfs(pre[v][i]);
}
tempve.pop_back();
}
void addEdge(int a,int b,int c){
edge[top].to=b;
edge[top].cost=c;
edge[top].next=head[a];
head[a]=top++;
}
int main(){
top=0;
memset(head,-1,sizeof(head));
cin>>cmax>>n>>pi>>m;
for(int i=1;i<=n;i++){
cin>>ci[i];
ci[i]-=cmax/2;
}
for(int i=0;i<m;i++){
int a,b,c;
cin>>a>>b>>c;
addEdge(a,b,c);
addEdge(b,a,c);
}
dijkstra(0);
dfs(pi);
reverse(path.begin(),path.end());
cout<<minneed<<" ";
for(int i=0;i<path.size();i++){
if(i==(int)path.size()-1){
cout<<path[i]<<" ";
}else cout<<path[i]<<"->";
}
cout<<minback<<endl;
return 0;
}
PAT-1018(Public Bike Management)最短路+额外条件+所有最短路中找出满足条件的路径+dijkstra算法的更多相关文章
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- PAT 1018. Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- 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 ...
- 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
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 1018 Public Bike Management (30)(30 分)
时间限制400 ms 内存限制65536 kB 代码长度限制16000 B There is a public bike service in Hangzhou City which provides ...
- pat 甲级 Public Bike Management
Public Bike Management (30) 题目描述 There is a public bike service in Hangzhou City which provides grea ...
随机推荐
- poj3415 Common Substrings (后缀数组+单调队列)
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 9414 Accepted: 3123 Description A sub ...
- Beautiful numbers CodeForces - 55D
题意: 找出区间[li,ri]内有多少数满足,这个数的每一个位的非0数都能把这个数整除 题解: 因为这个数每一位的值都可以把这个数整除,那也就是说这个数是它所有位数的公倍数,但是可能不是最小公倍数. ...
- entity framwork修改指定字段
1.ef修改时指修改指定字段public void ChangePassword(int userId, string password) { var user = new User() { Id = ...
- 快速获取 Wi-Fi 密码——GitHub 热点速览 v.21.06
作者:HelloGitHub-小鱼干 还有 2 天开启春节七天宅家生活,GitHub 也凑了一把春节热闹,wifi-password 连续霸榜 3 天,作为一个能快速让你连上 Wi-Fi 的小工具,春 ...
- SpringCloud @RefreshScope标注的Bean在生命周期中怎么样的?
@RefreshScope 前言 该文章是由上一篇引申而来的,上一篇文章描述了Nacos是怎么触发配置刷新的,接着来写有关@RefreshScope的一些东西,都是由DEBUG而来 上一篇 文章概览 ...
- 国产网络损伤仪SandStorm -- 基本概念:什么是仿真引擎
"仿真引擎"在网络损伤仪SandStorm(www.minismb.com)或者网络IP仿真损伤仪中是一个最基本概念,它就相当于一个由两个物理以太网口组成的"网桥&quo ...
- 浅谈Webpack模块打包工具四
Webpack 生产环境优化 生产环境和开发环境有很大的差异,生产环境只注重运行效率,开发环境主要开发效率,webpack4.0开始提出了(mode)模式的概念 针对不同的环境进行不同的配置,为不同的 ...
- 爬虫入门五 gooseeker
title: 爬虫入门五 gooseeker date: 2020-03-16 16:00:00 categories: python tags: crawler gooseeker是一个简单的爬虫软 ...
- 二、mycat基础知识、基本配置
官网 http://www.mycat.io/ Mycat 概要介绍 https://github.com/MyCATApache/Mycat-Server 入门指南 https://github.c ...
- Ubuntu 编译并执行含opencv的cpp文件
# compilation g++ main.cpp -o main `pkg-config --cflags --libs opencv` # execution ./img-display lin ...