QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#498923 | #7185. Poor Students | user_000 | TL | 123ms | 7704kb | C++14 | 7.3kb | 2024-07-30 21:41:08 | 2024-07-30 21:41:08 |
Judging History
answer
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int , int > ii;
// Stores the found edges
vector<bool> found;
// Stores the number of nodes
int N = 0;
// Stores the capacity of each edge
vector<vector<int>> cap;
vector<vector<int>> flow;
// Stores the cost per unit flow of each edge
vector<vector<int>> cost;
// Stores the distance from each node
// and picked edges for each node
vector<int> dad, dist, pi;
const int INF = INT_MAX / 2 - 1;
int n,m;
bool minimize(int &x, int y){if (x>y){x=y;return 1;}return 0;}
// Function to check if it is possible to
// have a flow from the src to sink
bool search(int src, int sink) {
// Initialise found[] to false
found = vector<bool>(N + 1, false);
// Initialise the dist[] to INF
dist = vector<int>(N + 1, INF);
// Distance from the source node
dist[src] = 0;
priority_queue< ii,vector<ii> , greater<ii> > pq;
pq.push(ii(dist[src],src));
// Iterate until src reaches N
while (!pq.empty())
{
int srcu = pq.top().se;
pq.pop();
//cout<<srcu<<" ";
if (found[srcu])
{
//cout<<"\n";
continue;
}
//cout<<srcu<<" : "<<dist[srcu]<<endl;
found[srcu] = true;
int u;
if (srcu<=n)
u = srcu;
else
u = srcu - n;
if (srcu == n+m+1)
break;
if (srcu==0)
{
for (int k = 1; k <=n; k++)
{
if (found[k])
continue;
if (flow[k][u] < cap[k][u])
{
if (minimize(dist[k] , dist[srcu] + pi[srcu] - pi[k] + cost[k][u]))
{
dad[k] = srcu;
pq.push(ii(dist[k],k));
}
}
}
}
else if (srcu<=n)
{
for (int k = 1; k <= m; k++)
{
if (found[n+k])
continue;
if (flow[u][k] < cap[u][k])
{
if (minimize(dist[n+k] , dist[u] + pi[u] - pi[n+k] + cost[u][k]))
{
dad[n+k] = srcu;
pq.push(ii(dist[n+k],n+k));
}
}
}
}
else
{
int k = 0;
if (flow[0][u] < cap[0][u])
{
if (minimize(dist[n+m+1] , dist[srcu] + pi[srcu] - pi[n+m+1] + cost[0][u]))
{
dad[n+m+1] = srcu;
pq.push(ii(dist[n+m+1],n+m+1));
}
}
for (k=1;k<=n;k++)
{
if (found[k])
continue;
if (flow[k][u] != 0)
{
if (minimize(dist[k] , dist[srcu] + pi[srcu] - pi[k] - cost[k][u]))
{
dad[k] = srcu;
pq.push(ii(dist[k],k));
}
}
}
}
}
for (int k = 0; k < N; k++)
if (dist[k] + pi[k] > INF)
pi[k] = INF;
else
pi[k] = pi[k] + dist[k];
return found[sink];
}
// Function to obtain the maximum Flow
int getMaxFlow(vector<vector<int>>& capi,
vector<vector<int>>& costi,
int src, int sink) {
cap = capi;
cost = costi;
N = n+m+2;
found = vector<bool>(N + 2, false);
flow.assign(n+3, vector<int>(n+3, 0));
dist = vector<int>(N + 2, 0);
dad = vector<int>(N + 2, 0);
pi = vector<int>(N + 2, 0);
int totflow = 0, totcost = 0;
// If a path exists from src to sink
while (search(src, sink)) {
// Set the default amount
int amt = INF;
int x = sink;
while (x != src) {
int u = dad[x];
int v = x;
if (u>n)
u-=n;
if (v>n)
v-=n;
// cout<<dad[x]<<" -> "<<x<<endl;
if (u==0)
{
//cout<<cap[v][0] - flow[v][0]<<" 1u ";
min(amt, cap[v][0] - flow[v][0]);
}
else if (v==0)
{
// cout<<flow[u][0]<<" 2u ";
minimize(amt , flow[u][0]);
}
else if (x==(n+m+1))
{
// cout<<cap[0][u] - flow[0][u] << " 3u ";
minimize(amt , cap[0][u] - flow[0][u]);
}
else if (u==(n+m+1))
{
// cout<< flow[0][v] <<" 4u ";
minimize(amt, flow[0][v]);
}
else
{
//cout<< (flow[v][u] != 0 ? flow[v][u] : cap[u][v] - flow[u][v])<<" 5u "<<endl;
if (x>n)
minimize( amt, cap[u][v] - flow[u][v]);
else
minimize (amt , flow[v][u] );
}
//cout<<endl;
x = dad[x];
}
//cout<<" -> "<<amt<<endl;
// int z;
// cin>>z;
x = sink;
while (x != src) {
int u = dad[x];
int v = x;
if (u>n)
u-=n;
if (v>n)
v-=n;
// cout<<u<<" -> "<<v<<endl;
if (u==0)
{
flow[v][0] += amt;
totcost += amt * cost[v][0];
}
else if (v == 0)
{
flow[u][0] -= amt;
totcost -= amt * cost[u][0];
}
else if (x==(n+m+1))
{
// cout<<"..";
flow[0][u] += amt;
totcost += amt * cost[0][u];
}
else if (dad[x]==(n+m+1))
{
flow[0][v] -= amt;
totcost -= amt * cost[0][v];
}
else
{
// cout<<u<<" -> "<<v<<endl;
if (x<=n)
{
flow[v][u] -= amt;
totcost -= amt * cost[v][u];
}
else
{
flow[u][v] += amt;
totcost += amt * cost[u][v];
}
}
x = dad[x];
}
// cout<<flow[0][1]<<endl<<endl;
//cout<<"oke";
}
// cout<<totflow<<" : ";
// Return pair total cost and sink
return totcost;
}
// Driver Code
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
vector<vector<int>> cap(n+3, vector<int>(m+3));
vector<vector<int>> cost(n+3, vector<int>(m+3));
for (int i=0;i<=n;i++)
for (int j=0;j<=m;j++)
{
cap[i][j] = 0;
cost[i][j] = 0;
}
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
cin>>cost[i][j], cap[i][j] = 1;
for (int i=1 ;i<=n;i++)
cap[i][0] = 1, cost[i][0] = 0;
for (int i=1;i<=m;i++)
{
int x;
cin >> x;
cap[0][i] = x ;
cost[0][i] = 0;
}
int s = 0, t = n+m+1;
int ret = getMaxFlow(cap, cost, s, t);
// cout<< ret[0] << " ";
cout << ret << endl;
return 0;
}
// by phasing17
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3816kb
input:
6 2 1 2 1 3 1 4 1 5 1 6 1 7 3 4
output:
12
result:
ok answer is '12'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
3 3 1 2 3 2 4 6 6 5 4 1 1 1
output:
8
result:
ok answer is '8'
Test #3:
score: 0
Accepted
time: 123ms
memory: 7704kb
input:
1000 10 734 303 991 681 755 155 300 483 702 442 237 256 299 675 671 757 112 853 759 233 979 340 288 377 718 199 935 666 576 842 537 363 592 349 494 961 864 727 84 813 340 78 600 492 118 421 478 925 552 617 517 589 716 7 928 638 258 297 706 787 266 746 913 978 436 859 701 951 137 44 815 336 471 720 2...
output:
92039
result:
ok answer is '92039'
Test #4:
score: -100
Time Limit Exceeded
input:
5000 10 14 114 254 832 38 904 25 147 998 785 917 694 750 372 379 887 247 817 999 117 802 15 799 515 316 42 69 247 95 144 727 398 509 725 682 456 369 656 693 955 923 1 681 631 962 826 233 963 289 856 165 491 488 832 111 950 853 791 929 240 509 843 667 970 469 260 447 477 161 431 514 903 627 236 144 3...
output:
461878