Cod sursă (job #708157)

Utilizator avatar CalinHangu HanguCalin CalinHangu IP ascuns
Problemă Dragoni2 (clasele 11-12) Compilator cpp-32 | 3.01 kb
Rundă Arhiva de probleme Status evaluat
Dată 16 mar. 2023 19:01:14 Scor 100
#include <fstream>
#include <algorithm>
#include <vector>
#include <queue>
#include <stdio.h>
#include <ctype.h>

class InParser {
private:
	FILE *fin;
	char *buff;
	int sp;

	char read_ch() {
		++sp;
		if (sp == 4096) {
			sp = 0;
			fread(buff, 1, 4096, fin);
		}
		return buff[sp];
	}

public:
	InParser(const char* nume) {
		fin = fopen(nume, "r");
		buff = new char[4096]();
		sp = 4095;
	}

	InParser& operator >> (int &n) {
		char c;
		while (!isdigit(c = read_ch()) && c != '-');
		int sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}

	InParser& operator >> (long long &n) {
		char c;
		n = 0;
		while (!isdigit(c = read_ch()) && c != '-');
		long long sgn = 1;
		if (c == '-') {
			n = 0;
			sgn = -1;
		} else {
			n = c - '0';
		}
		while (isdigit(c = read_ch())) {
			n = 10 * n + c - '0';
		}
		n *= sgn;
		return *this;
	}
};

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

using namespace std;

InParser 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

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.front().x;
        int cur_type = pq.front().y;
        pq.pop();
        for(auto i : v[cur_node]) {
            if(i.y <= capacity[cur_type]) {
                if (capacity[cur_type] > capacity[i.x])
                    aux = cur_type;
                else
                    aux = i.x;
                if (dist[i.x][aux] > dist[cur_node][cur_type] + i.y) {
                    dist[i.x][aux] = dist[cur_node][cur_type] + i.y;
                    pq.push({ i.x,aux });
                }
            }
        }
    }
}

int main()
{
    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;
}