求助,
1.为什么会会在8~9测试点会出现数组越界,明明开足了呀?
2.为什么if (dis[i][j] > H) ans += j * H;中j也要开long long 才能过?
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
const int N = 3e3 + 10, M = 1e4 + 10;
const long long H = 1e9;
int h[N], e[M], en[M], ne[M], w[M], cnt[N], idx = 1;
int f[N], dis[N][N];
bool st[N], vis[N][N], cmp;
int n, m;
long long ans = 0;
void add(int a, int b, int c) {
e[idx] = b, en[idx] = a, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void spfa() {
memset(f, 0x3f, sizeof f);
queue<int> q;
q.push(0);
st[0] = 1;
f[0] = 0;
while (q.size()) {
int t = q.front();
q.pop();
st[t] = 0;
for (int i = h[t]; i; i = ne[i]) {
int j = e[i];
if (f[j] > f[t] + w[i]) {
f[j] = f[t] + w[i];
if (!st[j]) {
q.push(j);
st[j] = 1;
cnt[j]++;
if (cnt[j] > n+1) {
cmp = 1;
return;
}
}
}
}
}
return ;
}
void dijkstra(int u) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
q.push({0, u});
dis[u][u] = 0;
while (!q.empty()) {
auto ab = q.top();
q.pop();
int t = ab.second;
if (vis[u][t]) continue;
vis[u][t] = 1;
for (int i = h[t]; i; i = ne[i]) {
int j = e[i];
if (dis[u][j] > dis[u][t] + w[i]) {
dis[u][j] = dis[u][t] + w[i];
if (!vis[u][j])q.push({dis[u][j], j});
}
}
}
}
signed main() {
cin >> n >> m;
for (int i = 1; i <= m; i++) {
int c, a, b;
cin >> a >> b >> c;
add(a, b, c);
add(0, i, 0);
}
spfa();
if (cmp) {
cout << -1;
return 0;
}
memset(dis, 0x3f, sizeof dis);
for (int i = 1; i < idx; i++) {
int a = en[i], b = e[i], c = w[i];
w[i] = c + f[a] - f[b];
}
for (int i = 1; i <= n; i++) {
dijkstra(i);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (dis[i][j] > H) ans += j * H;
else if (i != j)ans += (dis[i][j] + f[j] - f[i]) * j;
}
cout << ans << endl;
ans = 0;
}
return 0;
}