debug_noob @ 2021-10-14 00:51:21
#include <iostream>
#include <string.h>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#define inf 0x3f3f3f3f
#define maxn 10010
using namespace std;
struct edge
{
int to, w, next;
} e[100100];
struct node
{
int dis;
int pos;
bool operator<(const node &a) const
{
return dis > a.dis;
}
};
int vis[maxn] = {0}, head[maxn] = {0}, cnt = 1, n, m, b, dis[maxn] = {0}, f[maxn] = {0}, f2[maxn] = {0}, ans = inf;
void add(int u, int v, int w)
{
e[cnt].to = v;
e[cnt].w = w;
e[cnt].next = head[u];
head[u] = cnt++;
}
bool dijkstra(int x)
{
if (x < f[1] || x < f[n])
return false;
priority_queue<node> q;
memset(dis, inf, sizeof(dis));
for (int i = 1; i <= n; i++)
{
if (f[i] > x)
vis[i] = 1;
else
vis[x] = 0;
}
q.push(node{0, 1});
dis[1] = 0;
while (!q.empty())
{
node temp = q.top();
q.pop();
int d = temp.dis, p = temp.pos;
if (vis[p])
continue;
vis[p] = 1;
for (int i = head[p]; i; i = e[i].next)
{
int v = e[i].to, w = e[i].w;
if (!vis[v] && dis[v] > dis[p] + w)
{
dis[v] = dis[p] + w;
q.push(node{dis[v], v});
}
}
}
if (dis[n] <= b)
return true;
else
return false;
}
int main()
{
cin >> n >> m >> b;
for (int i = 1; i <= n; i++)
{
scanf("%d", &f[i]);
f2[i] = f[i];
}
sort(f2 + 1, f2 + n + 1);
for (int u = 1, aa, bb, cc; u <= m; u++)
{
scanf("%d%d%d", &aa, &bb, &cc);
add(aa, bb, cc);
add(bb, aa, cc);
}
ans = f2[n];
if (!dijkstra(f2[n]))
{
cout << "AFK" << endl;
return 0;
}
int l = 1, r = n;
while (l <= r)
{
int mid = (l + r) >> 1;
if (dijkstra(f2[mid]))
{
ans = f2[mid];
r = mid - 1;
}
else
l = mid + 1;
}
cout << ans << endl;
return 0;
}