思路:你的答案必须满足三个条件:

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)的更多相关文章

  1. pat Public Bike Management (30)

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  2. PAT A1018 Public Bike Management (30 分)——最小路径,溯源,二标尺,DFS

    There is a public bike service in Hangzhou City which provides great convenience to the tourists fro ...

  3. 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 ...

  4. 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 ...

  5. PAT 1018 Public Bike Management[难]

    链接:https://www.nowcoder.com/questionTerminal/4b20ed271e864f06ab77a984e71c090f来源:牛客网PAT 1018  Public ...

  6. PAT甲级1018. Public Bike Management

    PAT甲级1018. Public Bike Management 题意: 杭州市有公共自行车服务,为世界各地的游客提供了极大的便利.人们可以在任何一个车站租一辆自行车,并将其送回城市的任何其他车站. ...

  7. PAT 1018 Public Bike Management(Dijkstra 最短路)

    1018. Public Bike Management (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...

  8. pat 甲级 Public Bike Management

    Public Bike Management (30) 题目描述 There is a public bike service in Hangzhou City which provides grea ...

  9. Pat(Advanced Level)Practice--1018(Public Bike Management)

    Pat1018代码 题目描写叙述: There is a public bike service in Hangzhou City which provides great convenience t ...

随机推荐

  1. kindeditor使用

    下载地址http://kindeditor.net/down.php @官方文档 使用步骤: 引入js <script charset="utf-8" src="r ...

  2. <<Senium2自动化测试>>读书笔记一

    为进一步加强Python知识扩展和学习,在朋友的推荐下选择了<<Selenium2自动化测试实战>>,作者胡志恒,基于Python语言实现,以实例的方式详细讲解WebDrive ...

  3. android新建项目

    MinMum Required SDK :最低支持的Android api的版本,你的应用不能在低于这个版本的手机上面运行 Target SDK:你的应用最高支持android api版本 Compi ...

  4. zabbix邮件发送3.2.4

    使用邮件服务发送,一般linux有两种发送方式 mail跟sendmail两款软件,我试验的时候总是会发生发送不了邮件的问题 简而便之,我两款软件都安装了,只要安装其中之一就ok了 #yum -y i ...

  5. 你可能不知道的.Net Core Configuration

    目录 执行原理 环境变量 Spring Cloud Config Server 挂卷Volume Config Server vs Volume 执行原理 1. 配置读取顺序:与代码先后顺序一致. p ...

  6. Web安全之CSP

    内容安全策略(Content-Security-Policy,简称CSP) 概念: 内容安全策略(CSP)是一种web应用技术用于帮助缓解大部分类型的内容注入攻击,包括XSS攻击和数据注入等,这些攻击 ...

  7. Tensorflow ActiveFunction激活函数解析

    Active Function 激活函数 原创文章,请勿转载哦~!! 觉得有用的话,欢迎一起讨论相互学习~Follow Me Tensorflow提供了多种激活函数,在CNN中,人们主要是用tf.nn ...

  8. Entity Framework VS Mybatis 不同点剖析

    大家都知道Entity Framework是.NET系统当中的一个重量级的ORM框架 ,它采用了延迟加载的技术,使得服务端不用每次都去尝试连接数据库,从而增加了使用效率和 减少了不必要的开销.而myb ...

  9. 2017年总结的前端文章——一劳永逸的搞定 flex 布局

    flex 基本概念 使用 flex 布局首先要设置父容器 display: flex,然后再设置 justify-content: center 实现水平居中,最后设置 align-items: ce ...

  10. BZOJ 1969: [Ahoi2005]LANE 航线规划 [树链剖分 时间倒流]

    题意: 一张图,删除边,求两点之间的割边数量.保证任意时刻图连通 任求一棵生成树,只有树边可能是割边 时间倒流,加入一条边,就是两点路径上的边都不可能是割边,区间覆盖... 然后本题需要把边哈希一下, ...