Cod sursa(job #701126)

Utilizator psycho21rAbabab psycho21r Data 1 martie 2012 13:48:02
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.41 kb
#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);
    if(K == 0)
    {
        cout << 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]]);
            cout << map[i][j] << " ";
        }
        cout << "\n";
    }
    int smallest = INF;
	for (int i = 0; i < K; ++i)
		smallest = min(smallest, map[(1<<K)-1][i] + path[i][N]);
    ofstream out("ubuntzei.out");
    out << smallest;
    out.close();
    return 0;
}