Cod sursa(job #1638757)

Utilizator hunisanHunor Csaki hunisan Data 8 martie 2016 08:57:59
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.39 kb
#include <iostream>
#include <fstream>
#include <queue>
#include <algorithm>

using namespace std;

int n,m,k;

bool comp(pair<int,int> a, pair<int,int> b)
{
    return a.second < b.second;

};

struct node
{
    bool *sz;
    int n;

    node(int a = 0)
    {
        sz = new bool[k];
        for(int i =0; i < k; i++)
            sz[i]=0;

        n=a;
    }
    bool getsz()
    {
        if(k==0)
            return true;
        for(int i = 0; i < k; i++)
            if(!sz[i])
                return false;

        return true;
    }
};
int main()
{
    cout << "hunibuntu" << endl;

    ifstream be("ubuntzei.in");
    ofstream ki("ubuntzei.out");


    be >> n >> m;
    be >> k;
    int sz[k];
    for(int i = 0; i < k; i++)
        be >> sz[i];

    int a[n][n];
    int d[n], volt[n];
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < n; j++)
            a[i][j]=0;
        d[i] = -1;
        volt[i] = 0;
    }
    for(int i = 0; i < m; i++)
    {
        int x, y, c;
        be >> x >> y >> c;
        a[x-1][y-1]=c;
        a[y-1][x-1]=c;

    }
    d[0]=0;

    queue<node> vsor;
    vsor.push(node(0));

    while(!vsor.empty())
    {
        node c = vsor.front();
        vsor.pop();
        //volt[c.n] = true;
        vector<pair<int,int> > lista;
        for(int i = 0; i < n; i++)
        {
            //cout << a[c][i] << endl;

            if(!volt[i] && a[c.n][i]>0)// && (d[i]==-1 || d[i] > d[c.n]+a[c.n][i]))
            {
                lista.push_back(make_pair(i,a[c.n][i]));
                d[i] = d[c.n]+a[c.n][i];
                if(i==n-1 && c.getsz())
                {
                    while(!vsor.empty())
                        vsor.pop();

                    break;
                }
            }
        }
        sort(lista.begin(),lista.end(),comp);
        for(int i = 0; i < lista.size(); i++)
        {
            node newn = c;
            newn.n = lista[i].first;
            for(int j = 0; j < k; j++)
            {
                if(c.n == sz[j])
                {
                    newn.sz[j] = true;
                    break;
                }
            }
            vsor.push(newn);
        }
//        for(int i  = 0; i < n; i++)
//            cout << d[i] << " ";
//        cout << endl;
    }

    ki << d[n-1] << endl;

    return 0;
}