[dfs] Jzoj P4252 QYQ的图
题解
- 直接搜索,如果一个点不选就把所有它连的点选上,再加上一些最优性剪枝就好了
代码
1 #include <cstdio> 2 #include <iostream> 3 #define N 60 4 using namespace std; 5 int e[N][N],a[N],c[N],n,m,ans=2147483647; 6 void dfs(int d,int x,int y) 7 { 8 if (d>x) { ans=min(ans,y); return; } 9 if (a[d]!=0) { dfs(d+1,x,y+(a[d]==1?c[d]:0)); return; } 10 int bz=0; 11 for (int i=1;i<=e[d][0];i++) 12 if (a[e[d][i]]==0) bz=1; 13 else if (a[e[d][i]]==2) { bz=2; break; } 14 if (bz==0) a[d]=2,dfs(d+1,x,y),a[d]=0; 15 if (bz==1) a[d]=1,dfs(d+1,x,y+c[d]),a[d]=2,dfs(d+1,x,y),a[d]=0; 16 if (bz==2) a[d]=1,dfs(d+1,x,y+c[d]),a[d]=0; 17 } 18 int main() 19 { 20 freopen("graph.in","r",stdin),freopen("graph.out","w",stdout),scanf("%d%d",&n,&m); 21 for (int i=1;i<=n;i++) scanf("%d",&c[i]); 22 for (int i=1,x,y;i<=m;i++) 23 { 24 scanf("%d%d",&x,&y); 25 if (x!=y) e[x][++e[x][0]]=y,e[y][++e[y][0]]=x; else a[x]=1; 26 } 27 dfs(1,n,0),printf("%d",ans); 28 }