bzoj2595[Wc2008]游览计划
http://www.lydsy.com/JudgeOnline/problem.php?id=2595
斯坦纳树。
斯坦纳树是在求一个图中的包含某些特定点的最小生成树,注意其他点不一定要包含。
这道题可以算是斯坦纳树的入门题了。
#include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<utility> #include<set> #include<bitset> #include<vector> #include<functional> #include<deque> #include<cctype> #include<climits> #include<complex> //#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL; typedef double DB; typedef pair<int,int> PII; typedef complex<DB> CP; #define mmst(a,v) memset(a,v,sizeof(a)) #define mmcy(a,b) memcpy(a,b,sizeof(a)) #define fill(a,l,r,v) fill(a+l,a+r+1,v) #define re(i,a,b) for(i=(a);i<=(b);i++) #define red(i,a,b) for(i=(a);i>=(b);i--) #define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++) #define fi first #define se second #define m_p(a,b) make_pair(a,b) #define p_b(a) push_back(a) #define SF scanf #define PF printf #define two(k) (1<<(k)) template<class T>inline T sqr(T x){return x*x;} template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;} template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} const DB EPS=1e-9; inline int sgn(DB x){if(abs(x)<EPS)return 0;return(x>0)?1:-1;} const DB Pi=acos(-1.0); inline int gint() { int res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } inline LL gll() { LL res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } const int maxN=10; const int maxK=10; const int INF=0xf0f0f0f; const int dx[]={1,-1,0,0}; const int dy[]={0,0,1,-1}; int N,M,K; int a[maxN+10][maxN+10]; int id[maxN+10][maxN+10]; int F[maxN+10][maxN+10][two(maxK)+100]; struct Tpre { int x,y,s; inline Tpre(int _x=0,int _y=0,int _s=0){x=_x;y=_y;s=_s;} }pre[maxN+10][maxN+10][two(maxK)+100]; inline int update(int x,int y,int state,int px,int py,int pstate,int v) { if(v<F[x][y][state]) return F[x][y][state]=v,pre[x][y][state]=Tpre(px,py,pstate),1; return 0; } queue<PII>Q; int vis[maxN+10][maxN+10]; inline void SPFA(int state) { while(!Q.empty()) { int i,x=Q.front().fi,y=Q.front().se;Q.pop(); vis[x][y]=0; re(i,0,3) { int _x=x+dx[i],_y=y+dy[i]; if(_x<1 || _x>N || _y<1 || _y>M)continue; if(update(_x,_y,state,x,y,state,F[x][y][state]+a[_x][_y]) && !vis[_x][_y])vis[_x][_y]=1,Q.push(PII(_x,_y)); } } } inline void DFS(int i,int j,int state) { if(i==0 && j==0 && state==0)return; vis[i][j]=1; int x=pre[i][j][state].x,y=pre[i][j][state].y,s=pre[i][j][state].s; DFS(x,y,s); if(i==x && j==y)DFS(x,y,state-s); } inline void output() { int i,j; re(i,1,N) { re(j,1,M) if(a[i][j]==0) putchar('x'); else if(vis[i][j]) putchar('o'); else putchar('_'); putchar('\n'); } } int main() { freopen("bzoj2595.in","r",stdin); freopen("bzoj2595.out","w",stdout); int i,j; N=gint();M=gint(); mmst(F,0xf); re(i,1,N)re(j,1,M) { a[i][j]=gint(); if(a[i][j]==0)id[i][j]=++K,F[i][j][two(K-1)]=0; } int state,maxstate=two(K)-1; re(state,1,maxstate) { re(i,1,N)re(j,1,M) { for(int s=state&(state-1);s;s=(s-1)&state) update(i,j,state,i,j,s,F[i][j][s]+F[i][j][state-s]-a[i][j]); if(F[i][j][state]!=INF)Q.push(PII(i,j)),vis[i][j]=1; } SPFA(state); } re(i,1,N)re(j,1,M)if(a[i][j]==0) { cout<<F[i][j][maxstate]<<endl; DFS(i,j,maxstate); output(); return 0; } return 0; }