1018 Public Bike Management (30 分)

坑:路径后面车站多出来的车不能弥补前面的空缺

#include <bits/stdc++.h>
#define LOCAL
using namespace std;

template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { os << '{'; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << '}'; }
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#ifdef LOCAL
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#else
#define dbg(...)
#endif

#define vec vector
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define all(a) (a).begin(), (a).end()

const int MAX_N = 1e5 + 5;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;

int Cmax, N, Sp, M;
int C[510];
map<int, map<int, int>> ma;
int Min;
int st[510];
vec<int> path;
int send = INF, rec = INF;

int vis[510];
int dist[510];
void dijkstra(int start){
    memset(dist, 0x3f, sizeof dist);
    dist[start] = 0;
    for(int i = 0; i <= N; i ++){
        int k = -1;
        for(int j = 0; j <= N; j ++)
            if(vis[j] == 0 && (k == -1 || dist[k] > dist[j]))
                k = j;
        vis[k] = 1;
        for(auto t : ma[k]){
            dist[t.first] = min(dist[t.first], dist[k] + t.second); // 为什么这里不需要特判vis = 0
        }
    }
}

void dfs(int u, int w, int sd, int rc, vec<int> &p){
    st[u] = 1;
    if(w > Min) return;
    if(u == Sp){
        if(Min == w){
            if(sd < send){
                send = sd;
                rec = rc;
                path = p;
            }else if(sd == send && rc < rec){
                rec = rc;
                path = p;
            }
        }
        return;
    }
    for(auto t : ma[u]){
        if(st[t.first] == 0){
            p.push_back(t.first);
            int val = C[t.first] - Cmax / 2;
            if(val < 0){
                int ssd = sd - val - min(-val, rc);
                int rrc = rc - min(-val, rc);
                dfs(t.first, w + t.second, ssd, rrc, p);
            }else{
                int rrc = rc + val;
                dfs(t.first, w + t.second, sd, rrc, p);
            }
            st[t.first] = 0;
            p.pop_back();
        }
    }
}

void solve() {
    cin >> Cmax >> N >> Sp >> M;
    for(int i = 1; i <= N; i ++){
        cin >> C[i];
    }
    for(int i = 0; i < M; i ++){
        int a, b, w;
        cin >> a >> b >> w;
        if(ma[a].count(b) == 0){
            ma[a][b] = ma[b][a] = w;
        }else{
            ma[a][b] = min(ma[a][b], w);
            ma[b][a] = min(ma[b][a], w);
        }
    }
    dijkstra(0);
    Min = dist[Sp];
    
    // cout << Min << endl;
    
    st[0] = 1;
    vec<int> p{0};
    for(auto t : ma[0]){
        p.push_back(t.first);
        int sd = max(Cmax / 2 - C[t.first], 0);
        int rc = max(C[t.first] - Cmax / 2, 0);
        dfs(t.first, t.second, sd, rc, p);
        p.pop_back();
    }
    cout << send << ' ';
    cout << path[0];
    for(int i = 1; i < path.size(); i ++) cout << "->" << path[i];
    cout << ' ' << rec << endl;
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    int tc = 1;
    // cin >> tc;
    for (int t = 1; t <= tc; t++) {
        // cout << "Case #" << t << ": ";
        solve();
    }
}
上一篇:GBase8d_C_API_09_添加条目_使用异步函数添加条目的例子


下一篇:ubuntu 挂载硬盘