洛谷 P4012 深海機器人問題【費用流】

題目鏈接:https://www.luogu.org/problemnew/show/P4012

洛谷 P4012 深海機器人問題

輸入輸出樣例

輸入樣例#1:
1 1
2 2
1 2
3 4
5 6
7 2
8 10
9 3
2 0 0
2 2 2
輸出樣例#1:
42

說明

題解:建圖方法如下:

  對于矩陣中的每個點,向東、向北分別與其相鄰點都要連兩條邊(重邊):

    1)容量為1,費用為該邊價值的邊;

    2)容量為INF,費用為0的邊(因為多個深海機器人可以在同一時間占據同一位置)。

  對于每個起點:從S(源點)到這個點連:容量為該點機器人數,費用為0的邊。

  對于每個終點:從這個點到T(匯點)連:容量為該點機器人數,費用為0的邊。

?

代碼:

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 const int N = 455;
  5 const int M = N*4+30;
  6 const int INF = 0x3f3f3f3f;
  7 struct Edge { int to,next,cap,flow,cost; }edge[M];
  8 int head[N],tol;
  9 int pre[N],dis[N];
 10 bool vis[N];
 11 int V;      
 12 void init(int n) {
 13     V = n;
 14     tol = 0;
 15     memset(head,-1,sizeof(head));
 16 }
 17 void addedge(int u,int v,int cap,int cost) {
 18     edge[tol].to = v; edge[tol].cap = cap; edge[tol].cost = cost; edge[tol].flow = 0; edge[tol].next = head[u]; head[u] = tol++;
 19     edge[tol].to = u; edge[tol].cap = 0; edge[tol].cost = -cost; edge[tol].flow = 0; edge[tol].next = head[v]; head[v] = tol++;
 20 }
 21 bool spfa(int s,int t) {
 22     queue<int>q;
 23     for(int i = 0;i < V;i++) {
 24         dis[i] = INF;
 25         vis[i] = false;
 26         pre[i] = -1;
 27     }
 28     dis[s] = 0;
 29     vis[s] = true;
 30     q.push(s);
 31     while(!q.empty()) {
 32         int u = q.front();
 33         q.pop();
 34         vis[u] = false;
 35         for(int i = head[u]; i != -1;i = edge[i].next) {
 36             int v = edge[i].to;
 37             if(edge[i].cap > edge[i].flow && dis[v] > dis[u] + edge[i].cost ) {
 38                 dis[v] = dis[u] + edge[i].cost;
 39                 pre[v] = i;
 40                 if(!vis[v]) {
 41                     vis[v] = true;
 42                     q.push(v);
 43                 }
 44             }
 45         }
 46     }
 47     if(pre[t] == -1) return false;
 48     else return true;
 49 }
 50 int minCostMaxflow(int s,int t,int &cost) {
 51     int flow = 0;
 52     cost = 0;
 53     while(spfa(s,t)) {
 54         int Min = INF;
 55         for(int i = pre[t];i != -1;i = pre[edge[i^1].to]) {
 56             if(Min > edge[i].cap - edge[i].flow)
 57                 Min = edge[i].cap - edge[i].flow;
 58         }
 59         for(int i = pre[t];i != -1;i = pre[edge[i^1].to]) {
 60             edge[i].flow += Min;
 61             edge[i^1].flow -= Min;
 62             cost += edge[i].cost * Min;
 63         }
 64         flow += Min;
 65     }
 66     return flow;
 67 }
 68 int main() {
 69     int a, b, p, q, k, x, y, i, j, ans = 0;
 70     scanf("%d%d", &a, &b);//出發和目的地數目
 71     scanf("%d%d", &p, &q);
 72     init((p+1)*(q+1)+3);
 73 
 74     int s = (p+1)*(q+1)+1, t = (p+1)*(q+1)+2;
 75 
 76     for(i = 0; i <= p; ++i) {//p+1行,向東移動
 77         for(j = 0; j < q; ++j) {
 78             scanf("%d", &x);//邊上的標本價值
 79             addedge(i*(q+1)+j, i*(q+1)+j+1, 1, -x);
 80             addedge(i*(q+1)+j, i*(q+1)+j+1, INF, 0);
 81         }
 82     }
 83     for(j = 0; j <= q; ++j) {//q+1列,向北移動
 84         for(i = 0; i < p; ++i) {
 85             scanf("%d", &x);
 86             addedge(i*(q+1)+j, i*(q+1)+j+q+1, 1, -x);
 87             addedge(i*(q+1)+j, i*(q+1)+j+q+1, INF, 0);
 88         }
 89     }
 90     for(i = 1; i <= a; ++i) {//起點
 91         scanf("%d%d%d", &k, &x, &y);
 92         addedge(s, x*(q+1)+y, k, 0);
 93     }
 94     for(i = 1; i <= b; ++i) {//終點
 95         scanf("%d%d%d", &k, &x, &y);
 96         addedge(x*(q+1)+y, t, k, 0);
 97     }
 98     minCostMaxflow(s, t, ans);
 99     printf("%d\n", -ans);
100     return 0;
101 }
View Code

?

轉載于:https://www.cnblogs.com/GraceSkyer/p/9038586.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/389651.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/389651.shtml
英文地址,請注明出處:http://en.pswp.cn/news/389651.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

day5 模擬用戶登錄

_user "yangtuo" _passwd "123456"# passd_authentication False #flag 標志位for i in range(3): #for 語句后面可以跟else&#xff0c;但是不能跟elifusername input("Username:")password input("Password:")if username _use…

opencv實現對象跟蹤_如何使用opencv跟蹤對象的距離和角度

opencv實現對象跟蹤介紹 (Introduction) Tracking the distance and angle of an object has many practical uses, especially in robotics. This tutorial explains how to get an accurate distance and angle measurement, even when the target is at a strong angle from…

spring cloud 入門系列七:基于Git存儲的分布式配置中心--Spring Cloud Config

我們前面接觸到的spring cloud組件都是基于Netflix的組件進行實現的&#xff0c;這次我們來看下spring cloud 團隊自己創建的一個全新項目&#xff1a;Spring Cloud Config.它用來為分布式系統中的基礎設施和微服務提供集中化的外部配置支持&#xff0c;分為服務端和客戶端兩個…

458. 可憐的小豬

458. 可憐的小豬 有 buckets 桶液體&#xff0c;其中 正好 有一桶含有毒藥&#xff0c;其余裝的都是水。它們從外觀看起來都一樣。為了弄清楚哪只水桶含有毒藥&#xff0c;你可以喂一些豬喝&#xff0c;通過觀察豬是否會死進行判斷。不幸的是&#xff0c;你只有 minutesToTest…

熊貓數據集_大熊貓數據框的5個基本操作

熊貓數據集Tips and Tricks for Data Science數據科學技巧與竅門 Pandas is a powerful and easy-to-use software library written in the Python programming language, and is used for data manipulation and analysis.Pandas是使用Python編程語言編寫的功能強大且易于使用…

圖嵌入綜述 (arxiv 1709.07604) 譯文五、六、七

應用 圖嵌入有益于各種圖分析應用&#xff0c;因為向量表示可以在時間和空間上高效處理。 在本節中&#xff0c;我們將圖嵌入的應用分類為節點相關&#xff0c;邊相關和圖相關。 節點相關應用 節點分類 節點分類是基于從標記節點習得的規則&#xff0c;為圖中的每個節點分配類標…

聊聊自動化測試框架

無論是在自動化測試實踐&#xff0c;還是日常交流中&#xff0c;經常聽到一個詞&#xff1a;框架。之前學習自動化測試的過程中&#xff0c;一直對“框架”這個詞知其然不知其所以然。 最近看了很多自動化相關的資料&#xff0c;加上自己的一些實踐&#xff0c;算是對“框架”有…

1971. Find if Path Exists in Graph

1971. Find if Path Exists in Graph 有一個具有 n個頂點的 雙向 圖&#xff0c;其中每個頂點標記從 0 到 n - 1&#xff08;包含 0 和 n - 1&#xff09;。圖中的邊用一個二維整數數組 edges 表示&#xff0c;其中 edges[i] [ui, vi] 表示頂點 ui 和頂點 vi 之間的雙向邊。 …

移動磁盤文件或目錄損壞且無法讀取資料如何找回

文件或目錄損壞且無法讀取說明這個盤的文件系統結構損壞了。在平時如果數據不重要&#xff0c;那么可以直接格式化就能用了。但是有的時候里面的數據很重要&#xff0c;那么就必須先恢復出數據再格式化。具體恢復方法可以看正文了解&#xff08;不格式化的恢復方法&#xff09;…

python 平滑時間序列_時間序列平滑以實現更好的聚類

python 平滑時間序列In time series analysis, the presence of dirty and messy data can alter our reasonings and conclusions. This is true, especially in this domain, because the temporal dependency plays a crucial role when dealing with temporal sequences.在…

基于SmartQQ協議的QQ自動回復機器人-1

0. 本項目的原始代碼及我二次開發后的代碼 1. 軟件安裝:【myeclipse6.0 maven2】 0. https://blog.csdn.net/zgmzyr/article/details/6886440 1. https://blog.csdn.net/shuzhe66/article/details/45009175 2. https://www.cnblogs.com/whgk/p/7112560.html<mirror><…

1725. 可以形成最大正方形的矩形數目

1725. 可以形成最大正方形的矩形數目 給你一個數組 rectangles &#xff0c;其中 rectangles[i] [li, wi] 表示第 i 個矩形的長度為 li 、寬度為 wi 。 如果存在 k 同時滿足 k < li 和 k < wi &#xff0c;就可以將第 i 個矩形切成邊長為 k 的正方形。例如&#xff0c…

幫助學生改善學習方法_學生應該如何花費時間改善自己的幸福

幫助學生改善學習方法There have been numerous studies looking into the relationship between sleep, exercise, leisure, studying and happiness. The results were often quite like how we expected, though there have been debates about the relationship between sl…

Spring Boot 靜態資源訪問原理解析

一、前言 springboot配置靜態資源方式是多種多樣&#xff0c;接下來我會介紹其中幾種方式&#xff0c;并解析一下其中的原理。 二、使用properties屬性進行配置 應該說 spring.mvc.static-path-pattern 和 spring.resources.static-locations這兩屬性是成對使用的&#xff0c;如…

深挖“窄帶高清”的實現原理

過去幾年&#xff0c;又拍云一直在點播、直播等視頻應用方面潛心鉆研&#xff0c;取得了不俗的成果。我們結合點播、直播、短視頻等業務中的用戶場景&#xff0c;推出了“省帶寬、壓成本”系列文章&#xff0c;從編碼技術、網絡架構等角度出發&#xff0c;結合又拍云的產品成果…

學習總結5 - bootstrap學習記錄1__安裝

1.bootstrap是什么&#xff1f; 簡潔、直觀、強悍的前端開發框架&#xff0c;說白了就是給后端二把刀開發網頁用的&#xff0c;讓web開發更迅速、簡單。 復制代碼 2.如何使用&#xff1f; 如圖所示到bootstrap中文網進行下載 復制代碼 下載完成之后&#xff0c;如圖所示&#x…

519. 隨機翻轉矩陣

519. 隨機翻轉矩陣 給你一個 m x n 的二元矩陣 matrix &#xff0c;且所有值被初始化為 0 。請你設計一個算法&#xff0c;隨機選取一個滿足 matrix[i][j] 0 的下標 (i, j) &#xff0c;并將它的值變為 1 。所有滿足 matrix[i][j] 0 的下標 (i, j) 被選取的概率應當均等。 …

模型的搜索和優化方法綜述:

一、常用的優化方法&#xff1a; 1.爬山 2.最陡峭下降 3.期望最大值 二、常用的搜索方法&#xff1a; 1.貪婪搜索 2.分支界定 3.寬度&#xff08;深度&#xff09;優先遍歷轉載于:https://www.cnblogs.com/xyp666/p/9042143.html

Redis 服務安裝

下載 客戶端可視化工具: RedisDesktopManager redis官網下載: http://redis.io/download windos服務安裝 windows服務安裝/卸載下載文件并解壓使用 管理員身份 運行命令行并且切換到解壓目錄執行 redis-service --service-install windowsR 打開運行窗口, 輸入 services.msc 查…

熊貓數據集_對熊貓數據框使用邏輯比較

熊貓數據集P (tPYTHON) Logical comparisons are used everywhere.邏輯比較隨處可見 。 The Pandas library gives you a lot of different ways that you can compare a DataFrame or Series to other Pandas objects, lists, scalar values, and more. The traditional comp…