ZOJ 1609 Equivalence(状压+dfs减枝)
When learning mathamatics, sometimes one may come to an theorem which goes like this:
The following statements are equivalent:
a)......
b)......
c)......
For example, let A be an angle between 0 and 360 degrees, the following statements are equivalent:
a)A = 90 degrees;
b)A is a right angle;
c)sin(A) = 1.
Proving such a theorem is usually a difficult task, because you have to prove that for any two statements Si and Sj, Si concludes Sj and vise versa. Sometimes, proving Si concludes Sj directly is quite difficult so we may find a Sk and prove that Si concludes Sk and Sk concludes Sj. Now given the difficulty of proving every Si => Sj, you are to calculate the minimal total difficulty to prove that the given statements are equivalent.
Input
The input contains several cases. Each case begins with an integer n (2 <= n <= 6), the number of statements in this case, followed by n lines, each contains n integers.
The jth integer of the ith row represents the difficulty of proving Si => Sj. The ith integer of the ith row is always 0 as it's obvious that Si concludes Si. All the n * n integers are between 0 and 100, inclusively. Input is terminated by EOF.
Output
For each test case, output a line with the minimal difficulty for that case.
Sample Input
4
0 2 3 4
5 0 7 8
9 10 0 12
13 14 15 0
Sample Output
34
Author: PAN, Minghao
Source: ZOJ Monthly, May 2003
题解:该题是求强连通,并使权值最小。。。 状压+dfs减枝
#include <iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<stack> #include<map> #include<set> #include<queue> #include<cmath> #include<string> #include<vector> using namespace std; struct node { int x,y,w; }mp[50]; int n,cnt,ans; int a[1000][10]; void dfs(int k,int sum) { if (sum>ans) return; //减枝,如果sum已经大于已保存ans的最优解,说明此方法不是最优,直接返回 int num=0; //记录到现在为止,能强连通其他点的点的个数 for(int i=0;i<n;i++) { num+=a[k][i]==((1<<n)-1); //(1<<n)-1 表达的是所有都连通的状态 a[k+1][i]=a[k][i]; } if(num==n){ ans=min(ans,sum); return;} //完成了所有的强连通 if(k>cnt) return; //枚举的路已经没有了 for(int i=0;i<n;i++) { if(a[k+1][i] & 1<<mp[k].x) //如果i点已经连通mp[k].x,则加上mp[k]这条边,就能连通mp[k].y能连通的点 a[k+1][i]|=a[k][mp[k].y]; } dfs(k+1,sum+mp[k].w);//第k条路取 for(int i=0;i<n;i++) a[k+1][i]=a[k][i]; dfs(k+1,sum);//不取 } int main() { while(~scanf("%d",&n)) { cnt=0; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { int x; scanf("%d",&x); if (i!=j) mp[cnt].x=i,mp[cnt].y=j,mp[cnt++].w=x; } cnt--; for(int i=0;i<n;i++) a[0][i]=1<<i; ans=0x7fffffff; dfs(0,0); printf("%d\n",ans); } return 0; }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步