bzoj 1001 平面圖轉對偶圖
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 29376 Solved: 7694 [Submit][Status][Discuss] Description 現在小朋友們最喜歡的"喜羊羊與灰太狼",話說灰太狼抓羊不到,但抓兔子還是比較在行的, 而且現在的兔子還比較笨,它們只有兩個窩,現在你做為狼王,面對下面這樣一個網格的地形:
左上角點為(1,1),右下角點為(N,M)(上圖中N=4,M=5).有以下三種類型的道路 1:(x,y)<>(x+1,y) 2:(x,y)<>(x,y+1) 3:(x,y)<==>(x+1,y+1) 道路上的權值表示這條路上最多能夠通過的兔子數,道路是無向的. 左上角和右下角為兔子的兩個窩, 開始時所有的兔子都聚集在左上角(1,1)的窩裡,現在它們要跑到右下解(N,M)的窩中去,狼王開始伏擊 這些兔子.當然為了保險起見,如果一條道路上最多通過的兔子數為K,狼王需要安排同樣數量的K只狼, 才能完全封鎖這條道路,你需要幫助狼王安排一個伏擊方案,使得在將兔子一網打盡的前提下,參與的 狼的數量要最小。因為狼還要去找喜羊羊麻煩. Input 第一行為N,M.表示網格的大小,N,M均小於等於1000. 接下來分三部分 第一部分共N行,每行M-1個數,表示橫向道路的權值. 第二部分共N-1行,每行M個數,表示縱向道路的權值. 第三部分共N-1行,每行M-1個數,表示斜向道路的權值. 輸入檔案保證不超過10M Output 輸出一個整數,表示參與伏擊的狼的最小數量.
Sample Input 3 4
5 6 4
4 3 1
7 5 3
5 6 7 8
8 7 6 5
5 5 5
6 6 6 Sample Output 14
平面圖的的最大流就是他對偶圖的最短路,轉換的方法,起點和終點連一條邊,然後把每個面定義一個編號,如果相鄰的連個面連線一條邊,邊的權值就是這個這兩個面之間的那個邊的權值。
#include<bits/stdc++.h> using namespace std; const int N = 2e6+100; vector<pair<int,int> > G[N]; void add_edge(int u,int v,int w){ //cout <<u << ' '<<v << endl; G[u].push_back({v,w}); G[v].push_back({u,w}); } int d[N]; int dij(int s,int t){ memset(d,0x3f,sizeof(d)); d[s] = 0; priority_queue<pair<int,int>,vector<pair<int,int> > ,greater<pair<int,int> > > que; que.push({0,0}); while(!que.empty()){ pair<int,int> now = que.top(); que.pop(); int u = now.second,dd = now.first; if(dd > d[u]) continue; for(int i = 0;i < G[u].size();i ++){ int v = G[u][i].first; if(d[v] > dd+G[u][i].second){ d[v] = dd+G[u][i].second; que.push({d[v],v}); } } } return d[t]; } int main(){ int n,m; scanf("%d %d",&n,&m); for(int i = 1;i <= n;i ++){ for(int j = 1;j < m;j ++){ int now; scanf("%d",&now); int u,v; if(i == n) v = n*m*2+1; else v = (i-1)*(m-1)*2+2*j; if(i == 1) u = 0; else u = (i-2)*(m-1)*2+2*j-1; add_edge(u,v,now); } } for(int i = 1;i < n;i ++){ for(int j= 1;j <= m;j ++){ int now; scanf("%d",&now); int u,v; if(j == 1) u = 2*n*m+1; else u = (i-1)*(m-1)*2+2*(j-1); if(j == m) v = 0; else v = (i-1)*(m-1)*2+2*j-1; add_edge(u,v,now); } } for(int i = 1;i < n;i ++){ for(int j = 1;j < m;j ++){ int now; scanf("%d",&now); int u,v; u = (i-1)*2*(m-1)+2*j-1; v = u+1; add_edge(u,v,now); } } printf("%d\n",dij(0,2*n*m+1)); return 0; }