ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

1018 Public Bike Management (30 分)

2022-02-05 10:33:11  阅读:185  来源: 互联网

标签:Management ma int Bike 1018 rc dist sd first


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

#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();
    }
}

标签:Management,ma,int,Bike,1018,rc,dist,sd,first
来源: https://www.cnblogs.com/tomori/p/15863914.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有