Cod sursă (job #369120)

Utilizator avatar Artanis Petrea Calin Artanis IP ascuns
Problemă Dragoni2 (clasele 11-12) Compilator cpp | 1,21 kb
Rundă lasm_22_03_b Status evaluat
Dată 22 mar. 2018 16:02:32 Scor 20
#include <bits/stdc++.h>
using namespace std;

int c, n, m, viz[801], f[801], max_l, max_d;
vector <pair <int, int> > a[801];

void dfs_1(int n)
{
	viz[n] = 1;
	if (f[n] > max_l)
		max_l = f[n];
	for (int i = 0; i < a[n].size(); i++)
		if(!viz[a[n][i].first] && a[n][i].second <= f[1])
			dfs_1(a[n][i].first);
}

void erase_v(int x, int y)
{
	for (int i = 0; i <= a[x].size(); i++)
		if (a[x][i].first == y)
			a[x].erase(a[x].begin() + i);
}

void dfs_2(int n, int dist)
{
	if (f[n] > max_l)
		max_l = f[n];
	if (dist > max_d)
		max_d = dist;
	for (int i = 0; i < a[n].size(); i++)
		if(!viz[a[n][i].first] && a[n][i].second <= max_l)
		{
			int x = a[n][i].first, y = a[n][i].second;
			a[n].erase(a[n].begin() + i);
			erase_v(x, n);
			dfs_2(x, y + dist);
		}
}

int main()
{
	ifstream cin("dragoni2.in");
	ofstream cout("dragoni2.out");
	cin >> c >> n >> m;
	for (int i = 1; i <= n; i++)
		cin >> f[i];
	for (int i = 1; i <= m; i++)
	{
		int x, y, z;
		cin >> x >> y >> z;
		a[x].push_back({y, z});
		a[y].push_back({x, z});
	}
	if (c == 1)
	{
		max_l = f[1];
		dfs_1(1);
		cout << max_l;
	}
	else
	{
		max_l = f[1];
		dfs_2(1, 0);
		cout << max_d;
	}
	return 0;
}