Pagini recente » Cod sursa (job #598597) | Cod sursa (job #727272) | Cod sursa (job #1865886) | Cod sursa (job #2301810) | Cod sursa (job #822209)
Cod sursa(job #822209)
#include <fstream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <queue>
#define INF 2000000000
using namespace std;
int n, m, k;
int d[2012], dist[15][2012];
int best[15][(1<<15)];
// best[i][j] = costul minim de a ajunge in orasul i
// si a vizita toate orasele din multimea j
int oras[20];
struct NOD
{
int x, cost;
};
vector <NOD> a[2012];
inline void Read()
{
ifstream f("ubuntzei.in");
f>>n>>m>>k;
int i;
for (i=0; i<k; i++)
f>>oras[i];
NOD aux;
int x, y, cost;
for (i=0; i<m; i++)
{
f>>x>>y>>cost;
aux.x = y;
aux.cost = cost;
a[x].push_back(aux);
aux.x = x;
a[y].push_back(aux);
}
f.close();
}
inline void BellmanFord(int nod, int v[])
{
queue<int> Q;
int i;
vector<NOD>::iterator it, final;
NOD aux;
for (i=1; i<=n; i++)
v[i] = INF;
v[nod] = 0;
Q.push(nod);
int x, cost, y;
while(!Q.empty())
{
x = Q.front();
Q.pop();
it = a[x].begin();
final = a[x].end();
for(; it!=final; it++)
{
aux = *it;
y = aux.x;
cost = aux.cost;
if (v[y] > v[x] + cost)
{
v[y] = v[x] + cost;
Q.push(y);
}
}
}
}
inline void Solve()
{
BellmanFord(1, d);
if (k==0)
{
ofstream g("ubuntzei.out");
g<<d[n]<<"\n";
g.close();
return ;
}
int i, j;
for(i=0; i<k; i++)
BellmanFord(oras[i], dist[i]);
int nrmul = (1<<k) - 1, mul;
bool ok;
for (mul = 1; mul <= nrmul; mul++)
{
ok = false;
for (i=0; i<k && !ok; i++)
{
if (mul == (1<<i))
{
ok = true;
best[i][mul] = d[oras[i]];
}
}
if (!ok)
{
// trebuie sa ajung in orasul i vizitand multimea mul
for (i=0; i<k; i++)
{
best[i][mul] = INF;
if ((mul&(1<<i)) != 0) // daca am orasul i in multime
{
for (j=0; j<k; j++)
{
if (j!=i && (mul&(i<<j))!=0) // ajung in orasul i din orasul j vizitand multimea mul
{
best[i][mul] = min (best[i][mul], best[j][mul - (1<<i)] + dist[j][oras[i]]);
}
}
}
}
}
}
int sol;
sol = INF;
for(i=0; i<k; i++)
{
sol = min(sol, best[i][nrmul] + dist[i][n]);
}
ofstream g("ubuntzei.out");
g<<sol<<"\n";
g.close();
}
int main()
{
Read();
Solve();
return 0;
}