Pagini recente » Cod sursa (job #559407) | Cod sursa (job #1827683) | Cod sursa (job #559365) | Cod sursa (job #1345699) | Cod sursa (job #701138)
Cod sursa(job #701138)
#include <iostream>
#include <fstream>
#include <vector>
#include <queue>
#define INF 999999999
#define COST first
#define DIS first
#define NODE second
#define TO second
using namespace std;
int N, M, K, path[15][10005];
typedef pair<int,int> edge;
vector< vector<edge> > graph(50001);
int map[1<<15][15];
void dijkstra(int source, int *dis)
{
bool vis[10005];
for (int i = 1; i <= N; ++i)
dis[i] = INF, vis[i] = false;
dis[source] = 0;
priority_queue <edge, vector<edge>, greater<edge> > S;
S.push(make_pair(0, source));
while(!S.empty())
{
int d = S.top().DIS;
int x = S.top().NODE;
S.pop();
if(vis[x])
continue;
vis[x] = true;
for(int i = 0; i < graph[x].size(); ++i)
{
if(dis[graph[x][i].TO] > d + graph[x][i].COST)
{
dis[graph[x][i].TO] = d + graph[x][i].COST;
S.push(make_pair(d + graph[x][i].COST, graph[x][i].TO));
}
}
}
}
int main()
{
vector <int> custom_path;
ifstream in("ubuntzei.in");
in >> N >> M >> K;
for(int a, i = 0; i < K; ++i)
{
in >> a;
custom_path.push_back(a);
}
for (int a, b, c; M; --M)
{
in >> a >> b >> c;
graph[a].push_back(make_pair(c,b));
graph[b].push_back(make_pair(c,a));
}
in.close();
int dis[10005];
dijkstra(1, dis);
ofstream out("ubuntzei.out");
if(K == 0)
{
out << dis[N];
return 0;
}
for (int i = 0; i < K; ++i)
dijkstra(custom_path[i], path[i]);
for(int i = 1; i < (1<<K); ++i)
{
int j;
for(j = 0; j < K; ++j)
if (i == (1<<j))
break;
if (j < K && i == (1<<j))
{
map[i][j] = dis[custom_path[j]];
continue;
}
for(j = 0; j < K; ++j)
{
map[i][j] = INF;
if(i & (1<<j))
for(int k = 0; k < K; ++k)
if(k != j && (i & (1<<j)))
map[i][j] = min(map[i][j], map[i-(1<<j)][k] + path[k][custom_path[j]]);
}
}
int smallest = INF;
for (int i = 0; i < K; ++i)
smallest = min(smallest, map[(1<<K)-1][i] + path[i][N]);
out << smallest;
out.close();
return 0;
}