记录编号 |
604994 |
评测结果 |
AAAAAAAAAA |
题目名称 |
2449.[APIO2009] 抢掠计划 |
最终得分 |
100 |
用户昵称 |
hsl_beat |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.044 s |
提交时间 |
2025-08-13 12:17:57 |
内存使用 |
3.94 MiB |
显示代码纯文本
// 这个代码常数估计很差 所以卡一下(?
#include<bits/stdc++.h>
using namespace std;
#define int long long
struct Graph
{
int _n;
vector<vector<int>> _edges;
vector<int> _in;
explicit Graph(int __n) : _n(__n) {
_edges.resize(__n + 1, {});
_in.resize(_n + 1, 0);
}
void adddeg(int _x) {
_in[_x]++;
}
void addedge(int _u, int _v) {
assert(1 <= _u && _u <= _n);
assert(1 <= _v && _v <= _n);
_edges[_u].push_back(_v);
adddeg(_v);
}
vector<int> tarjan() {
vector<int> _res(_n + 1, 0);
vector<int> _dfn(_n + 1, 0), _low(_n + 1, 0);
stack<int> _st;
vector<bool> _vis(_n + 1, 0);
int _cnt = 0, _cntscc = 0;
auto dfs = [&](auto &&self, int _u) -> void {
_st.push(_u);
_vis[_u] = 1;
_cnt++;
_dfn[_u] = _low[_u] = _cnt;
for (auto _v : _edges[_u]) {
if (!_dfn[_v]) {
self(self, _v);
_low[_u] = min(_low[_u], _low[_v]);
} else if (_vis[_v]) {
_low[_u] = min(_low[_u], _dfn[_v]);
}
}
if (_dfn[_u] == _low[_u]) {
int _v;
_cntscc++;
do {
_v = _st.top();
_st.pop();
_vis[_v] = 0;
_res[_v] = _cntscc;
} while (_v != _u);
}
};
for (int _i = 1; _i <= _n; ++_i) {
if (!_dfn[_i]) {
dfs(dfs, _i);
}
}
return _res;
}
vector<int> topo() {
queue<int> _que;
vector<int> _res;
for (int _i = 1; _i <= _n; ++_i) {
if (_in[_i] == 0) {
_que.push(_i);
_res.push_back(_i);
}
}
while (_que.size()) {
int _x = _que.front();
_que.pop();
for (auto _nex : _edges[_x]) {
if (--_in[_nex] == 0) {
_que.push(_nex);
_res.push_back(_nex);
}
}
}
assert(_res.size() == _n);
return _res;
}
};
int dp[555555];
bool vis[555555];
int n, m;
int a[555555], b[555555];
struct wGraph
{
int _n;
vector<vector<pair<int, int>>> _edges;
explicit wGraph(int __n) : _n(__n) {
_edges.resize(__n + 1, {});
}
void addedge(int _u, int _v, int _w) {
assert(1 <= _u && _u <= _n);
assert(1 <= _v && _v <= _n);
_edges[_u].push_back({_v, _w});
}
vector<int> dijkstra(int _s) {
assert(1 <= _s && _s <= _n);
vector<int> _res(_n + 1, 0);
_res[_s] = b[_s];
priority_queue<pair<int, int>, vector<pair<int, int>>, less<>> _pq;
_pq.push({0, _s});
while (!_pq.empty()) {
int _d = _pq.top().first;
int _u = _pq.top().second;
_pq.pop();
if (_d > _res[_u]) {
continue;
}
for (auto _tp : _edges[_u]) {
int _v = _tp.first;
int _w = _tp.second;
if (_res[_v] < _res[_u] + _w) {
_res[_v] = _res[_u] + _w;
_pq.push({_res[_v], _v});
}
}
}
return _res;
}
};
signed main()
{
freopen("atm.in", "r", stdin);
freopen("atm.out", "w", stdout);
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
Graph g(n);
vector<pair<int, int>> edges(m);
for (int i = 0; i < m; ++i) {
cin >> edges[i].first >> edges[i].second;
g.addedge(edges[i].first, edges[i].second);
}
auto scc = g.tarjan();
int cnt = *max_element(scc.begin(), scc.end());
for (int i = 1; i <= n; ++i) {
cin >> a[i];
b[scc[i]] += a[i];
}
int s, p;
cin >> s >> p;
s = scc[s];
for (int i = 1; i <= p; ++i) {
int x;
cin >> x;
vis[scc[x]] = 1;
}
wGraph ng(cnt);
for (int i = 0; i < m; ++i) {
if (scc[edges[i].first] != scc[edges[i].second]) {
ng.addedge(scc[edges[i].first], scc[edges[i].second], b[scc[edges[i].second]]);
}
}
auto ans = ng.dijkstra(s);
int anss = 0;
for (int i = 1; i < ans.size(); ++i) {
if (vis[i]) {
anss = max(anss, ans[i]);
}
}
cout << anss;
return 0;
}