题意:求拓扑结构中的最长路径
用删除源点法,出队的时候更新邻接点的最大值。
// Problem#: 12261
// Submission#: 3236983
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <map>
using namespace std;
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define REP(i, s, t) for(int (i)=(s);(i)<=(t);++(i))
#define UREP(i, s, t) for(int (i)=(s);(i)>=(t);--(i))
typedef long long LL;
const int maxn = 10005;
struct Edge {
int from, to;
};
vector<Edge> E;
vector<int> g[maxn];
int n, m;
int t[maxn];
int in[maxn];
int d[maxn];
int main() {
// freopen("input.in", "r", stdin);
while (cin >> n >> m) {
memset(in, 0, sizeof(in));
REP(i, 1, n) g[i].clear();
REP(i, 1, n) {
cin >> t[i];
}
int u, v;
REP(i, 1, m) {
cin >> u >> v;
//cout << u << ' ' << v << endl;
E.push_back((Edge){u, v});
in[v]++;
g[u].push_back(E.size()-1);
}
queue<int> Q;
REP(i, 1, n)
if (in[i] == 0)
Q.push(i);
memset(d, 0, sizeof(d));
int ans = 0;
while (Q.empty() == false) {
int fr = Q.front();Q.pop();
d[fr] += t[fr];
ans = max(ans, d[fr]);
int sz = g[fr].size();
REP(i, 0, sz-1) {
int id = g[fr][i];
int v = E[id].to;
d[v] = max(d[v], d[fr]);
if (--in[v] == 0) Q.push(v);
}
}
cout << ans << endl;
}
return 0;
}