QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#273724 | #7883. Takeout Delivering | ucup-team2000# | WA | 0ms | 3596kb | C++20 | 1.9kb | 2023-12-03 03:37:59 | 2023-12-03 03:37:59 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
class DSU {
public:
vector<int> id;
DSU(int n){
id.resize(n);
for (int i = 0; i < n; ++i) id[i] = i;
}
int find(int u) {
if (id[u] == u) return u;
return id[u] = find(id[u]);
}
void unionThese(int u, int v) {
int idU = find(u), idV = find(v);
id[idU] = idV;
}
};
int main(){
int n, m; cin >> n >> m;
vector<vector<pair<int, int>>> adj(n + 1);
vector<vector<int>> edges(m);
for (int i = 0; i < m; ++i) {
int u, v, w; cin >> u >> v >> w;
adj[u].push_back({v,w});
adj[v].push_back({u,w});
edges[i] = {w, u, v};
}
sort(edges.begin(), edges.end());
DSU uf = *(new DSU(n + 1));
vector<vector<pair<int, int>>> res(n + 1);
for (int i = 0; i < edges.size(); ++i) {
vector<int> edge = edges[i];
int u = edge[1], v = edge[2], w = edge[0];
if (uf.find(u) != uf.find(v)) {
cout << u << " " << v << endl;
res[u].push_back({v, w});
res[v].push_back({u, w});
uf.unionThese(u, v);
}
}
vector<int> vis(n + 1); vector<pair<int, int>> ans(n + 1);
for (int i = 0; i < n + 1; ++i) ans[i] = {-1, -1};
queue<int> q; q.push(1); vis[1] = 1;
while (!q.empty()) {
int front = q.front(); q.pop();
for (auto i : res[front]) {
if (i.second >= ans[front].first) {
ans[i.first].second = ans[front].first;
ans[i.first].first = i.second;
} else if (i.second > ans[front].second) {
ans[i.first].second = i.second;
}
if (!vis[i.first]) {
q.push(i.first);
vis[i.first] = 1;
}
}
}
cout << ans[n].first + max(0, ans[n].second);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3596kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
2 3 1 2 2 4 5
result:
wrong answer 1st numbers differ - expected: '5', found: '2'