Pagini recente »
Atașamentele paginii Clasament 2015-02-24-clasa-8-tema-21
|
triunghiiii
|
Istoria paginii utilizator/mihnea_cazan
|
Monitorul de evaluare
|
Cod sursă (job #708151)
Cod sursă (job
#708151)
#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];
vector<pii> v[NMAX];
/// x - node, y - weight
priority_queue<dragon> 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, 0});
while(!pq.empty()){
int cur_node = pq.top().x;
int cur_type = pq.top().type;
int cur_weight = pq.top().w;
pq.pop();
if(capacity[cur_node] > capacity[cur_type]){
dist[cur_node][cur_node] = min(dist[cur_node][cur_node], dist[cur_node][cur_type]);
cur_type = cur_node;
}
for(auto neigh: v[cur_node]){
if(dist[neigh.x][cur_type] > cur_weight + neigh.y && neigh.y <= capacity[cur_type]){
dist[neigh.x][cur_type] = cur_weight + neigh.y;
pq.push({neigh.x, cur_type, dist[neigh.x][cur_type]});
}
}
}
}
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;
}