Cod sursă (job #708126)

Utilizator avatar CalinHangu HanguCalin CalinHangu IP ascuns
Problemă Dragoni2 (clasele 11-12) Compilator cpp-32 | 2,17 kb
Rundă Arhiva de probleme Status evaluat
Dată 16 mar. 2023 18:39:19 Scor 60
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>

#define ll long long
#define pb push_back
#define pii pair<int, int>
#define x first
#define y second

using namespace std;

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

const int NMAX = 805;
const char nl = '\n';
const int INF = 1e9;

/*
struct dragon{
    int x, type, w;
    bool operator <(const dragon &aux) const{
        return w > aux.w;
    }
};
*/

int q, n, m, dmax = -INF, capacity[NMAX], f[NMAX], dist[NMAX][NMAX], aux;

vector<pii> v[NMAX];
/// x - node, y - weight

priority_queue<pii> pq;


void dfs(int node){
    f[node] = 1;
    if(capacity[node] > dmax)
        dmax = capacity[node];
    for(auto i: v[node]){
        if(!f[i.x] && i.y <= capacity[1])
            dfs(i.x);
    }
}

void dijkstra(){
    dist[1][1] = 0;
    pq.push({1, 1});
    while(!pq.empty()){
        int cur_node = pq.top().x;
        int cur_type = pq.top().y;
        pq.pop();
        for(auto neigh: v[cur_node]){
            if(neigh.y <= capacity[cur_type]) {
                if(capacity[cur_type] > capacity[neigh.x])
                    aux = cur_type;
                else
                    aux = neigh.x;
                if(dist[neigh.x][aux] > dist[cur_node][cur_type] + neigh.y){
                    dist[neigh.x][aux] = dist[cur_node][cur_type] + neigh.y;
                    pq.push({neigh.x, aux});
                }
            }
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    in.tie(0);out.tie(0);
    in >> q >> n >> m;
    for(int i = 1; i <= n; ++i)
        in >> capacity[i];
    for(int i = 1; i <= m; ++i){
        int a, b, c;
        in >> a >> b >> c;
        v[a].pb({b, c});
        v[b].pb({a, c});
    }
    if(q == 1){
        dfs(1);
        out << dmax << nl;
    }

    else{
        for(int i = 1; i <= n; ++i){
            for(int j = 1; j <= n; ++j){
                dist[i][j] = INF;
            }
        }
        dijkstra();
        dmax = INF;
        for(int i = 1; i <= n; ++i)
            dmax = min(dmax, dist[n][i]);
        out << dmax << nl;
    }
    return 0;
}