translate this code from C++ to Java: #include <bits/stdc++.h> using namespace std;…
- User
translate this code from C++ to Java:
#include <bits/stdc++.h>
using namespace std;
struct node { int to, w, id; };
struct to_pred { int u, vip, d, prox, id; };
const int INF = 1e9 + 7; const int N = 81; const int M = 501; int dp[N][N][2 * M]; to_pred pred[N][N][2 * M]; int a, n, m, op = 0; vector is_vip; vector<vector> gr;
void make_ans() { dp[0][is_vip[0]][0] = 0; for (int d = 0; d < 2 * m; d++) { for (int u = 0; u < n; u++) { for (int vip = 0; vip <= n; vip++) { for (auto c : gr[u]) { int to = c.to, w = c.w, fl = is_vip[to], id = c.id; if (vip > 0) { int nvip = vip - 1 + fl, nd = d + 1; if (dp[to][nvip][nd] > dp[u][vip][d]) { pred[to][nvip][nd] = { u, vip, d, 1, id }; dp[to][nvip][nd] = dp[u][vip][d]; } } int nvip = vip + fl, nd = d + 1; if (nvip > n) continue; if (dp[to][nvip][nd] > dp[u][vip][d] + w) { pred[to][nvip][nd] = { u, vip, d, 0, id }; dp[to][nvip][nd] = dp[u][vip][d] + w; } } } } } }
void solve() { cin >> a >> n >> m; is_vip = vector(n); for (int i = 0; i < n; i++) for (int j = 0; j <= n; j++) for (int x = 0; x <= 2 * m; x++) dp[i][j][x] = INF; gr = vector<vector>(n); for (int i = 0; i < n; i++) cin >> is_vip[i]; vector<vector> mn_to(n, vector(n, INF)), who = mn_to; int fl_st = 0; for (int i = 0; i < m; i++) { int u, v, w; cin >> u >> v >> w; u--; v--; if (w < mn_to[u][v]) { mn_to[u][v] = w; who[u][v] = i; } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (mn_to[i][j] == INF) continue; gr[i].push_back({ j, mn_to[i][j], who[i][j] }); } } make_ans(); int ans = INF, u = n - 1, cur_vip = -1, sz_p = -1; for (int i = 0; i <= 2 * m; i++) { for (int v = 0; v <= n; v++) { if (dp[n - 1][v][i] < ans) { ans = dp[n - 1][v][i]; cur_vip = v; sz_p = i; } } } vector<pair<int, int>> out; while (sz_p > 0) { to_pred next = pred[u][cur_vip][sz_p]; out.push_back({ next.id, next.prox }); u = next.u; cur_vip = next.vip; sz_p = next.d; } reverse(out.begin(), out.end()); cout << ans << " " << out.size() << endl; for (auto c : out) { cout << c.first + 1 << " " << 1 - c.second << endl; } }
signed main() { int t = 1; cout << fixed << setprecision(20); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); while (t--) { solve(); } }
- Assistant