PAT A1018
A 1018 Public Bike Management
这个题目算是比较典型的一个。我分别用dfs,及dijkstra+dfs实现了一下。
dfs实现代码:
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define MAXNUM 510
#define CLR(a,b) memset(a,b,sizeof(a));
const int INF = 0x7f7f7f7f;
int C, N, Sp, M, dstDis = INF, tmpDstDis = , dstSendBikeNum = INF, dstBackBikeNum = INF;
vector<int> dstPathVec, tmpPathVec, routeVec[MAXNUM];
vector<int> bikeNumStaVec(MAXNUM,);
vector<int> visitFlagVec(MAXNUM,false);
int route[MAXNUM][MAXNUM];
void dfs(int u)
{
if(u == Sp && tmpDstDis <= dstDis)
{
int halfC = C/, tmpBikeNum = , tmpSendBikeNum = , tmpBackBikeNum = ;
for(int i = ; i < tmpPathVec.size(); ++ i)
{
tmpBikeNum += halfC-bikeNumStaVec[tmpPathVec[i]];
if(tmpBikeNum > )
{
tmpSendBikeNum += tmpBikeNum;
tmpBikeNum = ;
}
}
tmpBackBikeNum = -tmpBikeNum;
if(tmpDstDis < dstDis)
{
dstSendBikeNum = tmpSendBikeNum;
dstBackBikeNum = tmpBackBikeNum;
dstDis = tmpDstDis;
dstPathVec = tmpPathVec;
}
else if((tmpSendBikeNum < dstSendBikeNum) || (dstSendBikeNum == tmpSendBikeNum && dstBackBikeNum > tmpBackBikeNum))
{
dstSendBikeNum = tmpSendBikeNum;
dstBackBikeNum = tmpBackBikeNum;
dstDis = tmpDstDis;
dstPathVec = tmpPathVec;
}
return;
}
if(tmpDstDis > dstDis)
return;
for(auto it = routeVec[u].begin(); it != routeVec[u].end(); ++ it)
{
if(!visitFlagVec[*it])
{
visitFlagVec[*it] = true;
tmpPathVec.push_back(*it);
tmpDstDis += route[u][*it];
dfs(*it);
tmpDstDis -= route[u][*it];
tmpPathVec.pop_back();
visitFlagVec[*it] = false;
}
}
}
int main()
{
cin >> C >> N >> Sp >> M;
int tmpSt, tmpEnd, tmpDis;
for(int i = ; i <= N; ++i)
cin >> bikeNumStaVec[i];
CLR(route,0x7f);
while(M--)
{
cin >> tmpSt >> tmpEnd >> tmpDis;
route[tmpSt][tmpEnd] = tmpDis;
route[tmpEnd][tmpSt] = tmpDis;
routeVec[tmpSt].push_back(tmpEnd);
routeVec[tmpEnd].push_back(tmpSt);
}
visitFlagVec[] = true;
tmpPathVec.push_back();
dfs();
cout << dstSendBikeNum;
for(int i = ; i < dstPathVec.size(); ++i)
{
if(i == ) printf(" ");
else printf("->");
cout << dstPathVec[i];
}
cout << " " << dstBackBikeNum;
return ;
}
dijkstra和dfs实现代码
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define MAXNUM 510
#define CLR(a,b) memset(a,b,sizeof(a));
const int INF = 0x7f7f7f7f;
int C, N, Sp, M, dstSendBikeNum = INF, dstBackBikeNum = INF;
vector<int> dstPathVec, tmpPathVec, preRoute[MAXNUM];
vector<int> bikeNumStaVec(MAXNUM,);
vector<int> visitFlagVec(MAXNUM,false);
int dis[MAXNUM];
int route[MAXNUM][MAXNUM];
void dijkstra(int u)
{
dis[u] = ;
for(int i = ; i <= N; ++ i)
{
int tmpMinDis = INF, v = -;
for(int j = ; j <= N; ++ j)
{
if(!visitFlagVec[j] && dis[j] < tmpMinDis)
{
v = j;
tmpMinDis = dis[j];
}
}
if(v == - || v == Sp)
return;
visitFlagVec[v] = true;
for(int j = ; j <= N; ++ j)
{
if(!visitFlagVec[j] && dis[j] > dis[v] + route[v][j])
{
dis[j] = dis[v] + route[v][j];
preRoute[j].clear();
preRoute[j].push_back(v);
}
else if(!visitFlagVec[j] && dis[j] == dis[v] + route[v][j])
preRoute[j].push_back(v);
}
}
}
void dfs(int u)
{
if(u == )
{
tmpPathVec.push_back(u);
int halfC = C/, tmpBikeNum = , tmpSendBikeNum = , tmpBackBikeNum = ;
for(int i = tmpPathVec.size()-; i >= ; -- i)
{
tmpBikeNum += halfC-bikeNumStaVec[tmpPathVec[i]];
if(tmpBikeNum > )
{
tmpSendBikeNum += tmpBikeNum;
tmpBikeNum = ;
}
}
tmpBackBikeNum = -tmpBikeNum;
if((tmpSendBikeNum < dstSendBikeNum) || (dstSendBikeNum == tmpSendBikeNum && dstBackBikeNum > tmpBackBikeNum))
{
dstSendBikeNum = tmpSendBikeNum;
dstBackBikeNum = tmpBackBikeNum;
dstPathVec = tmpPathVec;
}
tmpPathVec.pop_back();
return;
}
tmpPathVec.push_back(u);
for(auto it = preRoute[u].begin(); it != preRoute[u].end(); ++ it)
dfs(*it);
tmpPathVec.pop_back();
}
int main()
{
cin >> C >> N >> Sp >> M;
int tmpSt, tmpEnd, tmpDis;
for(int i = ; i <= N; ++i)
cin >> bikeNumStaVec[i];
CLR(route,0x7f);
CLR(dis,0x7f);
while(M--)
{
cin >> tmpSt >> tmpEnd >> tmpDis;
route[tmpSt][tmpEnd] = tmpDis;
route[tmpEnd][tmpSt] = tmpDis;
}
dijkstra();
dfs(Sp);
cout << dstSendBikeNum;
reverse(dstPathVec.begin(), dstPathVec.end());
for(int i = ; i < dstPathVec.size(); ++i)
{
if(i == ) printf(" ");
else printf("->");
cout << dstPathVec[i];
}
cout << " " << dstBackBikeNum;
return ;
}
另:一种错误(dfs中,最后判断第二条件时出现了错误)
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstring>
using namespace std;
#define MAXNUM 510
#define CLR(a,b) memset(a,b,sizeof(a));
const int INF = 0x7f7f7f7f;
int C, N, Sp, M, dstSendBikeNum = INF, dstBackBikeNum = INF;
vector<int> dstPathVec, tmpPathVec, preRoute[MAXNUM];
vector<int> bikeNumStaVec(MAXNUM,);
vector<int> visitFlagVec(MAXNUM,false);
int dis[MAXNUM];
int route[MAXNUM][MAXNUM];
void dijkstra(int u)
{
dis[u] = ;
for(int i = ; i <= N; ++ i)
{
int tmpMinDis = INF, v = -;
for(int j = ; j <= N; ++ j)
{
if(!visitFlagVec[j] && dis[j] < tmpMinDis)
{
v = j;
tmpMinDis = dis[j];
}
}
if(v == - || v == Sp)
return;
visitFlagVec[v] = true;
for(int j = ; j <= N; ++ j)
{
if(!visitFlagVec[j] && dis[j] > dis[v] + route[v][j])
{
dis[j] = dis[v] + route[v][j];
preRoute[j].clear();
preRoute[j].push_back(v);
}
else if(!visitFlagVec[j] && dis[j] == dis[v] + route[v][j])
preRoute[j].push_back(v);
}
}
}
void dfs(int u)
{
if(u == )
{
tmpPathVec.push_back(u);
int tmpBikeNum = , tmpBackBikeNum = ;
for(int i = ; i < tmpPathVec.size()-; ++i)
{
tmpBikeNum += bikeNumStaVec[tmpPathVec[i]]-C/;
if(tmpBikeNum > )
{
tmpBackBikeNum += tmpBikeNum;
tmpBikeNum = ;
}
}
tmpBikeNum = -tmpBikeNum;
if(tmpBikeNum < dstSendBikeNum)
{
dstSendBikeNum = tmpBikeNum;
dstBackBikeNum = tmpBackBikeNum;
dstPathVec = tmpPathVec;
}
else if(tmpBikeNum == dstSendBikeNum && tmpBackBikeNum < dstBackBikeNum)
{
dstPathVec == tmpPathVec;
dstBackBikeNum = tmpBackBikeNum;
}
tmpPathVec.pop_back();
return;
}
tmpPathVec.push_back(u);
for(auto it = preRoute[u].begin(); it != preRoute[u].end(); ++ it)
dfs(*it);
tmpPathVec.pop_back();
}
int main()
{
cin >> C >> N >> Sp >> M;
int tmpSt, tmpEnd, tmpDis;
for(int i = ; i <= N; ++i)
cin >> bikeNumStaVec[i];
CLR(route,0x7f);
CLR(dis,0x7f);
while(M--)
{
cin >> tmpSt >> tmpEnd >> tmpDis;
route[tmpSt][tmpEnd] = tmpDis;
route[tmpEnd][tmpSt] = tmpDis;
}
dijkstra();
dfs(Sp);
cout << dstSendBikeNum;
reverse(dstPathVec.begin(), dstPathVec.end());
for(int i = ; i < dstPathVec.size(); ++i)
{
if(i == ) printf(" ");
else printf("->");
cout << dstPathVec[i];
}
cout << " " << dstBackBikeNum;
return ;
}
PAT A1018的更多相关文章
- PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- [PAT] A1018 Public Bike Management
[思路] 题目生词 figure n. 数字 v. 认为,认定:计算:是……重要部分 The stations are represented by vertices and the roads co ...
- PAT (Advanced Level) Practice(更新中)
Source: PAT (Advanced Level) Practice Reference: [1]胡凡,曾磊.算法笔记[M].机械工业出版社.2016.7 Outline: 基础数据结构: 线性 ...
- PAT_A1018#Public Bike Management
Source: PAT A1018 Public Bike Management (30 分) Description: There is a public bike service in Hangz ...
- PAT甲级——A1018 Public Bike Management
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- PAT甲级题解分类byZlc
专题一 字符串处理 A1001 Format(20) #include<cstdio> int main () { ]; int a,b,sum; scanf ("%d %d& ...
- 《转载》PAT 习题
博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...
- PAT Judge
原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...
- PAT/字符串处理习题集(二)
B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...
随机推荐
- P1065 单身狗
P1065 单身狗 转跳点:
- xfpt 连接Linux失败问题
首先切换到root用户 1. su 未设置root密码的可以使用一下命令 sudo passwd root 一.上传文件失败(一动不动) 1.安装ftp服务 apt-get install vsftp ...
- liunx mysql 5.7 二进制安装
liunx 5.6版本 本人安装次数不下20次,基本上按照正常的操作流程不会出现什么问题,一切顺利. 今天开发新项目需要按照mysql 5.7 版本.mysql 5.7版本和mysql 5.6版本变化 ...
- 13.在项目中部署redis企业级数据备份方案以及各种踩坑的数据恢复容灾演练
到这里为止,其实还是停留在简单学习知识的程度,学会了redis的持久化的原理和操作,但是在企业中,持久化到底是怎么去用得呢? 企业级的数据备份和各种灾难下的数据恢复,是怎么做得呢? 1.企业级的持久化 ...
- 嵊州普及Day2T2
题意:对于n个数的数列,进行排列,求第m个大于此数列的数列. 思路:查找后2个是否逆序,若是,将后3个递归.如此运算,找后面大于此数中最小数交换,然后将后面数列顺序排列. 相对简单. 见代码: #in ...
- TFIDF介绍
简介 全称: Term Frequency-inverse document frequency(文本频率与逆文档频率指数) 目的: 表征一个token(可以是一个字或者一个词)的重要程度 是Elas ...
- Bookshelf 2 简单DFS
链接:https://ac.nowcoder.com/acm/contest/993/C来源:牛客网 题目描述 Farmer John recently bought another bookshel ...
- WordPress 更新中断故障
WordPress 更新中断故障 WordPress更新中断后显示:Briefly unavailable for scheduled maintenance. Check back in a min ...
- uniapp 小程序实现自定义底部导航栏(tarbar)
在小程序开发中,默认底部导航栏很难满足实际需求,好在官方给出了自定义形式,效果如下: 话不多说,直接上代码 1.组件 custom-tarbar.vue文件 <template> < ...
- 3 —— node —— 文件追加内容
思想 : 先读取 , 再追加 const fs = require('fs') fs.readFile("./hello.txt","utf-8",(err,d ...