Cod sursă (job #429332)

Utilizator avatar ezioconnor Vlad - Gabriel Iftimescu ezioconnor IP ascuns
Problemă Dragoni2 (clasele 11-12) Compilator cpp | 2,13 kb
Rundă Arhiva de probleme Status evaluat
Dată 3 feb. 2019 11:07:56 Scor 100
#include <bits/stdc++.h>

using namespace std;

ifstream in("dragoni2.in");
ofstream out("dragoni2.out");

const int oo = (1 << 30);

int n, m, p, v[801], MAX1;
int distanta[801][801];
bool vizitat[801];

vector < pair <int, int> > Muchii[801];
queue < pair <int, int> > coada;

void DFS(int nod, int eDragon)
{
    int vecin, cost;
    if (v[nod] > MAX1)
        MAX1 = v[nod];
    vizitat[nod] = 1;
    for (int i = 0; i < Muchii[nod].size(); ++i)
    {
        vecin = Muchii[nod][i].first;
        cost = Muchii[nod][i].second;
        if (vizitat[vecin] == 0 && cost <= eDragon)
            DFS(vecin, eDragon);
    }
}

void BFS()
{
    int x, y, c, newdrag, rez = oo, drag;
    coada.push(make_pair(1, 1));
    distanta[1][1] = 0;
    while (!coada.empty())
    {
        x = coada.front().first;
        drag = coada.front().second;
        coada.pop();
        for (int i = 0; i < Muchii[x].size(); ++i)
        {
            y = Muchii[x][i].first;
            c = Muchii[x][i].second;
            if (v[drag] < v[y])
                newdrag = y;
            else
                newdrag = drag;
            if (v[drag] >= c && distanta[y][newdrag] > distanta[x][drag] + c)
            {
                distanta[y][newdrag] = distanta[x][drag] + c;
                coada.push(make_pair(y, newdrag));
            }
        }
    }
    for (int i = 1; i <= n; ++i)
        rez = min(rez, distanta[n][i]);
    out << rez;
}

void rezolvare1()
{
    MAX1 = v[1];
    DFS(1, v[1]);
    out << MAX1;
}

void rezolvare2()
{
    BFS();
}

void citire()
{
    int x, y, c;
    in >> p >> n >> m;
    for (int i = 1; i <= n; ++i)
        in >> v[i];
    for (int i = 1; i <= m; ++i)
    {
        in >> x >> y >> c;
        Muchii[x].push_back(make_pair(y, c));
        Muchii[y].push_back(make_pair(x, c));
    }
    if (p == 1)
        rezolvare1();
    else
    {
        for (int i = 1; i <= n; ++i)
        {
            for (int j = 1; j <= n; ++j)
                distanta[i][j] = oo;
        }
        rezolvare2();
    }
}

int main()
{
    citire();
    return 0;
}