方格取数--状压DP or 网络流
题意:http://acm.hdu.edu.cn/showproblem.php?pid=1565
取不相邻的点是权值最大。
这题可以网络流做,暂时先DP一下,网络流明天学一下~~
1 #define IOS ios_base::sync_with_stdio(0); cin.tie(0); 2 #include <cstdio>//sprintf islower isupper 3 #include <cstdlib>//malloc exit strcat itoa system("cls") 4 #include <iostream>//pair 5 #include <fstream>//freopen("C:\\Users\\13606\\Desktop\\草稿.txt","r",stdin); 6 #include <bitset> 7 //#include <map> 8 //#include<unordered_map> 9 #include <vector> 10 #include <stack> 11 #include <set> 12 #include <string.h>//strstr substr 13 #include <string> 14 #include <time.h>// srand(((unsigned)time(NULL))); Seed n=rand()%10 - 0~9; 15 #include <cmath> 16 #include <deque> 17 #include <queue>//priority_queue<int, vector<int>, greater<int> > q;//less 18 #include <vector>//emplace_back 19 //#include <math.h> 20 //#include <windows.h>//reverse(a,a+len);// ~ ! ~ ! floor 21 #include <algorithm>//sort + unique : sz=unique(b+1,b+n+1)-(b+1);+nth_element(first, nth, last, compare) 22 using namespace std;//next_permutation(a+1,a+1+n);//prev_permutation 23 //****************** 24 int abss(int a); 25 int lowbit(int n); 26 int Del_bit_1(int n); 27 int maxx(int a,int b); 28 int minn(int a,int b); 29 double fabss(double a); 30 void swapp(int &a,int &b); 31 clock_t __STRAT,__END; 32 double __TOTALTIME; 33 void _MS(){__STRAT=clock();} 34 void _ME(){__END=clock();__TOTALTIME=(double)(__END-__STRAT)/CLOCKS_PER_SEC;cout<<"Time: "<<__TOTALTIME<<" s"<<endl;} 35 //*********************** 36 #define rint register int 37 #define fo(a,b,c) for(rint a=b;a<=c;++a) 38 #define fr(a,b,c) for(rint a=b;a>=c;--a) 39 #define mem(a,b) memset(a,b,sizeof(a)) 40 #define pr printf 41 #define sc scanf 42 #define ls rt<<1 43 #define rs rt<<1|1 44 typedef long long ll; 45 const double E=2.718281828; 46 const double PI=acos(-1.0); 47 //const ll INF=(1LL<<60); 48 const int inf=(1<<30); 49 const double ESP=1e-9; 50 const int mod=(int)1e9+7; 51 const int N=(int)1e6+10; 52 53 int ok_num[N]; 54 int dp[2][20000]; 55 int a[25][25]; 56 57 bool OK(int n,int x) 58 { 59 int F[22]; 60 F[0]=0; 61 for(int i=1;i<=n;++i) 62 F[i]=(x>>(n-i))&1; 63 for(int i=1;i<n;++i) 64 if(F[i]&&F[i+1])return 0; 65 return 1; 66 } 67 int get(int now,int state,int n) 68 { 69 int F[22]; 70 F[0]=0; 71 for(int i=1;i<=n;++i) 72 F[i]=(state>>(n-i))&1; 73 74 int temp=0; 75 for(int i=1;i<=n;++i) 76 if(F[i])temp+=a[now][i]; 77 return temp; 78 } 79 80 void solve(int n) 81 { 82 int cnt=0; 83 int tot=(1<<n)-1; 84 for(int i=0;i<=tot;++i) 85 { 86 if(OK(n,i)) 87 ok_num[++cnt]=i; 88 } 89 // cout<<cnt<<endl; 90 for(int i=1;i<=n;++i) 91 for(int j=1;j<=n;++j) 92 sc("%d",&a[i][j]); 93 for(int i=0;i<=cnt;++i) 94 dp[0][i]=dp[1][i]=0; 95 for(int i=1;i<=n;++i) 96 { 97 for(int j=1;j<=cnt;++j) 98 { 99 int temp=get(i,ok_num[j],n); 100 // dp[1][j]=temp; 101 for(int k=1;k<=cnt;++k) 102 { 103 if((ok_num[j]&ok_num[k])==0) 104 dp[1][j]=maxx(dp[1][j],temp+dp[0][k]); 105 } 106 } 107 for(int j=1;j<=cnt;++j) 108 dp[0][j]=dp[1][j],dp[1][j]=0; 109 } 110 int ans=0; 111 for(int i=1;i<=cnt;++i) 112 ans=maxx(ans,dp[0][i]); 113 pr("%d\n",ans); 114 } 115 116 int main() 117 { 118 int n; 119 while(~sc("%d",&n))solve(n); 120 return 0; 121 } 122 123 /**************************************************************************************/ 124 125 int maxx(int a,int b) 126 { 127 return a>b?a:b; 128 } 129 130 void swapp(int &a,int &b) 131 { 132 a^=b^=a^=b; 133 } 134 135 int lowbit(int n) 136 { 137 return n&(-n); 138 } 139 140 int Del_bit_1(int n) 141 { 142 return n&(n-1); 143 } 144 145 int abss(int a) 146 { 147 return a>0?a:-a; 148 } 149 150 double fabss(double a) 151 { 152 return a>0?a:-a; 153 } 154 155 int minn(int a,int b) 156 { 157 return a<b?a:b; 158 }