解題:2017清華集訓 無限之環

題面

費用流

把每種水管再拆出來四個方向的接頭,然后根據水管的形狀連出旋轉時的代價。最后黑白染色成二分圖,然后白點對應的接頭向黑點對應的接頭連邊,源點向白點自己連邊,黑點自己向匯點連邊。

怎么連邊?我是大力討論16種情況連的,應該是可以巧妙一點的=。=

洛谷不開O2會T,LOJ過了

  1 // luogu-judger-enable-o2
  2 #include<queue>
  3 #include<cstdio>
  4 #include<cstring>
  5 #include<algorithm>
  6 using namespace std;
  7 const int B=2005,N=10005,M=120005,inf=1e9;
  8 const int mov[4][2]={{-1,0},{0,1},{1,0},{0,-1}};
  9 int n,m,s,t,t1,t2,t3,t4,id,cnt,tot,sum,maxf,minc;
 10 int noww[2*M],goal[2*M],flow[2*M],cost[2*M];
 11 int p[N],mflw[N],mcst[N],queu[N],pren[N],pree[N];
 12 int mapp[B][B],idx[B][B],hed[B][B],tmp[5],hmap[5];
 13 queue<int> qs;
 14 void link(int f,int t,int v,int c,int x)
 15 {
 16     if(x) swap(f,t);
 17     noww[++cnt]=p[f],p[f]=cnt;
 18     goal[cnt]=t,flow[cnt]=v,cost[cnt]=c;
 19     noww[++cnt]=p[t],p[t]=cnt;
 20     goal[cnt]=f,flow[cnt]=0,cost[cnt]=-c;
 21 }
 22 int Bitcount(int x)
 23 {
 24     int ret=0;
 25     while(x)
 26         ret++,x-=x&-x;
 27     return ret; 
 28 }
 29 void Create(int x,int y,int z)
 30 {
 31     int typ=mapp[x][y],nde=idx[x][y];
 32     for(int i=1;i<=4;i++) tmp[i]=++tot;
 33     hed[x][y]=Bitcount(typ);
 34     if(!typ) 
 35         for(int i=1;i<=4;i++)
 36             link(nde,tmp[i],0,inf,z);
 37     else if(typ==1)
 38     {
 39         link(nde,tmp[1],1,0,z);
 40         link(tmp[1],tmp[2],1,1,z);
 41         link(tmp[1],tmp[3],1,2,z);
 42         link(tmp[1],tmp[4],1,1,z);
 43     } 
 44     else if(typ==2)
 45     {
 46         link(tmp[2],tmp[1],1,1,z);
 47         link(nde,tmp[2],1,0,z);
 48         link(tmp[2],tmp[3],1,1,z);
 49         link(tmp[2],tmp[4],1,2,z);
 50     } 
 51     else if(typ==3)
 52     {
 53         link(nde,tmp[1],1,0,z);
 54         link(nde,tmp[2],1,0,z);
 55         link(tmp[1],tmp[3],1,1,z);
 56         link(tmp[2],tmp[4],1,1,z);
 57     }
 58     else if(typ==4)
 59     {
 60         link(tmp[3],tmp[1],1,2,z);
 61         link(tmp[3],tmp[2],1,1,z);
 62         link(nde,tmp[3],1,0,z);
 63         link(tmp[3],tmp[4],1,1,z);
 64     }
 65     else if(typ==5)
 66         for(int i=1;i<=4;i++)
 67             link(nde,tmp[i],(i%2)?1:0,(i%2)?0:inf,z);
 68     else if(typ==6)
 69     {
 70         link(nde,tmp[2],1,0,z);
 71         link(nde,tmp[3],1,0,z);
 72         link(tmp[2],tmp[4],1,1,z);
 73         link(tmp[3],tmp[1],1,1,z);
 74     }
 75     else if(typ==7)
 76     {
 77         link(nde,tmp[1],1,0,z);
 78         link(nde,tmp[2],1,0,z);
 79         link(nde,tmp[3],1,0,z);
 80         link(tmp[1],tmp[4],1,1,z);
 81         link(tmp[2],tmp[4],1,2,z);
 82         link(tmp[3],tmp[4],1,1,z);
 83     }
 84     else if(typ==8)
 85     {
 86         link(tmp[4],tmp[1],1,1,z);
 87         link(tmp[4],tmp[2],1,2,z);
 88         link(tmp[4],tmp[3],1,1,z);
 89         link(nde,tmp[4],1,0,z);
 90     }
 91     else if(typ==9)
 92     {
 93         link(nde,tmp[1],1,0,z);
 94         link(nde,tmp[4],1,0,z);
 95         link(tmp[1],tmp[3],1,1,z);
 96         link(tmp[4],tmp[2],1,1,z);
 97     }
 98     else if(typ==10)
 99         for(int i=1;i<=4;i++)
100             link(nde,tmp[i],(i%2)?0:1,(i%2)?inf:0,z);
101     else if(typ==11)
102     {
103         link(nde,tmp[1],1,0,z);
104         link(nde,tmp[2],1,0,z);
105         link(nde,tmp[4],1,0,z);
106         link(tmp[1],tmp[3],1,2,z);
107         link(tmp[2],tmp[3],1,1,z);
108         link(tmp[4],tmp[3],1,1,z);
109     }
110     else if(typ==12)
111     {
112         link(nde,tmp[3],1,0,z);
113         link(nde,tmp[4],1,0,z);
114         link(tmp[3],tmp[1],1,1,z);
115         link(tmp[4],tmp[2],1,1,z);
116     }
117     else if(typ==13)
118     {
119         link(nde,tmp[1],1,0,z);
120         link(nde,tmp[3],1,0,z);
121         link(nde,tmp[4],1,0,z);
122         link(tmp[1],tmp[2],1,1,z);
123         link(tmp[3],tmp[2],1,1,z);
124         link(tmp[4],tmp[2],1,2,z);
125     }
126     else if(typ==14)
127     {
128         link(nde,tmp[2],1,0,z);
129         link(nde,tmp[3],1,0,z);
130         link(nde,tmp[4],1,0,z);
131         link(tmp[2],tmp[1],1,1,z);
132         link(tmp[3],tmp[1],1,2,z);
133         link(tmp[4],tmp[1],1,1,z);
134     }
135     else if(typ==15)
136         for(int i=1;i<=4;i++)
137             link(nde,tmp[i],1,0,z);
138 }
139 void Init(int st,int ed)
140 {
141     memset(mflw,0x3f,sizeof mflw);
142     memset(mcst,0x3f,sizeof mcst);
143     memset(queu,0,sizeof queu),pren[ed]=-1;
144     qs.push(st),queu[st]=true,mcst[st]=0;
145 }
146 bool SP(int st,int ed)
147 {
148     Init(st,ed); 
149     while(!qs.empty())
150     {
151         int tn=qs.front();
152         qs.pop(); queu[tn]=false;
153         for(int i=p[tn],g;i;i=noww[i])
154             if(mcst[g=goal[i]]>mcst[tn]+cost[i]&&flow[i])
155             {
156                 pren[g]=tn,pree[g]=i;
157                 mcst[g]=mcst[tn]+cost[i];
158                 mflw[g]=min(mflw[tn],flow[i]);
159                 if(!queu[g]) qs.push(g),queu[g]=true;
160             }
161     }
162     return ~pren[ed];
163 }
164 void MCMF(int st,int ed)
165 {
166     while(SP(st,ed))
167     {
168         maxf+=mflw[ed],id=ed;
169         minc+=mflw[ed]*mcst[ed]; 
170         while(id!=st)
171         {
172             flow[pree[id]]-=mflw[ed];
173             flow[pree[id]^1]+=mflw[ed];
174             id=pren[id];
175         }
176     }
177 }
178 int main ()
179 {
180     scanf("%d%d",&n,&m),cnt=1;
181     for(int i=1;i<=n;i++)
182         for(int j=1;j<=m;j++)
183         {
184             scanf("%d",&mapp[i][j]);
185             idx[i][j]=++tot,Create(i,j,(i+j)%2);
186         }
187     s=++tot,t=++tot;
188     hmap[0]=3,hmap[1]=4,hmap[2]=1,hmap[3]=2;
189     for(int i=1;i<=n;i++)
190         for(int j=1;j<=m;j++)
191         {
192             if((i+j)%2==0) 
193             {
194                 link(s,idx[i][j],inf,0,0),sum+=hed[i][j];
195                 for(int k=0;k<4;k++)
196                 {
197                     int tx=i+mov[k][0],ty=j+mov[k][1];
198                     if(tx>=1&&tx<=n&&ty>=1&&ty<=m)
199                         link(idx[i][j]+k+1,idx[tx][ty]+hmap[k],1,0,0);
200                 }
201             }
202             else 
203                 link(idx[i][j],t,inf,0,0);
204         }
205     MCMF(s,t);
206     if(maxf!=sum) printf("-1");
207     else printf("%d",minc);
208     return 0;
209 }
View Code

?

轉載于:https://www.cnblogs.com/ydnhaha/p/10127523.html

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

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

相關文章

Node.js學習之(第二章:exports和module.exports)

前言 Node中&#xff0c;每個模塊都有一個exports接口對象&#xff0c;我們需要把公共的方法或者字符串掛載在這個接口對象中&#xff0c;其他的模塊才可以使用。 Node.js中只有模塊作用域&#xff0c;默認兩個模塊之間的變量&#xff0c;方法互不沖突&#xff0c;互不影響&…

docker命令及掛載

常用命令所有鏡像:docker images當前執行:docker ps提交保存docker容器: docker commit進入到對應服務:docker attach <container id>已經執行帶容器:docker ps -l根據名稱啟動通過8081端口察看docker容器里的8080:docker run -i -t -d -p 8081:8080 -p23:22 ubuntu:ubun…

列表,元組,字典類的常見簡單方法

一.列表&#xff08;list類&#xff09; 1.append&#xff08;&#xff09;&#xff1a;追加一個參數&#xff0c;參數可以為字符串&#xff0c;數字或列表等&#xff0c;將參數視為一個整體 2.clear&#xff08;&#xff09;&#xff1a;直接清空列表里的所有 3.count&#xf…

與圖論的邂逅05:最近公共祖先LCA

什么是LCA&#xff1f; 祖先鏈 對于一棵樹T&#xff0c;若它的根節點是r&#xff0c;對于任意一個樹上的節點x&#xff0c;從r走到x的路徑是唯一的(顯然)&#xff0c;那么這條路徑上的點都是并且只有這些點是x的祖先。這些點組成的鏈(或者說路徑)就是x的祖先鏈。 LCA 根據名字來…

MAC地址進行驗證的方法

需要對對應的MAC地址進行驗證的方法&#xff0c;以為很簡單就能過&#xff0c;鼓搗了半天以后才發現&#xff0c;我的機器是window7&#xff0c;查詢出來是亂碼&#xff0c;居然不給支持。沒辦法在網上繼續找資料。終于找到了&#xff0c;貼上來&#xff0c;以備不時之需。 東西…

JAVA 分布式環境 Redis互斥鎖

開始的時候項目沒有添加互斥鎖&#xff0c;用的依然是老的思路&#xff0c;在并發量增加的情況下&#xff0c;遇到了很多的問題&#xff0c;包括數據庫重復讀等&#xff0c;想了下考慮增加 互斥鎖來排序對單個資源的操作。 Target(ElementType.METHOD) Retention(RetentionPoli…

相機添加多張圖片css布局

<section class"feedback-upload"><aside class"photos"><div></div><div class"camera"></div></aside><aside class"tips"><div><span>選填0~4</span></div&…

移動端滑動操作學習

(function(window,document){var Slide function(box,judge,fun){if (!(this instanceof Slide)) return new Slide(box,judge,fun);var startx,starty;box.addEventListener("touchstart", function(e) {e.preventDefault(); // 阻止瀏覽器默認事件startx parseIn…

深入學習Oracle分區表及分區索引

關于分區表和分區索引(About Partitioned Tables and Indexes)對于10gR2而言&#xff0c;基本上可以分成幾類&#xff1a; ?    Range(范圍)分區 ?    Hash(哈希)分區 ?    List(列表)分區 ?    以及組合分區&#xff1a;Range-Hash,R…

跟隨我在oracle學習php(21)

變量間的傳值方式 總體說明&#xff1a; 1&#xff0c;這里討論的傳值方式是指&#xff1a;一個變量對另一個變量 2&#xff0c;它不僅僅適用于賦值語句&#xff0c;也適用于其他有同樣含義的語句&#xff0c;比如&#xff1a;函數的實參到形參 3&#xff0c;傳值方式只有2種&a…

分區索引常用命令

一般使用LOCAL索引較為方便&#xff0c;而且維護代價較低&#xff0c;并且LOCAL索引是在分區的基礎上去創建索引&#xff0c;類似于在一個子表內部去創建索引&#xff0c;這樣開銷主要是區分分區上&#xff0c;很規范的管理起來&#xff0c;在OLAP系統中應用很廣泛&#xff1b;…

面向對象簡述

1&#xff0c;封裝&#xff1a;將對象的屬性集成在 class person:def __init__(self,name,idnum):self.namenameself.idnumidnum 2&#xff0c;繼承&#xff1a;子類自動擁有父類的的封裝&#xff0c;除了非私有之外 class person: def __init__(self,name,idnum): self.namena…

== 和 is 的區別

1. 比較的是值 a2 b2 print(a b) # True lis1 [1,2,3] lis2 [1,2,3] print(lis1 lis2) # True 2.is 是比較的是內存地址 a name print(id(a)) # 內存地址 字符串 a name b name print(a is b) # True 數字 n 10 n110 print(n is n1) # True 小數據池 數字 -5~256 字…

oracle數據量大時候分區索引思路

有一個分區表&#xff0c;按list分區&#xff0c;只有一個本地唯一索引&#xff0c;沒有外鍵和觸發器 當單個分區數量在2000萬以內時&#xff0c;insert效率還可以&#xff0c;每秒2.3-2.5萬條 但數據量越大&#xff0c;速度越慢&#xff0c; 目前單個分區數量達到3億&#xff…

【轉】WPF自定義控件與樣式(3)-TextBox RichTextBox PasswordBox樣式、水印、Label標簽、功能擴展...

一&#xff0e;前言.預覽 申明&#xff1a;WPF自定義控件與樣式是一個系列文章&#xff0c;前后是有些關聯的&#xff0c;但大多是按照由簡到繁的順序逐步發布的等。 本文主要是對文本輸入控件進行樣式開發&#xff0c;及相關擴展功能開發&#xff0c;主要內容包括&#xff1a;…

JVM調優 dump文件怎么生成和分析

1、獲取JVM的dump文件的兩種方式   1. JVM啟動時增加兩個參數: #出現 OOME 時生成堆 dump: -XX:HeapDumpOnOutOfMemoryError #生成堆文件地址&#xff1a; -XX:HeapDumpPath/home/liuke/jvmlogs/ 2. 發現程序異常前通過執行指令&#xff0c;直接生成當前JVM的dmp文件&#x…

關于 Oracle 分區索引的失效和重建

--創建測試表 SQL> create table t as select object_id,object_name from dba_objects;表已創建。SQL> select min(object_id),max(object_id) from t;MIN(OBJECT_ID) MAX(OBJECT_ID)-------------- --------------2 76083SQL> create table t_part(object…

【網絡安全/CTF】unseping 江蘇工匠杯

該題考察序列化反序列化及Linux命令執行相關知識。 題目 <?php highlight_file(__FILE__);class ease{private $method;private $args;function __construct($method, $args) {$this->method $method;$this->args $args;}function __destruct(){if (in_array($thi…

yum配置中driver-class-name: com.mysql.jdbc.Driver報錯

錯誤&#xff1a; 原因&#xff1a; 解決方法&#xff1a;把方框中的<scope>runtime</scope>刪掉 轉載于:https://www.cnblogs.com/zly123/p/10834958.html

gitlab中的CI

https://blog.csdn.net/chengzi_comm/article/details/78778284 轉載于:https://www.cnblogs.com/effortsing/p/10142720.html