QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#207372#7404. Back and ForthRedDiamondWA 0ms3596kbC++231.6kb2023-10-08 14:49:552023-10-08 14:49:55

Judging History

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

  • [2023-10-08 14:49:55]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3596kb
  • [2023-10-08 14:49:55]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
const int N = 200 + 7;
const int INF = (int) 1e9;
int p[N];
int dist[N][N];
int n, m, s, t;

struct Edge {
  int x;
  int y;
  int v;
};

bool operator < (Edge a, Edge b) {
  return a.v > b.v;
}

bool vis[N][N];

void solve() {
  cin >> n >> m >> s >> t;
  for (int i = 1; i <= n; i++) {
    cin >> p[i];
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dist[i][j] = INF;
      vis[i][j] = 0;
    }
  }
  for (int i = 1; i <= m; i++) {
    int x, y;
    cin >> x >> y;
    dist[x][y] = p[y];
  }
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
      }
    }
  }
  if (dist[s][t] == INF) {
    cout << "-1\n";
    return;
  }
  priority_queue<Edge> pq;
  pq.push({s, s, p[s]});
  while (!pq.empty()) {
    Edge e = pq.top();
    pq.pop();
    if (vis[e.x][e.y]) continue;
    vis[e.x][e.y] = 1;
    if (e.x == t && e.y == t) {
      cout << e.v << "\n";
      return;
    }
    if (!vis[e.y][e.x]) {
      pq.push({e.y, e.x, e.v + dist[e.x][e.y] - p[e.x]});
    }
    for (int k = 1; k <= n; k++) {
      pq.push({k, e.y, e.v + dist[e.x][k] - p[k] * (e.y == k)});
      pq.push({e.x, k, e.v + dist[k][e.y] - p[e.y] * (e.x == k)});
    }
  }
}

signed main() {
#ifdef ONPC
  freopen("input.txt", "r", stdin);
#else
  ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#endif // ONPC

  int tt;
  cin >> tt;
  while (tt--) {
    solve();
  }
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

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: 0
Accepted
time: 0ms
memory: 3420kb

input:

1
2 0 1 2
1 1

output:

-1

result:

ok 1 number(s): "-1"

Test #3:

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

input:

10
20 40 5 16
2 1 1 1 2 1 1 2 1 1 1 2 1 1 1 2 1 2 1 2
7 2
8 5
15 4
17 13
11 6
9 2
12 13
12 1
1 8
2 20
6 9
18 3
2 15
10 12
4 17
11 5
19 15
15 9
14 7
11 2
1 15
16 1
13 3
11 16
5 10
19 6
1 3
13 14
20 11
6 19
7 9
20 12
17 18
13 9
4 18
3 19
13 17
19 9
19 10
8 6
20 40 10 1
2 2 2 2 1 2 1 2 2 2 2 2 1 2 2 1 ...

output:

15
14
11
11
14
7
9
12
12
12

result:

wrong answer 4th numbers differ - expected: '12', found: '11'