Pagini recente » Cod sursa (job #3197945) | Cod sursa (job #2236685) | Diferente pentru arhiva-educationala intre reviziile 14 si 13 | Cod sursa (job #1333604) | Cod sursa (job #2059428)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
#define NMAX 2005
#define KMAX 20
#define NSTARI (1<<16)+5
#define INF (1<<30)
ifstream fin("ubuntzei.in");
ofstream fout("ubuntzei.out");
struct muchie{
int vf , cost;
};
class comp{
public: bool operator()(muchie &x, muchie &y){
return x.cost>y.cost;
};
};
vector <muchie> g[NMAX];
priority_queue <muchie,vector<muchie>, comp> q;
int n, m, k, u[KMAX], ub[KMAX][KMAX],d[NMAX], dp[NSTARI][KMAX];
void citire()
{
int x, y, c;
fin >> n >> m >> k;
for(int i = 1; i <= k; i++)
fin >> u[i];
u[0] = 1;
u[++k] = n;
for(int i = 1; i <= m; i++){
fin >> x >> y >> c;
g[x].push_back({y,c});
g[y].push_back({x,c});
}
}
void dijkstra(int s)
{
muchie z, z1;
for(int i = 1; i <= n; i++) d[i] = INF;
d[s] = 0;
q.push({s,0});
while(!q.empty()){
z = q.top();
q.pop();
if(d[z.vf]!=z.cost) continue;
for(unsigned int i = 0; i < g[z.vf].size(); i++)
{
z1 = g[z.vf][i];
if(d[z1.vf] > d[z.vf]+z1.cost){
d[z1.vf] = d[z.vf]+z1.cost;
q.push({z1.vf,d[z1.vf]});
}
}
}
}
int main()
{
citire();
//distanta intre ubuntzei
//u[0]=Cluj, u[k]=Constanta, u[1],u[k-1] ceilalti ubuntzei
for (int i = 0; i <= k ;i++){
dijkstra(u[i]); //calculez distantele minime de la u[i] la ceilalti ubuntzei
for(int j = 0; j <= k; j++)
ub[i][j] = d[u[j]];
}
int nst = 1<<(k+1); //nr de stari
for(int i = 0; i < nst; i++)
for(int j = 0; j <= k; j++)
dp[i][j] = INF;
dp[1][0] = 0;
for(int i = 1; i < nst; i++){
for(int bit = 0; bit <= k; bit++)
if((i & (1<<bit)) != 0) //in starea i bitul bit este 1
for(int bit1 = 0; bit1 <= k; bit1++)
if((i & (1<<bit1))!=0 && bit1 != bit)// ama ajuns in starea i in ubuntzelul bit,
// dintr-o stare identica cu i, dar cu bitul bit sters pornind din ubuntzelul bit1
dp[i][bit] = min(dp[i][bit], dp[i^(1<<bit)][bit1]+ub[bit1][bit]);
}
fout<<dp[nst-1][k];
return 0;
}