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 ...
随机推荐
- pat 1022 digital library
#include <iostream> #include <sstream> #include <string> #include <vector> # ...
- 解决C#编译中"csc不是内部或外部命令"的问题
安装完 VisualStudio 编译环境后,是不能用命令行直接编译写好的csc文件的,如果不配置环境变量,在命令提示符(cmd)中编译扩展名为cs的文件,会出现错误提示"csc不是内部或外 ...
- Linux中变量#,@,0,1,2,*,$$,$?的意思
$# 是传给脚本的参数个数 $0 是脚本本身的名字 $1 是传递给该shell脚本的第一个参数 $2 是传递给该shell脚本的第二个参数 $@ 是传给脚本的所有参数的列表 $* 是以一个单字符串显示 ...
- _beginthread和CreatThread的区别
转自:http://www.jb51.net/article/41459.htm 我们知道在Windows下创建一个线程的方法有两种,一种就是调用Windows API CreateThread()来 ...
- SQL Server中计算表达式的和
项目使用的是SQL Server数据库,需要做一个审核规则,字段A中表达式的值和字段B中的值,做比较: 需求本身很简单,但是表达式中存在很多非法字符(非法全角,运算符,汉字--) eg:1.1.1*2 ...
- CSS学习笔记(一):定位与溢出
一.定位:positionstatic | relative | absolute | fixed | inherit ,各值含义如下: 1)static:元素框正常生成,块级元素生成一个矩形框,作为 ...
- 10_set集合
一.集合类型 集合是一组无序排列的可哈希的值(可哈希的值->不可变),集合成员可以做字典中的键.但集合本身是不可哈希的. 集合是无序没有索引,也没有像字典的key,所以集合不能更改元素.只能增删 ...
- DAY11-Java中的类--接上篇
一.用户自定义类 1.写先出一个简单的Employee类作为例子说明. 代码如下: import java.time.LocalDate; /** * 自定义方法练习--测试 这个程序中包含了两个类E ...
- 2017年总结的前端文章——一劳永逸的搞定 flex 布局
flex 基本概念 使用 flex 布局首先要设置父容器 display: flex,然后再设置 justify-content: center 实现水平居中,最后设置 align-items: ce ...
- SEO页面优化以及如何对单页面应用进行SEO优化
一.简介 1.何为SEO? SEO(search engine optimization),翻译为搜索引擎优化,是利用搜索引擎的搜索规则来提高在相关搜索引擎的排名以及访问量的方式. 2.目的 为了获取 ...