https://www.luogu.com.cn/problem/P1938
思路
将点权转换为边权跑一遍最长路即可,一开始还想着拓扑排序,忘了是有环的
代码
#include <bits/stdc++.h>
using namespace std;
const int N = 222;
struct edge {
int v;
int w;
};
int money;
int pay, m1, n, m2, s;
vector <edge> g[N];
int dis[N];
int inq[N];
queue <int> q;
int main() {
//freopen("in.txt", "r", stdin);
cin >> pay >> m1 >> n >> m2 >> s;
for (int i = 0; i < m1; i++) {
int u, v;
cin >> u >> v;
g[u].push_back((edge){v, 0});
}
for (int i = 0; i < m2; i++) {
int j, k, t;
cin >> j >> k >> t;
g[j].push_back((edge){k, -t});
}
for (int i = 1; i <= n; i++) {
dis[i] = -9999999;
}
dis[s] = pay; money = pay;
q.push(s);
while (!q.empty()) {
int fro = q.front();
q.pop();
// cout << "*************" << endl;
// cout << fro << endl;
inq[fro] = 0;
for (int i = 0; i < g[fro].size(); i++) {
edge nv = g[fro][i];
if (dis[nv.v] < dis[fro] + nv.w + pay) {
dis[nv.v] = dis[fro] + nv.w + pay;
money = max(money, dis[nv.v]);
if (inq[nv.v]) continue;
q.push(nv.v);
inq[nv.v] = 1;
}
}
}
// for (int i = 1; i <= n; i++)
// cout << dis[i] << endl;
cout << money << endl;
return 0;
}