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

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. PHP实现QQ登录的开发教程

    第三方登录,就是使用大家比较熟悉的比如QQ.微信.微博等第三方软件登录自己的网站,这可以免去注册账号.快速留住用户的目的,免去了相对复杂的注册流程.下边就给大家讲一下怎么使用PHP开发QQ登录的功能. ...

  2. 一个ios的各种组件、代码分类,供参考

    http://github.ibireme.com/github/list/ios/#

  3. awk使用正则精确匹配

    [root@localhost home]# cat file 5001][YRSD5-1][YRSD5-1-2][0203008400028411] 010102 5001][YRSD7-1][YR ...

  4. spring之集合注入

    list: <bean id="userAction" class="com.xx.action.UserAction"> <property ...

  5. the first simple html page generated by div and table tags

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w ...

  6. HTTP/HTTPS GET&POST两种方式的实现方法

    关于GET及POST方式的区别请参照前面文章:http://www.cnblogs.com/hunterCecil/p/5698604.html http://www.cnblogs.com/hunt ...

  7. 玩转webpack(二):webpack的核心对象

    欢迎大家前往云+社区,获取更多腾讯海量技术实践干货哦~ 作者介绍:陈柏信,腾讯前端开发,目前主要负责手Q游戏中心业务开发,以及项目相关的技术升级.架构优化等工作. 前言 webpack 是一个强大的模 ...

  8. iOS-NSPredicate正则验证【三种验证方法】

    1.NSPredicate验证(谓词匹配) ///验证(string:验证的字符串) + (BOOL)stringValidate:(NSString *)string{ NSString *regu ...

  9. python中的列表排序

    对列表进行排序是常见的操作,最简单的方式是使用sort()函数. 1.一般用法 不管列表元素是数.字符串还是元组,函数sort()总是就地操作列表,按升序进行排列元素,并返回None. #数 > ...

  10. 2017年总结的前端文章——CSS高级技巧汇总

    1. 页面顶部阴影 body:before{ content: ""; position: fixed; top:-10px; left:; width: 100%; height ...