Pagini recente » Cod sursa (job #1835323) | Cod sursa (job #3128221) | Cod sursa (job #1530423) | Cod sursa (job #1258865) | Cod sursa (job #2654263)
#include <fstream>
#include<deque>
#include <bits/stdc++.h>
#include<math.h>
using namespace std;
struct dr{int oras;int cost;};
struct pt_coada{int oras;int ubun;};
int ubuntz[20][655],n,m,k,dorm[2001];
deque<pt_coada>q;
deque<dr>drum[10001];
int main()
{ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");
f>>n>>m>>k;
for(int i=1;i<k+1;i++)
{
int yy;
f>>yy;
dorm[yy]=i;
}
for(int i=1;i<=m;i++)
{int x,y,cost;
f>>x>>y>>cost;
dr pus;pus.oras=y;pus.cost=cost;
drum[x].push_front(pus);
pus.oras=x;
drum[y].push_front(pus);
}
int yyy=pow(2,k)-1;
ubuntz[1][0]=-1;
pt_coada nou;nou.oras=1;nou.ubun=0;
q.push_front(nou);
while(!q.empty())
{
pt_coada scos=q.front();q.pop_front();
int oras=scos.oras;
int omuleti=scos.ubun;
for(int i=1;i<=drum[oras].size();i++)
{
dr nou_chestie=drum[oras].front();
drum[oras].push_back(drum[oras].front());
drum[oras].pop_front();
int nou_oras=nou_chestie.oras;
int nou_cost=nou_chestie.cost;
if(dorm[nou_oras]!=0)
{int yui=(omuleti)+(1<<(dorm[nou_oras]-1));
if(((omuleti&(1<<(dorm[nou_oras]-1)))==0)&&((ubuntz[oras][omuleti]+nou_cost+(ubuntz[oras][omuleti]==-1?1:0)<ubuntz[nou_oras][yui])||ubuntz[nou_oras][yui]==0))
{
ubuntz[nou_oras][yui]=ubuntz[oras][omuleti]+nou_cost+(ubuntz[oras][omuleti]==-1?1:0);
q.push_back({nou_oras,yui});
}
else
if((ubuntz[oras][omuleti]+nou_cost+(ubuntz[oras][omuleti]==-1?1:0)<ubuntz[nou_oras][omuleti])||ubuntz[nou_oras][omuleti]==0)
{
ubuntz[nou_oras][omuleti]=ubuntz[oras][omuleti]+nou_cost+(ubuntz[oras][omuleti]==-1?1:0);
q.push_back({nou_oras,omuleti});
}
}
else
{
if((ubuntz[oras][omuleti]+nou_cost+(ubuntz[oras][omuleti]==-1?1:0)<ubuntz[nou_oras][omuleti])||ubuntz[nou_oras][omuleti]==0)
{
ubuntz[nou_oras][omuleti]=ubuntz[oras][omuleti]+nou_cost+(ubuntz[oras][omuleti]==-1?1:0);
q.push_back({nou_oras,omuleti});
}
}
}
}
int uu=1;
for(int i=1;i<=k;i++)
uu*=2;
uu--;
g<<ubuntz[n][uu];
return 0;
}