Cod sursa(job #2999380)

Utilizator AndreiBOTOBotocan Andrei AndreiBOTO Data 10 martie 2023 22:07:21
Problema Ubuntzei Scor 100
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 3.08 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <queue>

///#include <tryhardmode>
///#include <GODMODE::ON>

using namespace std;

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

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

#define int long long

const int INF=2e9;
const int NMAX=2e3+5;
const int MASK=20;

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

bool viz[NMAX];
int dist[NMAX];
int dp2[NMAX][NMAX];
int dp[(1<<MASK)][MASK];
int c[NMAX];

void dijkstra(int p)
{
    priority_queue<pair<int,int>>pq;
    pq.push(make_pair(0,p));
    dist[p]=0;
    viz[p]=true;
    while(!pq.empty())
    {
        pair<int,int>p=pq.top();
        viz[p.second]=false;
        pq.pop();
        for(auto i:v[p.second])
        {
            if(dist[i.first]>dist[p.second]+i.second)
            {
                dist[i.first]=dist[p.second]+i.second;
                if(!viz[i.first])
                {
                    pq.push(make_pair(-dist[i.first],i.first));
                    viz[i.first]=true;
                }
            }
        }
    }
}

signed main()
{
    int n,m,i,j,k,mask;
    fin>>n>>m>>k;
    for(i=1;i<=k;i++)
        fin>>c[i];
    c[0]=1;
    c[k+1]=n;
    for(i=1;i<=m;i++)
    {
        int x,y,cost;
        fin>>x>>y>>cost;
        v[x].push_back(make_pair(y,cost));
        v[y].push_back(make_pair(x,cost));
    }
    for(i=0;i<=k+1;i++)
    {
        for(j=1;j<=n;j++)
            dist[j]=INF;
        dijkstra(c[i]);
        for(j=0;j<=k+1;j++)
        {
            dp2[i][j]=dist[c[j]];
            dp2[j][i]=dist[c[j]];
        }
        dp2[i][i]=INF;
    }
    for(i=0;i<(1<<(k+2));i++)
        for(j=0;j<=k+1;j++)
            dp[i][j]=INF;
    for(i=0;i<=k+1;i++)
        dp[0][i]=0;
    dp[1][0]=0;
    for(mask=1;mask<(1<<(k+2));mask++)
    {
        for(i=0;i<=k+1;i++)
        {
            if(mask & (1<<i))
            {
                for(j=0;j<=k+1;j++)
                {
                    if(dp[mask][i]>dp[mask^(1<<i)][j]+dp2[i][j])
                        dp[mask][i]=dp[mask^(1<<i)][j]+dp2[i][j];
                }
            }
        }
    }
    fout<<dp[(1<<(k+2))-1][k+1];
    return 0;
}