A1018 Public Bike Management (30分)

First, the technical summary

  1. The problem, understanding the problem is critical, there are two main points, with a minimum of time spent premise, one is to adjust the way to go, can bring bicycle least priority; if still a number of, then back cycling the least priority. Note Only adjust the station's car on the way to go, back to the time can not be adjusted, imagine if back to the time can be adjusted, then it will not be a second constraint appeared.
  2. Use Djikstra + DFS
  3. While the number of inputs per car station, for subtracting Cmax / 2, for the direct operation to facilitate the subsequent operations. Specific reference code at
  4. For each station more than two properties, one need, one remain, need to record the number of the station premises lacking, that is, the number of cars coming from the need to adjust the management center; remain recorded at the station, extra car , which is shipped back of the car.

Second, the reference code

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 510;
const int INF = 0x3fffffff;
int G[MAXN][MAXN];
int d[MAXN], weight[MAXN], w[MAXN];
vector<int> path, tempPath;
vector<int> pre[MAXN];
bool vis[MAXN] = {false};
int Cmax, n, s, m;
int minNeed = INF, minRemain = INF;
void Djikstra(int s){
    fill(d, d+MAXN, INF);
    d[s] = 0;
    for(int i = 0; i <= n; i++){
        int u = -1, MIN = INF;
        for(int j = 0; j <= n; j++){
            if(vis[j] == false && d[j] < MIN){
                u = j;
                MIN = d[j];
            }
        }
        if(u == -1) return;
        vis[u] = true;
        for(int v = 0; v <= n; v++){
            if(vis[v] == false && G[u][v] != INF){
                if(d[u] + G[u][v] < d[v]){
                    d[v] = d[u] + G[u][v];
                    pre[v].clear();
                    pre[v].push_back(u);
                }else if(d[u] + G[u][v] == d[v]){
                    pre[v].push_back(u);
                }
            }
        }
    }
}
void DFS(int v){
    if(v == 0){
        tempPath.push_back(v);
        int need = 0, remain = 0;
        for(int i = tempPath.size()-1; i >= 0; i--){
            int id = tempPath[i];
            if(weight[id] > 0){
                remain += weight[id];
            }else{
                if(remain > abs(weight[id])){
                    remain -= abs(weight[id]);
                }else{
                    need += abs(weight[id]) - remain;
                    remain = 0;
                }
            }

        }
        if(need < minNeed){//优化,如果所需的更少 
            minNeed = need;
            minRemain = remain;
            path = tempPath; 
        }else if(need == minNeed && remain < minRemain){
            //携带数目相同,带回数目变多
            minRemain = remain;
            path = tempPath; 
        }
        tempPath.pop_back();
        return;
    }
    tempPath.push_back(v);
    for(int i = 0; i < pre[v].size(); i++){
        DFS(pre[v][i]);
    }
    tempPath.pop_back();
}
int main(){
    scanf("%d%d%d%d", &Cmax, &n, &s, &m);
    fill(G[0], G[0]+MAXN*MAXN, INF);
    //weight[0] = 0;
    for(int i = 1; i <= n; i++){
        scanf("%d", &weight[i]);
        weight[i] -= Cmax/2;
    }
    int id1, id2, L;
    for(int i = 0; i < m; i++){
        scanf("%d%d%d", &id1, &id2, &L);
        G[id1][id2] = L;
        G[id2][id1] = L;
    }
    Djikstra(0);
    DFS(s);
    int number = path.size()-1;
    printf("%d ", minNeed);
    for(int i = path.size()-1; i >= 0; i--){
        printf("%d", path[i]);
        if(i > 0) printf("->");
    }
    printf(" %d", minRemain);
    return 0;
}

Guess you like

Origin www.cnblogs.com/tsruixi/p/12403147.html