QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#318050 | #7185. Poor Students | Minhho | TL | 0ms | 0kb | C++17 | 2.6kb | 2024-01-30 12:13:03 | 2024-01-30 12:13:03 |
answer
#define taskname "MCMF"
#include <bits/stdc++.h>
#define int long long
#define ii pair<int,int>
#define ff first
#define ss second
using namespace std;
const int maxn = 100005;
vector<int> adj[maxn];
int d[maxn], cur[maxn], cost, n, m, k, s, t, cnt;
struct edge {
int u, v, f, c, w;
} e[maxn*100];
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
inline void add(int u, int v, int c, int w)
{
// cout<<"ADD: "<<u<<" "<<v<<" "<<c<<" "<<w<<"\n";
adj[u].emplace_back(cnt);
e[cnt++] = {u, v, 0, c, w};
adj[v].emplace_back(cnt);
e[cnt++] = {v, u, 0, 0, -w};
}
inline bool BFS()
{
fill(d+s, d+t+1, 1e18);
d[s] = 0;
priority_queue<ii, vector<ii>, greater<ii>> pq;
pq.emplace(0, s);
while (pq.size())
{
auto [du, u] = pq.top(); pq.pop();
for (int id: adj[u])
{
auto [_, v, f, c, w] = e[id];
if (f < c && cost >= w && d[v] > d[u] + w)
{
d[v] = d[u] + w;
pq.emplace(d[v], v);
}
}
}
return (d[t] != 1e18);
}
int DFS(int u, int flow)
{
if (u == t) return flow;
for (int &i=cur[u]; i<adj[u].size();)
{
int id = adj[u][i++];
auto [_, v, f, c, w] = e[id];
if (f < c && d[v] == d[u] + w)
{
int x = DFS(v, min(flow, c - f));
if (x)
{
e[id].f += x;
e[id^1].f -= x;
return x;
}
}
}
return 0;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
if (fopen("input.in", "r")) {
freopen("input.in", "r", stdin);
freopen("output.out", "w", stdout);
}
cin>>n>>k;
assert(n <= 1e4);
s = 0, t = n + k + 1;
for (int i=1, u, v, c, w; i<=n; i++)
{
add(0, i, 1, 0);
u = i;
vector<ii> ve;
for (v=n+1; v<=n+k; v++)
{
cin>>w;
ve.emplace_back(w, v);
}
shuffle(ve.begin(), ve.end(), rng);
for (auto [w, v]: ve) add(u, v, 1, w);
}
for (int i=1; i<=k; i++)
{
int c; cin>>c;
if (c) add(n+i, t, c, 0);
}
int ans = 0, need = n;
for (cost=1; ; cost <<= 1)
{
while (BFS() && need)
{
fill(cur+s, cur+t+1, 0);
int x;
while (x = DFS(s, need))
{
ans += x * d[t];
need -= x;
if (!need) break;
}
}
}
cout<<ans;
}
详细
Test #1:
score: 0
Time Limit Exceeded
input:
6 2 1 2 1 3 1 4 1 5 1 6 1 7 3 4