Pagini recente » Cod sursa (job #2144410) | Cod sursa (job #134193) | Cod sursa (job #3164729) | Cod sursa (job #915851) | Cod sursa (job #2844723)
#include <bits/stdc++.h>
using namespace std;
ifstream f ("ubuntzei.in");
ofstream g ("ubuntzei.out");
int n,m;
int k;
int dist[25][25];
int prieteni[25];
int gasit[100005];
int dp[18][300005];
struct muchie
{
int fiu;
int cost;
};
vector <muchie> v[100005];
struct el
{
int nod;
int cost;
int stare;
bool operator < (const el & A) const
{
return cost>A.cost;
}
};
priority_queue <el> Q;
int pret[2005];
void Dijkstra(int inceput ,int catelea)
{
pret[inceput]=0;
Q.push({inceput , 0 , 0});
while(!Q.empty())
{
int cur=Q.top().nod;
int ccost=Q.top().cost;
///cout<<inceput<<" "<<cur<<" "<<ccost<<"\n";
if(gasit[cur]!=0)
{
dist[catelea][gasit[cur]]=min(ccost , dist[catelea][gasit[cur]]);
dist[gasit[cur]][catelea]=min(ccost , dist[gasit[cur]][catelea]);
}
for(int x=0;x<v[cur].size();++x)
{
int fiu=v[cur][x].fiu;
if(pret[fiu]>pret[cur]+v[cur][x].cost)
{
pret[fiu]=pret[cur]+v[cur][x].cost;
el A;
A.nod=v[cur][x].fiu;
A.cost=pret[fiu];
Q.push(A);
}
}
Q.pop();
}
}
int Ciclu()
{
while(!Q.empty())
Q.pop();
Q.push({1 , 0 , 1});
dp[1][1]=0;
while(!Q.empty())
{
int nod=Q.top().nod;
int cost=Q.top().cost;
int stare=Q.top().stare;
cout<<nod<<" "<<cost<<" "<<stare<<"\n";
if(prieteni[nod]==n && stare==(1<<k)-1)
return cost;
for(int i=1;i<=k;++i)
{
int urm=prieteni[i];
if(urm!=nod && (stare&(1<<(i-1)))==0)
{
if(dp[i][stare+(1<<(i-1))]>dp[nod][stare]+dist[gasit[urm]][nod])
{
dp[i][stare+(1<<(i-1))]=dp[nod][stare]+dist[gasit[urm]][nod];
Q.push({i , dp[i][stare+(1<<(i-1))] , stare+(1<<(i-1))});
}
}
}
Q.pop();
}
}
int main()
{
f>>n>>m;
f>>k;
prieteni[1]=1;
gasit[1]=1;
for(int i=2;i<=k+1;++i)
f>>prieteni[i] , gasit[prieteni[i]]=i;
gasit[n]=k+2;
prieteni[k+2]=n;
k+=2;
for(int i=1;i<=m;++i)
{
int x , y , co;
f>>x>>y>>co;
muchie A;
A.fiu=y;
A.cost=co;
v[x].push_back(A);
A.fiu=x;
v[y].push_back(A);
}
for(int i=1;i<=k;++i)
{
for(int j=1;j<=k;++j)
dist[i][j]=INT_MAX;
}
for(int i=1;i<=k;++i)
{
for(int j=1;j<=n;++j)
pret[j]=INT_MAX;
Dijkstra(prieteni[i] , i);
}
for(int i=1;i<=k;++i)
for(int st=1;st<=(1<<k)-1;++st)
dp[i][st]=INT_MAX;
g<<Ciclu();
return 0;
}