Cod sursa(job #1864386)

Utilizator jason2013Andronache Riccardo jason2013 Data 31 ianuarie 2017 18:52:58
Problema Ubuntzei Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 2.28 kb
#include<bits/stdc++.h>

#define pb push_back
#define mp make_pair
#define nod first
#define cost second
#define inf 0x3f3f3f3f

using namespace std;

ifstream f("ubuntzei.in");
ofstream g("ubuntzei.out");

const int NMAX = 2000;
vector< pair<int, int> > G[NMAX];
queue<int> q;
bool USED[NMAX];
int D[NMAX], vec_prieteni[NMAX], parinti[NMAX], drum[NMAX];
int n, m, k, Nod, counter;


inline bool exista(int value)
{
    int left, right, middle;
    left = 1;
    right = k;
    while(left <= right)
    {
        middle = (left+right) / 2;
        if(vec_prieteni[middle] == value) return true;
        else if(vec_prieteni[middle] > value) right = middle-1;
        else left = middle + 1;
    }
    return false;
}

void citire()
{
    f>>n>>m;
    k = min(15, n-2);
    for(int i = 1; i <= k; i ++)
        f>>vec_prieteni[i];

    for(int i = 1; i <= m; i++)
    {
        int x, y, c;
        f>>x>>y>>c;
        G[x].pb( mp(y, c) );
        G[y].pb( mp(x, c) );
    }

}

void Bellman_Ford()
{
    while(!q.empty())
    {
        int Nod = q.front();
        q.pop();
        USED[Nod] = false;
        for(int i = 0; i < G[Nod].size(); i++)
        {
            if(exista((G[Nod][i]).nod) == true) counter++;
            if( D[(G[Nod][i]).nod] > D[Nod] + (G[Nod][i]).cost)
            {
                D[(G[Nod][i]).nod] = D[Nod] + (G[Nod][i]).cost;
                parinti[(G[Nod][i]).nod] = Nod;
                if(USED[(G[Nod][i]).nod] ==  false)
                {
                    USED[(G[Nod][i]).nod] = true;
                    q.push((G[Nod][i]).nod);
                }
            }
        }

    }

}

int main()
{
    citire();
    memset(D, inf, sizeof(D));

    //--start node

    int START = 1;
    q.push(START);
    USED[START] = true;
    D[START] = 0;

    //--end start node

    sort(vec_prieteni, vec_prieteni+k);
    if(exista(START) == true) counter++;

    Bellman_Ford();
    int suma_minima = 0;


    int z = n, j = 1;
    //--find drum

    while( z!=0 )
    {
        drum[j] = D[z];
        z = parinti[z];
        j++;
    }

    if(counter >= k)
    {
        for(int i = 1; i <= n; i++) if(drum[i] != 0) suma_minima += drum[i];
        g<<suma_minima;
    }
    return 0;
}