PAT Public Bike Management (dfs)
思路:你的答案必须满足三个条件:
1.在所有路径中选择最短的;
2.如果路径相等,则选择从PBMC中送出最少的;
3.如果路径相等且PBMC送出的车也相等,则选择带回最少的。
注意:这题很恶心,你要考虑–在最后计算发车数和返回车数时,首先要从sp开始反向寻找第一个缺车的站(由于发车是单向的,路径上靠近PBMC的站可以将多出的车交给较远的缺车的站,但较远的站之后不能将多出的车返还给较近的缺车站,其后多出的车最后全部返还给PBMC了,因此发车数要从第一个缺车的站开始往前计算),之后多出的车全部计入返回车辆数
AC代码
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn = 500+5;
int c, m, n, goal;
int G[maxn][maxn], sta[maxn];
int vis[maxn];
int path[maxn], length, tmp[maxn];
struct res{
int length, send, back;
res() {
this->length = inf;
this->send = inf;
this->back = inf;
}
res(int l, int s, int b) {
this->length = l;
this->send = s;
this->back = b;
}
}ans;
void updatePath(int cnt) {
length = cnt;
for(int i = 0; i < cnt; i++) {
path[i] = tmp[i];
}
}
/*
1.在所有路径中选择最短的;
2.如果路径相等,则选择从PBMC中送出最少的;
3.如果路径相等且PBMC送出的车也相等,则选择带回最少的。
*/
res better(res &a, res &b, int cnt) {
bool update = false;
if(a.length > b.length) {
update = true;
} else if(a.length == b.length){
if(a.send > b.send) {
update = true;
} else if(a.send == b.send) {
if(a.back > b.back) {
update = true;
}
}
}
if(update) {
updatePath(cnt);
return b;
} else {
return a;
}
}
void dfs(int u, int len, int cnt, int send, int back) {
if(len > ans.length) return;
if(u == goal) {
res tp = res(len, send, back);
ans = better(ans, tp, cnt);
return;
}
for(int i = 1; i <= n; i++) {
if(!vis[i] && G[u][i] != -1) {
vis[i] = 1;
tmp[cnt] = i;
if(sta[i] >= c/2) {
dfs(i, len+G[u][i], cnt+1, send, back+sta[i]-c/2);
} else {
int x = c/2 - sta[i];
dfs(i, len+G[u][i], cnt+1, x>back?send+x-back:send, x>back?0:back-x);
}
vis[i] = 0;
}
}
}
void init() {
ans.length = inf;
ans.send = inf;
ans.back = inf;
memset(G, -1, sizeof(G));
memset(vis, 0, sizeof(vis));
}
int main() {
while(scanf("%d%d%d%d", &c, &n, &goal, &m) == 4) {
init();
for(int i = 1; i <= n; i++) {
scanf("%d", &sta[i]);
}
int u, v, cost;
for(int i = 0; i < m; i++) {
scanf("%d%d%d", &u, &v, &cost);
G[u][v] = cost;
G[v][u] = cost;
}
vis[0] = 1;
dfs(0, 0, 0, 0, 0);
printf("%d ", ans.send);
printf("0");
for(int i = 0; i < length; i++) {
printf("->%d", path[i]);
}
printf(" %d\n", ans.back);
}
return 0;
}
如有不当之处欢迎指出!
PAT Public Bike Management (dfs)的更多相关文章
- pat Public Bike Management (30)
There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...
- 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 甲级 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 ...
- PAT 1018 Public Bike Management[难]
链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018 Public ...
- PAT甲级1018. Public Bike Management
PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...
- PAT 1018 Public Bike Management(Dijkstra 最短路)
1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- pat 甲级 Public Bike Management
Public Bike Management (30) 题目描述 There is a public bike service in Hangzhou City which provides grea ...
- Pat(Advanced Level)Practice--1018(Public Bike Management)
Pat1018代码 题目描写叙述: There is a public bike service in Hangzhou City which provides great convenience t ...
随机推荐
- Django_xamin注册model错误
可能出现的错误: 1. xadmin.sites.AlreadyRegistered: The model UserProfile is already registered 2. error:Fie ...
- python_py2和py3读写文本区别
python2和python3的区别? python 2 str 对应 python3 bytes python 2 uincode 对应 ...
- Linux实践篇--crontab定时任务
原文出处:http://www.cnblogs.com/tracy/archive/2011/12/27/2303788.html.感谢作者的无私分享 一. Crontab 介绍 ...
- PHP中利用PHPMailer配合QQ邮箱实现发邮件
PHPMailer的介绍: 优点: 可运行在任何平台之上 支持SMTP验证 发送邮时指定多个收件人,抄送地址,暗送地址和回复地址:注:添加抄送.暗送仅win平台下smtp方式支持 支持多种邮件编码包括 ...
- Shader 入门笔记(三) ShaderLab 初识
Unity中,Unity Shader 都是ShaderLab 来编写的.ShaderLab 是Unity提供的编写Unity Shader 的一种说明性语言. 1)Properties :定义了着色 ...
- SQL语句-INSERT语句
Insert语句 Insert语句三种写法: mysql> desc students; +-------+-------------+------+-----+---------+------ ...
- 循环while 、do…while 、for
循环while .do-while .for 循环结构的选择? - 当对一个条件进行一次判断时,用if语句: - 当对一个条件进行多次判断时,用while语句: 1.While 循环条件成立就会执行循 ...
- Python算法——二叉树
一.二叉树 from collections import deque class BiTreeNode: def __init__(self, data): self.data = data sel ...
- ubuntu+mono+PetaPoco+Oracle+.net 程序部署
前言:将windows 下开发的 .net 控制台程序(连接Oracle数据库)部署到 ubuntu 下步骤记录 2017-09-19 实验所用机器为虚拟机Ubuntu16.04 amd64 安装 ...
- Mybatis异常There is no getter for property named 'XXX' in 'class com.xxx.xxx.UserAccountDTO
mybatis报错异常信息如下: 解决: 在接口中加上注解:@Param("userAccountDTO"),如图