QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#639280 | #6615. Cross the Maze | jty233 | WA | 0ms | 3828kb | C++23 | 4.8kb | 2024-10-13 18:42:34 | 2024-10-13 18:42:35 |
Judging History
answer
#include "bits/stdc++.h"
using ll = long long;
struct MCFGraph {
struct Edge {
int v, c;
double f;
Edge(int v, int c, double f) : v(v), c(c), f(f) {}
};
const int n;
std::vector<Edge> e;
std::vector<std::vector<int>> g;
std::vector<double> h, dis;
std::vector<int> pre;
bool dijkstra(int s, int t) {
dis.assign(n, std::numeric_limits<double>::max());
pre.assign(n, -1);
std::priority_queue<std::pair<double, int>, std::vector<std::pair<double, int>>, std::greater<>> que;
dis[s] = 0;
que.emplace(0, s);
while (!que.empty()) {
double d = que.top().first;
int u = que.top().second;
que.pop();
if (dis[u] < d) continue;
for (int i : g[u]) {
int v = e[i].v;
int c = e[i].c;
double f = e[i].f;
if (c > 0 && dis[v] > d + h[u] - h[v] + f) {
dis[v] = d + h[u] - h[v] + f;
pre[v] = i;
que.emplace(dis[v], v);
}
}
}
return dis[t] != std::numeric_limits<double>::max();
}
MCFGraph(int n) : n(n), g(n) {}
void addEdge(int u, int v, int c, int f) {
g[u].push_back(e.size());
e.emplace_back(v, c, f);
g[v].push_back(e.size());
e.emplace_back(u, 0, -f);
}
std::pair<int, double> flow(int s, int t) {
int flow = 0;
double cost = 0;
h.assign(n, 0);
while (dijkstra(s, t)) {
for (int i = 0; i < n; ++i) h[i] += dis[i];
int aug = std::numeric_limits<int>::max();
for (int i = t; i != s; i = e[pre[i] ^ 1].v) aug = std::min(aug, e[pre[i]].c);
for (int i = t; i != s; i = e[pre[i] ^ 1].v) {
e[pre[i]].c -= aug;
e[pre[i] ^ 1].c += aug;
}
flow += aug;
cost += double(aug) * h[t];
}
return std::make_pair(flow, cost);
}
};
std::vector<std::pair<int, int>> players, ropes;
using std::cin, std::cout;
int h_dis(const std::pair<int, int> &a, const std::pair<int, int> &b) {
return std::abs(a.first - b.first) + std::abs(a.second - b.second);
}
double e_dis(const std::pair<int, int> &a, const std::pair<int, int> &b) {
return std::sqrt((a.first - b.first) * (a.first - b.first) + (a.second - b.second) * (a.second - b.second));
}
bool check(int mid, std::vector<std::pair<int, int>> &ans) {
MCFGraph g(players.size() * 2 + 5);
std::vector<std::tuple<int, int, int>> edge_id;
edge_id.reserve(players.size() * ropes.size());
for(int i = 0; i < players.size(); i++) {
for(int j = 0; j < ropes.size(); j++) {
if(h_dis(players[i], ropes[j]) > mid) {
continue;
}
edge_id.emplace_back(i, j, g.e.size());
g.addEdge(i, j + players.size(), 1, e_dis(players[i], ropes[j]));
}
}
int s = players.size() * 2;
int t = s + 1;
for(int i = 0; i < players.size(); i++) {
g.addEdge(s, i, 1, 0);
g.addEdge(i + players.size(), t, 1, 0);
}
auto [flow, cost] = g.flow(s, t);
if(flow != players.size()) {
return false;
} else {
ans.clear();
for(auto &[u, v, id] : edge_id) {
if(g.e[id].c == 0) {
ans.emplace_back(u, v);
}
}
return true;
}
}
int main() {
std::ios::sync_with_stdio(false);
std::cin.tie(nullptr);
int n, a, b;
cin >> n >> a >> b;
players.resize(n);
ropes.resize(n);
for(auto &[x, y] : players) {
cin >> x >> y;
}
for(auto &[x, y] : ropes) {
cin >> x >> y;
}
std::vector<std::pair<int, int>> match;
int l = 0, r = a + b;
while(l < r) {
int mid = (l + r) >> 1;
if(check(mid, match)) {
r = mid;
} else {
l = mid + 1;
}
}
cout << l << std::endl;
for(auto [u, v] : match) {
auto[x, y] = players[u];
auto[a, b] = ropes[v];
std::cout << x << ' ' << y << ' '
<< a << ' ' << b << ' ';
while(x!=a || y!=b){
if(x > a) {
cout << 'U';
x--;
continue;
}
if(y > b) {
cout << 'L';
y--;
continue;
}
if(x < a) {
cout << 'D';
x++;
continue;
}
if(y < b) {
cout << 'R';
y++;
continue;
}
}
cout << '\n';
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 3828kb
input:
3 4 4 1 1 1 4 4 4 1 3 2 3 2 4
output:
2 1 1 1 3 RR 1 4 2 3 LD 4 4 2 4 UU
result:
ok answer 2
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3664kb
input:
3 2 2 1 1 1 2 2 2 1 1 2 1 2 2
output:
1 1 1 2 1 D 1 2 1 1 L 2 2 2 2
result:
wrong output format Unexpected end of file - token expected