loj#6073. 「2017 山東一輪集訓 Day5」距離(費用流)

来源:https://www.cnblogs.com/zwfymqz/archive/2019/03/29/10619039.html
-Advertisement-
Play Games

題意 "題目鏈接" Sol 我們可以把圖行列拆開,同時對於行/列拆成很多個聯通塊,然後考慮每個點所在的行聯通塊/列聯通塊的貢獻。 可以這樣建邊 從S向每個行聯通塊連聯通塊大小條邊,每條邊的容量為1,費用為$i$(i表示這是第幾條邊)。 從每個點所在的行聯通塊向列聯通塊連邊,容量為1,費用為0 從每個 ...


題意

題目鏈接

Sol

我們可以把圖行列拆開,同時對於行/列拆成很多個聯通塊,然後考慮每個點所在的行聯通塊/列聯通塊的貢獻。

可以這樣建邊

從S向每個行聯通塊連聯通塊大小條邊,每條邊的容量為1,費用為\(i\)(i表示這是第幾條邊)。

從每個點所在的行聯通塊向列聯通塊連邊,容量為1,費用為0

從每個列聯通塊向T連聯通塊大小條邊,每條邊的容量為1,費用為\(i\)(i表示這是第幾條邊)。

這樣跑最小費用最大流,每增光一次的費用就是答案。預處理後O(1)回答即可

#include<bits/stdc++.h> 
#define Pair pair<int, int>
#define MP(x, y) make_pair(x, y)
#define fi first
#define se second
//#define int long long 
#define LL long long 
#define ull unsigned long long 
#define Fin(x) {freopen(#x".in","r",stdin);}
#define Fout(x) {freopen(#x".out","w",stdout);}
using namespace std;
const int MAXN = 5001, mod = 1e9 + 7, INF = 1e9 + 10;
const double eps = 1e-9;
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline LL add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline LL mul(A x, B y) {return 1ll * x * y % mod;}
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;}
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline LL sqr(A x){return 1ll * x * x;}
template <typename A, typename B> inline LL fp(A a, B p, int md = mod) {int b = 1;while(p) {if(p & 1) b = mul(b, a);a = mul(a, a); p >>= 1;}return b;}
template <typename A> A inv(A x) {return fp(x, mod - 2);}
inline int read() {
    char c = getchar(); int x = 0, f = 1;
    while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
    while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
    return x * f;
}

int N, S, T , TT;
char s[51][51];
int id[51][51][2], c1 = 1, c2 = 1, ans[MAXN * MAXN], tot1[20 * MAXN], tot2[20 * MAXN ], num1, num2;
struct Edge {
    int u, v, w, f, nxt;
}E[2 * MAXN * MAXN];
int head[MAXN  * 20 + 1], num;
void add_edge(int x, int y, int w, int f) {
    E[num] = (Edge){x, y, w, f, head[x]};
    head[x] = num++;
}
void AddEdge(int x, int y, int w, int f) {
    //printf("%d %d %d %d\n", x, y, w, f);
    add_edge(x, y, w, f);
    add_edge(y, x, -w, 0);
}
int dis[MAXN * 10], vis[MAXN * 10], pre[MAXN * 10];
int SPFA() {
    memset(dis, 0x3f, sizeof(dis));
    memset(vis, 0, sizeof(vis));
    queue<int> q; q.push(S); dis[S] = 0;
    while(!q.empty()) {
        int p = q.front(); q.pop(); vis[p] = 0;
        for(int i = head[p]; ~i; i = E[i].nxt) {
            int to = E[i].v, w = E[i].w;
            if(dis[to] > dis[p] + w && E[i].f) {
                dis[to] = dis[p] + w; pre[to] = i;
                if(!vis[to]) vis[to] = 1, q.push(to);
            }
        }
    }
    return dis[TT];
}
int MCMF() {
    int val = SPFA(), dec = INF;
    for(int k = TT; k != S; k = E[pre[k]].u) chmin(dec, E[pre[k]].f);
    for(int k = TT; k != S; k = E[pre[k]].u) E[pre[k]].f -= dec, E[pre[k] ^ 1].f += dec;
    return dec * val;
}
signed main() {
    //freopen("a.in", "r", stdin);
    memset(head, -1, sizeof(head));
    N = read(); S = 0; T = N * N * 10, TT = T + 1; c2 = N * N * 3 + 1;
    for(int i = 1; i <= N; i++) scanf("%s", s[i] + 1);
    for(int i = 1; i <= N; i++) {
        for(int j = 1; j <= N; j++) {
            if(s[i][j] == '#') tot1[c1] = num1, num1 = 0, c1++; 
            else id[i][j][0] = c1, num1++;
            if(s[j][i] == '#') tot2[c2] = num2, num2 = 0, c2++;
            else id[j][i][1] = c2, num2++;
        }
        if(num1) tot1[c1++] = num1, num1 = 0;
        if(num2) tot2[c2++] = num2, num2 = 0;
    }
    for(int i = 1; i <= N; i++)
        for(int j = 1; j <= N; j++)
            if(id[i][j][0] && id[i][j][1])
                AddEdge(id[i][j][0], id[i][j][1], 0, 1);
    for(int i = 1; i <= c1; i++) 
        for(int j = 0; j < tot1[i]; j++)
            AddEdge(S, i, j, 1);
    for(int i = N * N * 3 + 1; i <= c2; i++)
        for(int j = 0; j < tot2[i]; j++)
            AddEdge(i, T, j, 1);
    for(int i = 1; i <= 2 * N * N; i++)
        AddEdge(T, TT, 0, 1);
    for(int i = 1; i <= N * N; i++)
        ans[i] = ans[i - 1] + MCMF();
    int Q = read();
    while(Q--) cout << ans[read()] << '\n';
    return 0;
}

您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 前言 前文介紹過用Python寫爬蟲,但是當任務多的時候就比較慢, 這是由於Python自帶的http庫urllib2發起的http請求是阻塞式的,這意味著如果採用單線程模型,那麼整個進程的大部分時間都阻塞在等待服務端把數據傳輸過來的過程中。所以我們這次嘗試用node.js去做這個爬蟲。 為什麼選擇 ...
  • Document 電腦算數誤差: 0.1+0.2 = 0.30000000000000004 17.45*3*0.9 = 47.114999999999995 17.45*0.9*3 = 47.115 用math進行計算,避免誤差,見下方js ... ...
  • **vue可視化圖表 基於Echarts封裝好的v-charts** 近期公司又一個新的需求,要做一個訂單和銷售額統計的項目,需要用到可視化圖表來更直觀的展示數據。首先我想到的是Echarts,眾所周知Echarts是一個應用很廣的可視化圖表庫,用來展示統計數據更合適不過,但是偶然間發現了一個更為方 ...
  • 一、寫在前面 首先呢,由於之前重裝系統,又要重新配置環境,然後還有一些別的事,導致我一直沒有寫爬蟲了,不過現在又可以繼續寫了。 然後我這次說的模擬登錄新浪微博呢,不是使用Selenium模擬瀏覽器操作,畢竟Selenium的效率是真的有些低,所以我選擇用Python發送請求實現模擬登錄,整個過程還算 ...
  • import requests from bs4 import BeautifulSoup from math import ceil header = { 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (... ...
  • 一般公司的項目一般都是用Oracle、Mysql、SQL Server等一些國外的資料庫。前段時間公司做了一個國家政府保密單位的項目,別人要求用國產資料庫,所以研究了下,最後決定用神舟通用的,其實國產也很好幾家做資料庫的還不錯,下邊簡單總結了下,以供參考 1:南大通用 公司簡介 天津南大通用數據技術 ...
  • 題目如下。解題步驟參考的是https://cloud.tencent.com/developer/news/373865中作者的思路。 1.首先,兩個四位數相加等於一個五位數,那麼這個五位數的第一位必定是1,也就是“三”=1,。 2.繼續分析“祥”+“三”,若是“祥”(8),“三”為1,那麼低位必定 ...
  • JDBC的使用流程,通過JDBC進行對資料庫增刪改查的操作及代碼封裝。 ...
一周排行
    -Advertisement-
    Play Games
  • 示例項目結構 在 Visual Studio 中創建一個 WinForms 應用程式後,項目結構如下所示: MyWinFormsApp/ │ ├───Properties/ │ └───Settings.settings │ ├───bin/ │ ├───Debug/ │ └───Release/ ...
  • [STAThread] 特性用於需要與 COM 組件交互的應用程式,尤其是依賴單線程模型(如 Windows Forms 應用程式)的組件。在 STA 模式下,線程擁有自己的消息迴圈,這對於處理用戶界面和某些 COM 組件是必要的。 [STAThread] static void Main(stri ...
  • 在WinForm中使用全局異常捕獲處理 在WinForm應用程式中,全局異常捕獲是確保程式穩定性的關鍵。通過在Program類的Main方法中設置全局異常處理,可以有效地捕獲並處理未預見的異常,從而避免程式崩潰。 註冊全局異常事件 [STAThread] static void Main() { / ...
  • 前言 給大家推薦一款開源的 Winform 控制項庫,可以幫助我們開發更加美觀、漂亮的 WinForm 界面。 項目介紹 SunnyUI.NET 是一個基於 .NET Framework 4.0+、.NET 6、.NET 7 和 .NET 8 的 WinForm 開源控制項庫,同時也提供了工具類庫、擴展 ...
  • 說明 該文章是屬於OverallAuth2.0系列文章,每周更新一篇該系列文章(從0到1完成系統開發)。 該系統文章,我會儘量說的非常詳細,做到不管新手、老手都能看懂。 說明:OverallAuth2.0 是一個簡單、易懂、功能強大的許可權+可視化流程管理系統。 有興趣的朋友,請關註我吧(*^▽^*) ...
  • 一、下載安裝 1.下載git 必須先下載並安裝git,再TortoiseGit下載安裝 git安裝參考教程:https://blog.csdn.net/mukes/article/details/115693833 2.TortoiseGit下載與安裝 TortoiseGit,Git客戶端,32/6 ...
  • 前言 在項目開發過程中,理解數據結構和演算法如同掌握蓋房子的秘訣。演算法不僅能幫助我們編寫高效、優質的代碼,還能解決項目中遇到的各種難題。 給大家推薦一個支持C#的開源免費、新手友好的數據結構與演算法入門教程:Hello演算法。 項目介紹 《Hello Algo》是一本開源免費、新手友好的數據結構與演算法入門 ...
  • 1.生成單個Proto.bat內容 @rem Copyright 2016, Google Inc. @rem All rights reserved. @rem @rem Redistribution and use in source and binary forms, with or with ...
  • 一:背景 1. 講故事 前段時間有位朋友找到我,說他的窗體程式在客戶這邊出現了卡死,讓我幫忙看下怎麼回事?dump也生成了,既然有dump了那就上 windbg 分析吧。 二:WinDbg 分析 1. 為什麼會卡死 窗體程式的卡死,入口門檻很低,後續往下分析就不一定了,不管怎麼說先用 !clrsta ...
  • 前言 人工智慧時代,人臉識別技術已成為安全驗證、身份識別和用戶交互的關鍵工具。 給大家推薦一款.NET 開源提供了強大的人臉識別 API,工具不僅易於集成,還具備高效處理能力。 本文將介紹一款如何利用這些API,為我們的項目添加智能識別的亮點。 項目介紹 GitHub 上擁有 1.2k 星標的 C# ...