Cod sursa(job #2129509)

Utilizator Horia14Horia Banciu Horia14 Data 12 februarie 2018 21:13:25
Problema Ubuntzei Scor 45
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.87 kb
#include<cstdio>
#include<vector>
#define MAX_N 2000
#define MAX_K 15
#define oo 0x3f3f3f3f
using namespace std;

vector<pair<int,int>>g[MAX_N + 1];
int dp[MAX_N + 1][MAX_N + 1], dist[MAX_N + 1], v[MAX_K + 1], n, m, k, heapSize;
int h[MAX_N + 1], pos[MAX_N + 1], st[MAX_K + 1], minCost;
bool used[MAX_N + 1];
FILE *fout = fopen("ubuntzei.out","w");

void readGraph() {
    int i, x, y, cost;
    FILE *fin = fopen("ubuntzei.in","r");
    fscanf(fin,"%d%d%d",&n,&m,&k);
    for(i = 0; i < k; i++)
        fscanf(fin,"%d",&v[i]);
    for(i = 0; i < m; i++) {
        fscanf(fin,"%d%d%d",&x,&y,&cost);
        g[x].push_back(make_pair(y,cost));
        g[y].push_back(make_pair(x,cost));
    }
    fclose(fin);
}

inline void Swap(int i, int j) {
    int aux = h[i];
    h[i] = h[j];
    h[j] = aux;
    aux = pos[h[i]];
    pos[h[i]] = pos[h[j]];
    pos[h[j]] = aux;
}

inline void heapDown(int i) {
    int dleftSon, drightSon;
    if(2 * i > heapSize) return;
    dleftSon = dist[h[2*i]];
    if(2 * i + 1 <= heapSize)
        drightSon = dist[h[2*i+1]];
    else drightSon = dleftSon + 1;
    if(dleftSon < drightSon) {
        if(dist[h[i]] <= dleftSon) return;
        Swap(i,2*i);
        heapDown(2*i);
    } else {
        if(dist[h[i]] <= drightSon) return;
        Swap(i,2*i+1);
        heapDown(2*i+1);
    }
}

inline void heapUp(int i) {
    if(dist[h[i/2]] <= dist[h[i]]) return;
    Swap(i,i/2);
    heapUp(i/2);
}

void Dijkstra(int source) {
    vector<pair<int,int>>::iterator it;
    int i, node;
    for(i = 1; i <= n; i++) {
        dist[i] = oo;
        h[i] = pos[i] = i;
    }
    dist[source] = 0;
    heapSize = n;
    for(i = 1; i <= n; i++) {
        node = h[1];
        Swap(1,heapSize);
        heapSize--;
        heapDown(1);
        for(it = g[node].begin(); it != g[node].end(); it++) {
            if(dist[(*it).first] > dist[node] + (*it).second) {
                dist[(*it).first] = dist[node] + (*it).second;
                heapUp(pos[(*it).first]);
            }
        }
    }
    for(i = 1; i <= n; i++)
        dp[source][i] = dp[i][source] = dist[i];

}

void precalcDistances() {
    Dijkstra(1);
    Dijkstra(n);
    for(int i = 0; i < k; i++)
        Dijkstra(v[i]);
    minCost = oo;
}

void Back(int K) {
    if(K == k) {
        int cost = 0;
        for(int i = 0; i < K - 1; i++)
            cost += dp[v[st[i]]][v[st[i+1]]];
        cost += dp[1][v[st[0]]] + dp[v[st[k-1]]][n];
        if(cost < minCost)
            minCost = cost;
    } else {
        for(int i = 0; i < k; i++) {
            if(!used[v[i]]) {
                st[K] = i;
                used[v[i]] = true;
                Back(K+1);
                used[v[i]] = false;
            }
        }
    }
}

int main() {
    readGraph();
    precalcDistances();
    if(k == 0)
        fprintf(fout,"%d\n",dp[1][n]);
    else {
        Back(0);
        fprintf(fout,"%d\n",minCost);
    }
    fclose(fout);
    return 0;
}