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 ...
随机推荐
- sparkSQL——DataFrame&Datasets
对于新司机,可能看到sc与spark不知道是什么,看图知意 ********************************************************************** ...
- 获取Android设备的方向,Sensor和SensorManager实现手机旋转角度
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2012/1009/425.html 带有g-sensor的Android设备上可通过API ...
- CSS Border(边框)
CSS Border(边框) 一.CSS 边框属性 CSS边框属性允许你指定一个元素边框的样式和颜色. 示例效果: 二.边框样式 边框样式属性指定要显示什么样的边界. border-style属性用来 ...
- CSS Fonts(字体)
CSS Fonts(字体) 一.serif和sans-serif字体之间的区别 注意: 在计算机屏幕上,sans-serif字体被认为是比serif字体容易阅读. 二.CSS字型 在CSS中,有两种类 ...
- # 20145314《信息安全系统设计基础》期中复习总结 Part B
20145314<信息安全系统设计基础>期中复习总结 Part B 学习知识点内容总结 复习线索:http://group.cnblogs.com/topic/73069.html 本周的 ...
- cogs 341:[NOI2005] 聪聪与可可
★★ 输入文件:cchkk.in 输出文件:cchkk.out 简单对比 时间限制:1 s 内存限制:256 MB [问题描述] 在一个魔法森林里,住着一只聪明的小猫聪聪和一只可爱的小 ...
- Linq to SQL - 撤销所有未提交的改动
在某些情况下我们需要撤销/丢弃所有未提交的改动,包括Update, Delete和Insert.context中GetChangeSet()方法可以返回当前所有未提交的改动,而我们的目标是清空Chan ...
- eclipse 工程没有build path
项目的.project文件添加: <buildSpec><buildCommand><name>org.eclipse.jdt.core.javabuilder&l ...
- centos 查询mysql配置文件位置
具体指令: 1.which mysqld (”which 文件名“ : 搜索命令所在路径及别名) 2./usr/sbin/mysqld --verbose --help | grep -A 1 'D ...
- C# Programming Guide-->Statements, Expressions, and Operators-->Anonymous Functions
C# Programming Guide Anonymous Functions Lambda Expressions Anonymous Methods In versions of C# befo ...