Cod sursa(job #2713726)

Utilizator Arsene_DenisaArsene Denisa Arsene_Denisa Data 28 februarie 2021 15:25:00
Problema Ubuntzei Scor 5
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.48 kb
#include <iostream>
#include <fstream>
#include <vector>
#include<queue>

using namespace std;

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

const int inf=2e9;

int dp[262144][18];
int dist[2001][2001];
int l[18];

vector<pair<int, int> > v[18];

int main() {
    int n, m, i, j, x, y, c, k;

    fin>>n>>m;
    fin>>k;
    l[0]=1;
    l[k+1]=n;
    for(i=1;i<=k;i++) {
        fin>>l[i];
    }
    for(i=1; i<=m; i++) {
        fin>>x>>y>>c;
        v[y].push_back({x, c});
        v[x].push_back({y, c});
    }
    for(i=0;i<=k+2;i++) {
        for(j=0;j<=n+1;j++) {
                dist[i][j]=inf;
        }
    }
    for(i=0;i<=1<<(k+2)+5;i++) {
        for(j=0;j<k+2;j++) {
            dp[i][j]=inf;
        }
    }
    priority_queue<pair<int, int> >h;
    for(i=0;i<=k+1;i++) {
        dist[i][l[i]]=0;
        h.push({l[i], 0});
        while(!h.empty()) {
            int nod=h.top().first;
               h.pop();
            for(int j=0;j<v[nod].size();j++) {
                int newn=v[nod][j].first;
                int cost=v[nod][j].second;
                if(cost+dist[i][nod]<dist[i][newn]) {
                    dist[i][newn]=cost+dist[i][nod];
                    h.push({newn, -dist[i][newn]});
                }
            }
        }
        for(int po=1;po<=n;po++) {
            cout<<dist[i][po]<<" ";
        }
    }

    dp[1][0] = 0;
    int mask;
    for(mask=0; mask<(1<<k+2); mask++) {
        for(i=0; i<=k+1; i++) {
            if(mask&(1<<i)==0) continue;
                for(int t=0;t<=k+1;t++) {
                    if(mask&(1<<t)==0) continue;

                    dp[mask|(1<<t)][t]=min(dp[mask|(1<<t)][t], dp[mask][i]+dist[i][l[t]]);
                }
            }
    }
    fout<<dp[(1<<(k+2))-1][k+1];


    return 0;
}







/*

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

int n, m, k, c[2048];
vector<pair<int, int> > g[2048];

ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");

int main() {
    fin >> n >> m >> k;
    c[0] = 1, c[k+1] = n;
    for(int i = 1; i <= k; i++)
        fin >> c[i];

    while(m--) {
        int u, v, c;
        fin >> u >> v >> c;
        g[u].push_back({v, c});
        g[v].push_back({u, c});
    }

    vector<vector<int> >dist(k+2, vector<int> (n+1, 2e9)); // dist[i][j] = distanta minima dintre c[i] si j
    for(int i = 0; i <= k+1; i++) {
        dist[i][c[i]] = 0;
        priority_queue<pair<int, int> > pq;
        pq.push({0, c[i]});
        while(!pq.empty()) {
            int cur = pq.top().second;
            pq.pop();

            for(auto next: g[cur])
                if(dist[i][next.first] > dist[i][cur]+next.second) {
                    dist[i][next.first] = dist[i][cur]+next.second;
                    pq.push({-dist[i][next.first], next.first});
                }
        }
        for(int po=1;po<=n;po++) {
            cout<<dist[i][po]<<" ";
        }
    }

    vector<vector<int> > dp((1<<(k+2))+5, vector<int> (k+2, 2e9));
    dp[1][0] = 0;
    for(int i = 0; i < (1<<(k+2)); i++)
        for(int j = 0; j <= k+1; j++) {
            if((i&(1<<j)) == 0) continue;
            for(int next = 0; next <= k+1; next++) {
                if(i&(1<<next)) continue;
                dp[i|(1<<next)][next] = min(dp[i|(1<<next)][next], dp[i][j]+dist[j][c[next]]);
                //cout << i << ' ' << j << ' ' << next << '\n';
            }
        }
    fout << dp[(1<<(k+2))-1][k+1];
}
*/