Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define pb push_back
- #define mp make_pair
- #define sz(x) (int)(x).size()
- #define li long long
- #define ld long double
- #define x first
- #define y second
- #define pt pair<int, int>
- #define pll pair<ll, ll>
- #define forn(i, t) for(int i = 0; i < (t); i++)
- #define fore(i, f, t) for(int i = (f); i < (t); i++)
- #define forr(i, f, t) for(int i = (f) - 1; i >= (t); i--)
- #define all(x) (x).begin(), (x).end()
- #define ins insert
- using namespace std;
- const int INF = 1e9;
- const int MOD = 1000000007;
- const li INF64 = 1e18;
- const ld EPS = 1e-7;
- mt19937 myrand(time(NULL));
- const int N = 213;
- int n;
- int d[N][N], f[N][N], p[N];
- bool used[N];
- bool read(){
- if(scanf("%d", &n) != 1)
- return 0;
- forn(i, n)
- forn(j, n)
- scanf("%d", &d[i][j]);
- return 1;
- }
- int bfs(){
- queue<int> q;
- q.push(0);
- used[0] = 1;
- int dt = INF;
- bool fl;
- while (!q.empty()){
- int v = q.front();
- q.pop();
- fl = 0;
- forn(u, n)
- if (!used[u] && f[v][u] < d[v][u]){
- used[u] = 1;
- p[u] = v;
- q.push(u);
- if (u == n - 1){
- fl = 1;
- break;
- }
- }
- if (fl)
- break;
- }
- if (q.empty())
- return 0;
- int cur;
- cur = n - 1;
- while (cur != 0){
- dt = min(dt, d[p[cur]][cur] - f[p[cur]][cur]);
- cur = p[cur];
- }
- cur = n - 1;
- while (cur != 0){
- f[p[cur]][cur] += dt;
- f[cur][p[cur]] -= dt;
- cur = p[cur];
- }
- return dt;
- }
- void solve(){
- int res;
- memset(f, 0, sizeof(f));
- do{
- memset(used, 0, sizeof(used));
- res = bfs();
- } while(res > 0);
- li sum = 0;
- forn(i, n)
- sum += f[0][i];
- printf("%lld\n", sum);
- }
- int main(){
- #ifdef _DEBUG
- freopen("input.txt", "r", stdin);
- #endif
- while(read())
- solve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement