# Cow Hurdles## 题目描述Farmer John wants the cows to prepare for the county jumping competition so Bessie and the gang are practicing jumping over hurdles They are getting tired though so they want to be
#include <bits/stdc++.h>
using namespace std;
int n, m, Q;
const int N = 310, M = 25100, INF = 0x3f3f3f3f;
int h[N], e[M], ne[M], idx, w[M];
int q[N], dist[N], cnt[N];
bool st[N];
int id[N], timestamp;
int low[N], dfn[N];
void add(int a, int b, int c) {
e[idx] = b, ne[idx] = h[a], w[idx] = c, h[a] = idx++;
}
void tarjan(int u) {
low[u] = dfn[u] = ++timestamp;
q[u] = 1, st[u] = true;
for (int i = h[u]; ~i; i = ne[i]) {
int j = e[i];
if (!dfn[j]) {
tarjan(j);
low[u] = min(low[u], low[j]);
} else if (st[j]) {
low[u] = min(low[u], dfn[j]);
}
}
if (dfn[u] == low[u]) {
id[u] = ++timestamp;
q[u] = 0;
st[u] = false;
}
}
bool spfa(int limit) {
memset(dist, 0xcf, sizeof dist);
memset(cnt, 0, sizeof cnt);
dist[1] = 0;
cnt[1] = 1;
int hh = 0, tt = 0;
q[tt++] = 1;
while (hh != tt) {
int t = q[hh++];
if (hh == N) hh = 0;
st[t] = false;
for (int i = h[t]; ~i; i = ne[i]) {
int j = e[i];
if (w[i] > limit) continue;
if (dist[j] < dist[t] + w[i]) {
dist[j] = dist[t] + w[i];
cnt[j] = cnt[t] + 1;
if (cnt[j] >= n) return true;
if (!st[j]) {
q[tt++] = j;
if (tt == N) tt = 0;
st[j] = true;
}
}
}
}
return false;
}
int main() {
cin >> n >> m >> Q;
memset(h, -1, sizeof h);
while (m--) {
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
}
for (int i = 1; i <= n; i++)
if (!dfn[i])
tarjan(i);
for (int i = 0; i < idx; i++) {
int a = e[i], b = e[i ^ 1];
if (id[a] != id[b])
add(id[b], id[a], w[i]);
}
while (Q--) {
int a, b;
cin >> a >> b;
if (id[a] != id[b]) puts("-1");
else {
int l = 0, r = INF;
while (l < r) {
int mid = l + r >> 1;
if (spfa(mid)) r = mid;
else l = mid + 1;
}
cout << l << endl;
}
}
return 0;
}
``
原文地址: https://www.cveoy.top/t/topic/d5Pg 著作权归作者所有。请勿转载和采集!