ZOJ 1609 Equivalence(状压+dfs减枝)
Time Limit: 5 Seconds Memory Limit: 32768 KB
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[];
int n,cnt,ans;
int a[][];
void dfs(int k,int sum)
{
if (sum>ans) return; //减枝,如果sum已经大于已保存ans的最优解,说明此方法不是最优,直接返回
int num=; //记录到现在为止,能强连通其他点的点的个数
for(int i=;i<n;i++)
{
num+=a[k][i]==((<<n)-); //(1<<n)-1 表达的是所有都连通的状态
a[k+][i]=a[k][i];
}
if(num==n){ ans=min(ans,sum); return;} //完成了所有的强连通
if(k>cnt) return; //枚举的路已经没有了 for(int i=;i<n;i++)
{
if(a[k+][i] & <<mp[k].x) //如果i点已经连通mp[k].x,则加上mp[k]这条边,就能连通mp[k].y能连通的点
a[k+][i]|=a[k][mp[k].y];
} dfs(k+,sum+mp[k].w);//第k条路取
for(int i=;i<n;i++) a[k+][i]=a[k][i];
dfs(k+,sum);//不取
}
int main()
{ while(~scanf("%d",&n))
{
cnt=;
for(int i=;i<n;i++)
for(int j=;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=;i<n;i++) a[][i]=<<i;
ans=0x7fffffff;
dfs(,);
printf("%d\n",ans);
}
return ;
}
ZOJ 1609 Equivalence(状压+dfs减枝)的更多相关文章
- bzoj1725: [Usaco2006 Nov]Corn Fields牧场的安排(状压dfs)
1725: [Usaco2006 Nov]Corn Fields牧场的安排 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1122 Solved: 80 ...
- 状压dfs小记
一点前(tu)言(cao) 真的考起dfs来可谓是什么都能往dfs上套 状压不止能dp,还能与dfs结合成为搜索好(duliu)题 剪枝卡常司空见惯(打开题解一看并不是纯dfs,emmmm) 开始正文 ...
- codeforces 285 D. Permutation Sum 状压 dfs打表
题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...
- JZYZOJ1530 [haoi2013]开关控制 状压 dfs 折半搜索
http://172.20.6.3/Problem_Show.asp?id=1530 元宵节快要到了,某城市人民公园将举办一次灯展.Dr.Kong准备设计出一个奇妙的展品,他计划将编号为1到N的N(1 ...
- UVALive 6255:Kingdoms(状压DFS)
题目链接 题意 给出n个王国和n*n的矩阵,mp[i][j] 代表第 i 个王国欠第 j 个王国 mp[i][j] 块钱.如果当前的王国处于负债状态,那么这个王国就会被消除,和它相连的王国的债务都会被 ...
- 2018icpc南京网络赛-E AC Challenge(状压+dfs)
题意: n道题,每道题有ai和bi,完成这道题需要先完成若干道题,完成这道题可以得到分数t*ai+bi,其中t是时间 1s, n<=20 思路: 由n的范围状压,状态最多1e6 然后dfs,注意 ...
- hdu 4620 Fruit Ninja Extreme(状压+dfs剪枝)
对t进行从小到大排序(要记录ID),然后直接dfs. 剪枝的话,利用A*的思想,假设之后的全部连击也不能得到更优解. 因为要回溯,而且由于每次cut 的数目不会超过10,所以需要回溯的下标可以利用一个 ...
- ZOJ 3777-Problem Arrangement(状压DP)
B - Problem Arrangement Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %l ...
- HDU - 6341 多校4 Let Sudoku Rotate(状压dfs)
Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K ...
随机推荐
- Hbase1.0伪分布式集群启动失败问题
作者:Syn良子 出处:http://www.cnblogs.com/cssdongl/p/7340681.html 转载请注明出处 最近抽空折腾自己的虚拟机环境时启动伪分布式Hbase集群一直失败, ...
- js经典面试问题:如何让for循环中的setTimeout()函数像预想中一样工作?
setTimeout()是js中的一类重要函数,将一段代码延迟一定时间并异步执行.但是这个函数经常不听话.在实践中,可能经常有人碰到类似下面的这种情况: for (var i = 1; i <= ...
- 资产证券化(ABS)+ 特殊目的信托(SPV)
资产证券化是指以基础资产未来所产生的现金流为偿付支持,通过结构化设计进行信用增级,在此基础上发行资产支持证券(Asset-backed Securities, ABS)的过程,通过将有形或者无形资产作 ...
- 【虚拟机】linux 桥接模式 固定静态IP
1:在root用户下修改 2:进入root用户下 vim/etc/sysconfig/network-scripts/ifcfg-eth0 2.修改完保存退出 3.重启网络服务:service net ...
- openwrt的编译系统是如何制作根文件系统的
答:分析以下makefile即可获取整个过程 以nxp layerscape系统的编译过程为例 1.分析target/linux/layerscape/image/Makefile的最后一句,这是一个 ...
- IPTABLES拒绝某个IP某项服务,并记录到日志(rhel7实例)
#iptables -I INPUT -p icmp -s 192.168.0.1 -j DROP \\在INPUT链中插入:如果检测到从192.168.0.1发过来的 ...
- LeetCode——Unique Paths
Question A robot is located at the top-left corner of a m x n grid (marked 'Start' in the diagram be ...
- webservice获取天气信息
效果 1.eclipse中新建一个Java项目 2.通过命名获取天气的客户端信息 首先,打开天气网站http://ws.webxml.com.cn/WebServices/WeatherWS.asmx ...
- 解题报告:poj2689 Prime Distance
2017-10-03 11:29:20 writer:pprp 来源:kuangbin模板 从已经筛选好的素数中筛选出规定区间的素数 /* *prime DIstance *给出一个区间[L,U],找 ...
- LeetCode第[91]题(Java):Decode Ways(解码方法数)
题目:解码方法数 难度:Medium 题目内容: A message containing letters from A-Z is being encoded to numbers using the ...