This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
// try for O(N^3) and pray it works
const int N = 507;
int n, m, r, t, k;
int p[N], assign[N], pr[N];
bool vis[N];
set<int> nxt[N];
vector<int> adj[2*N];
int dfs(int v, int src) {
vis[v] = 1;
if (nxt[v].size() + 1 < nxt[src].size())
return v;
for (int w : nxt[v]) {
for (int u : adj[w+N]) {
if (u != v && !vis[u] && nxt[v].size() >= nxt[u].size()) {
pr[u] = w;
int ans = dfs(u,src);
if (ans>=0)
return ans;
}
}
}
return -1;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n>>m>>r>>t>>k;
memset(assign, -1, sizeof(assign));
for (int i = 0; i < k; i++) {
int a, b; cin>>a>>b;
adj[--a].push_back(--b+N);
adj[N+b].push_back(a);
if (assign[b] == -1) {
assign[b] = a;
nxt[a].insert(b);
}
}
for (int i = 0; i < n; i++)
p[i] = i;
while (1) {
sort(p, p+n, [](int i, int j) {
return adj[i].size() > adj[j].size();
});
memset(vis, 0, sizeof(vis));
memset(pr, -1, sizeof(pr));
bool found = 0;
for (int v = 0; v < n; v++) {
int w = dfs(v, v);
if (w != -1) {
while (w != v) {
// flip edges from w to assign[pr[w]]
int task = pr[w];
nxt[w].insert(task);
nxt[assign[pr[w]]].erase(task);
swap(assign[pr[w]], w);
}
found = 1;
break;
}
}
if (!found)
break;
}
stringstream ss;
long long ans = 0;
int actual_cnt = 0;
for (int i = 0; i < n; i++) {
int cnt = 0;
for (int x : nxt[i]) {
if (cnt < t/r) {
ss << i+1 << " " << x+1 << " " << cnt++*r << endl;
ans += cnt*r;
}
}
actual_cnt++;
}
cout << actual_cnt << " " << ans << endl;
cout << ss.str();
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... |
# | 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... |