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[+ ...
随机推荐
- pytorch max和clamp
torch.max() torch.max(a):数组a的最大值 torch.max(a, dim=1):多维数组沿维度1方向上的最大值,若a为二维数组,则为每行的最大值(此时是对每行的每列值比较取最 ...
- mongodb - 关联字段
1,博客表结构 Blog.js var mongoose = require('mongoose') mongoose.connect('mongodb://localhost/test',{ us ...
- JDK8中的HashMap实现原理及源码分析
大纲 一.什么是Hash?什么是HashMap? 二.HashMap的内部实现机制 1.HashMap基本元素 ①DEFAULT_INITIAL_CAPACITY&MAXIMUM_CAPACI ...
- 010.Oracle数据库 , ORDER BY 按升序降序排序
/*Oracle数据库查询日期在两者之间*/ SELECT DISTINCT ATA FROM LM_FAULT WHERE ( OCCUR_DATE BETWEEN to_date( '2017-0 ...
- 《新标准C++程序设计》4.7-4.9(C++学习笔记17)
一.重载类型强制转换运算符 在C++中,类型的名字(包括类的名字)本身也是一种运算符,即类型强制转换运算符.类型强制转换运算符是单目运算符,也可以被重载,但只能重载为成员函数,不能重载为全局函数.经过 ...
- 7.6 CLI 管理Varnish
./varnishd -f /usr/common/varnish/etc/varnish/ -a 测试: 代理tomcat服务器地址:http://172.20.10.5:1111/ telnet ...
- navicat报错2005 - Unknown MySQL server host 'localhost' (0) 原因及解决方法
报错原因:没有连接互联网,用navicat连接本地mysql数据库,连接属性ip为localhost. 解决办法:将ip改为127.0.0.1即可.localhost是需要DNS解析后才会是127.0 ...
- Yarn的资源调优
一.概述 每个job提交到yarn上执行时,都会分配Container容器去运行,而这个容器需要资源才能运行,这个资源就是Cpu和内存. 1.CPU资源调度 目前的CPU被Yarn划分为虚拟CPU,这 ...
- edge浏览器两个标签页localStorage不同步,解决办法
今天遇到个奇怪的问题,edge两个标签页之间的localStorage值不同步,网上说ie和edge如果想让localStorage值同步,需要主动出发localStorage的change事件 wi ...
- python爬虫笔记01
1.urllib库中request,parse的学习 1.1 简单的请求页面获取,并下载到本地 request的使用 from urllib import request # 获取此网页的demout ...