QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#357197#7404. Back and ForthckisekiWA 0ms3832kbC++234.9kb2024-03-18 19:16:452024-03-18 19:16:45

Judging History

你现在查看的是最新测评结果

  • [2024-03-18 19:16:45]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3832kb
  • [2024-03-18 19:16:45]
  • 提交

answer

// An AC a day keeps the doctor away.
#include <bits/stdc++.h>
using namespace std;

/*{{{*/
#define all(x) begin(x), end(x)
#ifdef CKISEKI
#include <experimental/iterator>
#define safe cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#define debug(a...) debug_(#a, a)
#define orange(a...) orange_(#a, a)
void debug_(auto s, auto ...a) {
  cerr << "\e[1;32m(" << s << ") = (";
  int f = 0;
  (..., (cerr << (f++ ? ", " : "") << a));
  cerr << ")\e[0m\n";
}
void orange_(auto s, auto L, auto R) {
  cerr << "\e[1;33m[ " << s << " ] = [ ";
  using namespace experimental;
  copy(L, R, make_ostream_joiner(cerr, ", "));
  cerr << " ]\e[0m\n";
}
#else
#define safe ((void)0)
#define debug(...) safe
#define orange(...) safe
#endif

template <typename T, T MOD> class Modular {
  public:
    constexpr Modular() : v() {}
    template <typename U> Modular(const U &u) { v = (0 <= u && u < MOD ? u : (u%MOD+MOD)%MOD); }
    template <typename U> explicit operator U() const { return U(v); }
    T operator()() const { return v; }
#define REFOP(type, expr...) Modular &operator type (const Modular &rhs) { return expr, *this; }
    REFOP(+=, v += rhs.v - MOD, v += MOD & (v >> width)) ; REFOP(-=, v -= rhs.v, v += MOD & (v >> width))
    // fits for MOD^2 <= 9e18
    REFOP(*=, v = static_cast<T>(1LL * v * rhs.v % MOD)) ; REFOP(/=, *this *= inverse(rhs.v))
#define VALOP(op) friend Modular operator op (Modular a, const Modular &b) { return a op##= b; }
    VALOP(+) ; VALOP(-) ; VALOP(*) ; VALOP(/)
    Modular operator-() const { return 0 - *this; }
    friend bool operator == (const Modular &lhs, const Modular &rhs) { return lhs.v == rhs.v; }
    friend bool operator != (const Modular &lhs, const Modular &rhs) { return lhs.v != rhs.v; }
    friend std::istream & operator>>(std::istream &I, Modular &m) { T x; I >> x, m = x; return I; }
    friend std::ostream & operator<<(std::ostream &O, const Modular &m) { return O << m.v; }
  private:
    constexpr static int width = sizeof(T) * 8 - 1;
    T v;
    static T inverse(T a) {
      // copy from tourist's template
      T u = 0, v = 1, m = MOD;
      while (a != 0) {
        T t = m / a;
        m -= t * a; std::swap(a, m);
        u -= t * v; std::swap(u, v);
      }
      assert(m == 1);
      return u;
    }
};

using lld = int64_t;
using llf = long double;
template <typename T> using max_heap = std::priority_queue<T,vector<T>,less<T> >;
template <typename T> using min_heap = std::priority_queue<T,vector<T>,greater<T> >;
template <typename V, typename T> int get_pos(const V &v, T x) { return lower_bound(all(v),x) - begin(v); }
template <typename V> void sort_uni(V &v) { sort(all(v)), v.erase(unique(all(v)),end(v)); }
template <typename T> bool chmin(T &x, const T &v) { return v < x ? (x=v, true) : false; }
template <typename T> bool chmax(T &x, const T &v) { return x < v ? (x=v, true) : false; }
constexpr inline lld cdiv(lld x, lld m) { return x/m + (x%m ? (x<0) ^ (m>0) : 0); } // ceiling divide
constexpr inline lld modpow(lld e,lld p,lld m) { lld r=1; for(e%=m;p;p>>=1,e=e*e%m) if(p&1) r=r*e%m; return r; }/*}}}*/

constexpr llf eps = 1e-9;
constexpr lld maxn = 3025, INF = 1e18, mod = 998244353, KK = 14699, inf = 1e9;
using Mint = Modular<int, mod>;
Mint modpow(Mint e, uint64_t p) { Mint r = 1; while (p) (p&1) && (r *= e), e *= e, p >>= 1; return r; } // 0^0 = 1

const auto dummy = [] { return cin.tie(nullptr)->sync_with_stdio(false); }();

void solve() {
  int n, m, s, t;
  cin >> n >> m >> s >> t;
  --s, --t;
  vector<int> cost(n);
  for (int &x : cost) cin >> x;

  vector<vector<int>> g(n), rg(n);
  for (int i = 0; i < m; i++) {
    int u, v;
    cin >> u >> v;
    --u, --v;
    g[u].emplace_back(v);
    rg[v].emplace_back(u);
  }

  vector<vector<int>> d(n, vector<int>(n, inf));
  for (int S = 0; S < n; S++) {
    min_heap<pair<int,int>> h;
    h.emplace(d[S][S] = 0, S);
    while (!h.empty()) {
      auto [D, i] = h.top(); h.pop();
      if (d[S][i] != D) continue;
      for (int j : g[i]) {
        if (d[S][j] > d[S][i] + cost[j]) {
          d[S][j] = d[S][i] + cost[j];
          h.emplace(d[S][j], j);
        }
      }
    }
  }

  min_heap<tuple<int,int,int>> pq;
  vector<vector<int>> dp(n, vector<int>(n, inf));
  auto relax = [&](int u, int v, int w) {
    if (dp[u][v] > w) {
      dp[u][v] = w;
      pq.emplace(dp[u][v], u, v);
    }
  };

  pq.emplace(dp[s][s] = cost[s], s, s);

  while (!pq.empty()) {
    auto [D, a, b] = pq.top(); pq.pop();
    if (D != dp[a][b]) continue;

    for (int x : g[a])
      relax(x, b, dp[a][b] + (x==b ? 0 : cost[x]));
    for (int x : rg[b])
      relax(a, x, dp[a][b] + (x==a ? 0 : cost[x]));

    if (a != b) {
      relax(b, a, dp[a][b] + d[a][b] - cost[b]);
    }
  }

  cout << dp[t][t] << '\n';
}

signed main() {
  int T;
  cin >> T;
  while (T--)
    solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3796kb

input:

3
4 5 1 4
1 1 1 1
1 2
2 3
3 1
4 2
3 4
4 4 1 2
1 1 1 1
1 2
2 3
3 4
4 1
4 8 1 3
1 100 1 1
1 2
2 1
2 3
3 2
1 4
4 1
3 4
4 3

output:

4
4
3

result:

ok 3 number(s): "4 4 3"

Test #2:

score: -100
Wrong Answer
time: 0ms
memory: 3832kb

input:

1
2 0 1 2
1 1

output:

1000000000

result:

wrong answer 1st numbers differ - expected: '-1', found: '1000000000'