QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#506878#1252. Floyd-Warshallhos_lyricWA 1ms6084kbC++144.0kb2024-08-06 00:41:272024-08-06 00:41:27

Judging History

This is the latest submission verdict.

  • [2024-08-06 00:41:27]
  • Judged
  • Verdict: WA
  • Time: 1ms
  • Memory: 6084kb
  • [2024-08-06 00:41:27]
  • Submitted

answer

#include <cassert>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

using namespace std;

using Int = long long;

template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &a) { return os << "(" << a.first << ", " << a.second << ")"; };
template <class T> ostream &operator<<(ostream &os, const vector<T> &as) { const int sz = as.size(); os << "["; for (int i = 0; i < sz; ++i) { if (i >= 256) { os << ", ..."; break; } if (i > 0) { os << ", "; } os << as[i]; } return os << "]"; }
template <class T> void pv(T a, T b) { for (T i = a; i != b; ++i) cerr << *i << " "; cerr << endl; }
template <class T> bool chmin(T &t, const T &f) { if (t > f) { t = f; return true; } return false; }
template <class T> bool chmax(T &t, const T &f) { if (t < f) { t = f; return true; } return false; }
#define COLOR(s) ("\x1b[" s "m")


/*
  G[u][v]
    <= 2 edges
    OR
    path u = u[0], u[1], ..., u[l-1], u[l] = v
    \exists 0 <= a <= b <= l
    s.t.
      - u[0] > ... > u[a]
      - u[a], ..., u[b] < min{u, v}
      - u[b] < ... < u[l]
*/

constexpr Int INF = 1001001001001001001LL;

int N, M;
vector<int> A, B;
vector<Int> C;

vector<vector<pair<Int, int>>> graphs[2];

void shortest(int h, int src, int lim, Int *dist) {
  using Entry = pair<Int, int>;
  priority_queue<Entry, vector<Entry>, greater<Entry>> que;
  fill(dist, dist + N, INF);
  que.emplace(dist[src] = 0, src);
  for (; que.size(); ) {
    const Int c = que.top().first;
    const int u = que.top().second;
    que.pop();
    if (dist[u] == c) {
      for (const auto &e : graphs[h][u]) {
        const Int cc = c + e.first;
        const int v = e.second;
        if (v < lim) {
          if (chmin(dist[v], cc)) {
            que.emplace(cc, v);
          }
        }
      }
    }
  }
}

Int F[2010][2010];
Int G[2010][2010];

int main() {
  for (; ~scanf("%d%d", &N, &M); ) {
    A.resize(M);
    B.resize(M);
    C.resize(M);
    for (int i = 0; i < M; ++i) {
      scanf("%d%d%lld", &A[i], &B[i], &C[i]);
      --A[i];
      --B[i];
    }
    
    graphs[0].assign(N, {});
    graphs[1].assign(N, {});
    for (int i = 0; i < M; ++i) {
      graphs[0][A[i]].emplace_back(C[i], B[i]);
      graphs[1][B[i]].emplace_back(C[i], A[i]);
    }
    
    for (int u = 0; u < N; ++u) {
      shortest(0, u, N, F[u]);
    }
// for(int u=0;u<N;++u){cerr<<"F["<<u<<"] = ";pv(F[u],F[u]+N);}
    
    vector<Int> dist(N);
    for (int u = 0; u < N; ++u) {
      shortest(0, u, u, dist.data());
      for (int v = 0; v < N; ++v) {
        for (const auto &e : graphs[1][v]) {
          const int w = e.second;
          if (w < v) {
            chmin(dist[v], dist[w] + e.first);
          }
        }
      }
      for (int v = u + 1; v < N; ++v) {
        G[u][v] = dist[v];
      }
    }
    for (int v = 0; v < N; ++v) {
      shortest(1, v, v, dist.data());
      for (int u = 0; u < N; ++u) {
        for (const auto &e : graphs[0][u]) {
          const int w = e.second;
          if (u > w) {
            chmin(dist[u], e.first + dist[w]);
          }
        }
      }
      for (int u = v + 1; u < N; ++u) {
        G[u][v] = dist[u];
      }
    }
    for (int i = 0; i < M; ++i) for (int j = 0; j < M; ++j) if (B[i] == A[j]) {
      chmin(G[A[i]][B[j]], C[i] + C[j]);
    }
// for(int u=0;u<N;++u){cerr<<"G["<<u<<"] = ";pv(G[u],G[u]+N);}
    
    int ans = 0;
    for (int u = 0; u < N; ++u) for (int v = 0; v < N; ++v) {
      if (F[u][v] != G[u][v]) {
        ++ans;
      }
    }
    printf("%d\n", ans);
  }
  return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 6084kb

input:

4 5
2 3 4
3 4 3
4 2 2
1 3 1
1 2 9

output:

1

result:

ok answer is '1'

Test #2:

score: 0
Accepted
time: 0ms
memory: 5828kb

input:

2 1
1 2 1000

output:

0

result:

ok answer is '0'

Test #3:

score: 0
Accepted
time: 1ms
memory: 5848kb

input:

2 1
2 1 420

output:

0

result:

ok answer is '0'

Test #4:

score: 0
Accepted
time: 1ms
memory: 5780kb

input:

2 2
1 2 69
2 1 333

output:

0

result:

ok answer is '0'

Test #5:

score: -100
Wrong Answer
time: 1ms
memory: 5952kb

input:

10 9
9 8 1
8 6 1
6 3 1
3 7 1
7 4 1
4 10 1
10 2 1
2 1 1
1 5 1

output:

22

result:

wrong answer expected '11', found '22'