Link:
#include <stdio.h>int main(){ puts("转载请注明出处[辗转山河弋流歌 by 空灰冰魂]谢谢"); puts("网址:blog.csdn.net/vmurder/article/details/46499973");}
Topic:
Eectilinear
Time limit:1 Sec Memory limit:128 MB
Description
Now there is a n*m matrix, some elements are 0, and the remaining elements are greater than 0.
Now you have to select some elements so that any two elements that are 0 can be connected by the selected element four.
(Note that if you want to meet the requirements, all 0 itself must be selected.)
What is the minimum value of the selected element?
Input
The first line is two integers n,m, which indicates the length and width of the matrix.
The next n rows, m integers per line, and the first row of J integers a[i,j] denote the size of the element in column J of the Matrix.
Output
The output line is an integer representing the minimum and the selected elements.
Sample Input
4 4
0 1 1 0
1 9 9 1
1 9 9 1
0 1 1 0
Sample Output
6
HINT
1<=n,m<=10,2<=num (0) <=10,0<=a[i,j]<=2^16.
Exercises
Steiner Tree Templates
Process:
Enumeration state set S
{
&NBSP; &NBSP, &NBSP; Span style= "Display:inline-block; width:0px; Height:2.279em; " > Enumerate s subsets of S
&NBSP; &NBSP; {
< Span class= "Mrow" id= "mathjax-span-14" > &NBSP; &NBSP; &NBSP; &NBSP; Update F[s][1~n]
& nbsp; &NBSP; &NBSP; }
& nbsp; &NBSP; &NBSP; Will F [S ][x]<INF The X queue
& nbsp; &NBSP; &NBSP; SPFA (S)
}
Code:
#include <queue>#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N#define P#define INF 0x3f3f3f3fusing namespace STD;Const intdx[]={0,0,1,-1};Const intdy[]={1,-1,0,0};structeli{intV,n;} e[n<<2];inthead[n],cnt;inline voidAddintUintV) {e[++cnt].v=v; E[cnt].n=head[u]; head[u]=cnt;}intN,m;intId[p][p],val[n];voidBuild () {intI,j,k;intx, y;scanf("%d%d", &n,&m); for(i=1; i<=n;i++) for(j=1; j<=m;j++)scanf("%d", &val[id[i][j]=++cnt]); Cnt=0; for(i=1; i<=n;i++) for(j=1; j<=m;j++) for(k=0;k<4; k++)if(Id[x=i+dx[k]][y=j+dy[k]]) Add (Id[i][j],id[x][y]); N*=m,cnt=0;}intf[1<<P][N]; Queue<int>QBOOLIn[n];voidSPFA (intS) {intI,u,v; while(!q.empty ()) {U=q.front (), Q.pop (), in[u]=0; for(I=HEAD[U];I;I=E[I].N) {if(F[s][v=e[i].v]>f[s][u]+val[v]) {F[S][V]=F[S][U]+VAL[V];if(!in[v]) Q.push (v); } } }}voidWork () {intI,j,k;intS,s;memset(F,0x3f3f3f3f,sizeoff); for(i=1; i<=n;i++)if(!val[i]) f[1<<cnt++][i]=0; for(s=1; s< (1<<CNT); s++) { for(S= (S-1) &s;s;s= (S-1) &s) for(i=1; i<=n;i++) F[s][i]=min (F[s][i],f[s][i]+f[s^s][i]-val[i]); for(i=1; i<=n;i++)if(F[s][i]<inf&&!in[i]) Q.push (i), in[i]=1; SPFA (S); }intAns=inf; for(i=1; i<=n;i++) Ans=min (ans,f[(1<<CNT)-1][i]);printf("%d\n", ans);}intMain () {Freopen ("Test.in","R", stdin); Build (); Work ();return 0;}
"Template" Eectilinear