Pagini recente »
Rating Enache Adelina (ade_tomi)
|
Istoria paginii runda/s18_tema_5
|
Istoria paginii runda/2023-10-01-clasa-8-tema-3
|
Atașamentele paginii Clasament 2021-10-29-clasa-6-tema-062
|
Cod sursă (job #708143)
Cod sursă (job
#708143)
#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 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()
{
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;
}