이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
struct Edge {
int u, v;
long long cost;
bool operator<(const Edge& e) const {
return cost < e.cost;
}
};
int find(vector<int>& parent, int i) {
if (parent[i] != i) {
parent[i] = find(parent, parent[i]);
}
return parent[i];
}
void union_set(vector<int>& parent, vector<int>& rank, int a, int b) {
a = find(parent, a);
b = find(parent, b);
if (a != b) {
if (rank[a] < rank[b]) swap(a, b);
parent[b] = a;
if (rank[a] == rank[b]) rank[a]++;
}
}
int main() {
int N, M, K;
cin >> N >> M >> K;
vector<Edge> edges(M);
for (int i = 0; i < M; i++) {
cin >> edges[i].u >> edges[i].v >> edges[i].cost;
}
int x, y;
cin >> x >> y; // Only one new road for K=1
vector<int> p(N + 1);
for (int i = 1; i <= N; i++) {
cin >> p[i];
}
sort(edges.begin(), edges.end());
vector<int> parent(N + 1), rank(N + 1, 0);
iota(parent.begin(), parent.end(), 0);
long long max_revenue = 0;
// Compute participants moving through each road to town 1
vector<int> participants_through(N + 1, 0);
function<void(int, int)> dfs = [&](int u, int prev) {
participants_through[u] = p[u];
for (const auto& e : edges) {
if (e.u == u && e.v != prev) {
dfs(e.v, u);
participants_through[u] += participants_through[e.v];
} else if (e.v == u && e.u != prev) {
dfs(e.u, u);
participants_through[u] += participants_through[e.u];
}
}
};
dfs(1, -1);
// Trying different tolls for the new road
for (long long toll = 1; toll <= 1000000; toll++) {
vector<Edge> new_edges = edges;
new_edges.push_back({x, y, toll});
sort(new_edges.begin(), new_edges.end());
iota(parent.begin(), parent.end(), 0);
fill(rank.begin(), rank.end(), 0);
long long mst_cost = 0, revenue = 0;
int count = 0;
for (const auto& e : new_edges) {
if (find(parent, e.u) != find(parent, e.v)) {
union_set(parent, rank, e.u, e.v);
mst_cost += e.cost;
if ((e.u == x && e.v == y) || (e.u == y && e.v == x)) {
revenue = toll * (participants_through[x] + participants_through[y]); // Assuming x and y are connected directly to town 1
}
if (++count == N - 1) break;
}
}
max_revenue = max(max_revenue, revenue);
}
cout << max_revenue << endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |