Cod sursa(job #2340408)

Utilizator vladcoroian2001Vlad Coroian vladcoroian2001 Data 10 februarie 2019 13:45:39
Problema Ubuntzei Scor 50
Compilator cpp-64 Status done
Runda Arhiva de probleme Marime 1.31 kb
#include <fstream>
#include <queue>
#include <vector>
#include <bitset>

using namespace std;
ifstream fi("ubuntzei.in");
ofstream fo("ubuntzei.out");
const int NMAX=2005,INF=1e9;
struct oras{
	int cost,conf,loc;
	bool operator < (const oras &other) const
	{
		return cost > other.cost; 
	}
};
int n,m,k,p[NMAX],bst[NMAX][(1<<15)+5],x,y,cost;
bitset <(1<<15)+5> VIZ[NMAX];
priority_queue <oras> pq;
vector <pair<int,int>> V[NMAX];
void dijkstra()
{
	for(int i=1;i<=n;i++)
		for(int conf=0;conf<(1<<k);conf++)
			bst[i][conf]=INF;
	pq.push({0,0,1});
	bst[1][0]=0;
	while(!pq.empty())
	{
		oras o=pq.top();
		pq.pop();
		int x=o.loc;
		int conf=o.conf;
		if(VIZ[x][conf]) continue;
		VIZ[x][conf]=1; 
		for(auto edge: V[x])
		{
			y=edge.first;
			cost=edge.second;
			int conf2;
			if(p[y] && !(conf&(1<<(p[y]-1)))) conf2=conf+(1<<(p[y]-1));
			else conf2=conf;
			if(bst[y][conf2]>bst[x][conf]+cost)
			{
				bst[y][conf2]=bst[x][conf]+cost;
				VIZ[y][conf2]=0;
				pq.push({bst[y][conf2],conf2,y});
			}
		}
	}
}
int main()
{
	fi>>n>>m>>k;
	for(int i=1;i<=k;i++)
	{
		fi>>x;
		p[x]=i;
	}
	for(int i=1;i<=m;i++)
	{
		fi>>x>>y>>cost;
		V[x].push_back({y,cost});
		V[y].push_back({x,cost});
	}
	dijkstra();
	fo<<bst[n][(1<<k)-1]<<"\n";
	fi.close();
	fo.close();
	return 0;
}