2019浙江大学考研复试上机题 Ambulance Dispatch (30 分)最直观的解法

思路

最直观的解法就是,建完图之后,每读入一个pick-up spots,就以它为起点,进行一次dijkstra,获得从它到所有点(包括救护车派遣中心)的最短距离、前驱。之后,从每个救护车派遣中心出发,进行dfs,得到从该救护车派遣中心到当前pick-up spots的完整路径,比较路径即可,获得最优路径。
使用优先队列优化的dijkstra,第四个数据点用时200ms。
(睡觉

#include <bits/stdc++.h>

using namespace std;

struct node {
    
    
    int v, dis;
    node () {
    
    }
    node (int _v, int _d) : v(_v), dis(_d) {
    
    }

    bool operator < (const node &b) const {
    
    
        return dis > b.dis;
    }
};

const int maxn = 1020;
const int INF = 1e9;
int d[maxn], G[maxn][maxn];
int acnt[maxn];
vector<int> temp, ans;
vector<node> adj[maxn];
vector<int> pre[maxn];
int optdis, optcnt, optstreet;
int ns, na, m, k, total = 0, st;
bool vis[maxn];

void dijkstra() {
    
    
    fill(d, d + maxn, INF);
    d[st] = 0;
    memset(vis, false, sizeof(vis));

    priority_queue<node> q;
    q.push(node(st, 0));
    while (q.size()) {
    
    
        int u = q.top().v;
        q.pop();
        if (vis[u]) continue;
        vis[u] = true;
        for (auto cur : adj[u]) {
    
    
            int v = cur.v, dis = cur.dis;
            if (!vis[v]) {
    
    
                if (d[v] > d[u] + dis) {
    
    
                    d[v] = d[u] + dis;
                    pre[v].clear();
                    pre[v].push_back(u);
                    q.push(node(v, d[v]));
                } else if (d[v] == d[u] + dis) {
    
    
                    pre[v].push_back(u);
                }
            }
        }
    }
}

int get_dis () {
    
    
    int r = 0;
    for (int i = 1; i < temp.size(); i++) {
    
    
        int u = temp[i-1], v = temp[i];
        r += G[u][v];
    }
    return r;
}

void dfs(int u) {
    
    
    if (u == st) {
    
    
        temp.push_back(u);
        int cur_dis = get_dis();
        if ((cur_dis < optdis) || (cur_dis == optdis && acnt[temp[0]] > optcnt) || (cur_dis == optdis && acnt[temp[0]] == optcnt && temp.size() < optstreet)) {
    
    
            optdis = cur_dis;
            optcnt = acnt[temp[0]];
            optstreet = temp.size();
            ans = temp;
        }
        temp.pop_back();
        return;
    }
    temp.push_back(u);
    for (auto v : pre[u]) {
    
    
        dfs(v);
    }
    temp.pop_back();
}

void show() {
    
    
    for (int i = 0; i < ans.size(); i++) {
    
    
        if (i) printf(" ");
        int u = ans[i];
        if (u > ns) printf("A-%d", u - ns);
        else printf("%d", u);
    }
    printf("\n");
}

int main()
{
    
    
    scanf("%d%d", &ns, &na);
    for (int i = ns + 1; i <= ns + na; i++) {
    
    
        scanf("%d", &acnt[i]);
        total += acnt[i];
    }

    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
    
    
        string s1, s2;
        int dis;
        cin >> s1 >> s2 >> dis;
        
        int u, v;
        if (s1[0] == 'A') u = ns + stoi(s1.substr(2));
        else u = stoi(s1);

        if (s2[0] == 'A') v = ns + stoi(s2.substr(2));
        else v = stoi(s2);

        adj[u].push_back(node(v, dis));
        adj[v].push_back(node(u, dis));
        G[u][v] = G[v][u] = dis;
    }

    scanf("%d" ,&k);
    while (k--) {
    
    
        scanf("%d", &st);
        if (total < 1) printf("All Busy\n");
        else {
    
    
            for (int i = 0; i < maxn; i++) pre[i].clear();
            dijkstra();
            optdis = INF, optcnt = 0, optstreet = INF;
            temp.clear(), ans.clear();

            for (int cur = ns + 1; cur <= ns + na; cur++) {
    
    
                if (acnt[cur] == 0) continue;
                dfs(cur);
            }
            show();
            printf("%d\n", optdis);
            acnt[ans[0]]--;
        }
        total--;
    }

    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_44321570/article/details/123606340