ZOJ Problem Set - 1609
Equivalence

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减枝)的更多相关文章

  1. bzoj1725: [Usaco2006 Nov]Corn Fields牧场的安排(状压dfs)

    1725: [Usaco2006 Nov]Corn Fields牧场的安排 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1122  Solved: 80 ...

  2. 状压dfs小记

    一点前(tu)言(cao) 真的考起dfs来可谓是什么都能往dfs上套 状压不止能dp,还能与dfs结合成为搜索好(duliu)题 剪枝卡常司空见惯(打开题解一看并不是纯dfs,emmmm) 开始正文 ...

  3. codeforces 285 D. Permutation Sum 状压 dfs打表

    题意: 如果有2个排列a,b,定义序列c为: c[i] = (a[i] + b[i] - 2) % n + 1 但是,明显c不一定是一个排列 现在,给出排列的长度n (1 <= n <= ...

  4. JZYZOJ1530 [haoi2013]开关控制 状压 dfs 折半搜索

    http://172.20.6.3/Problem_Show.asp?id=1530 元宵节快要到了,某城市人民公园将举办一次灯展.Dr.Kong准备设计出一个奇妙的展品,他计划将编号为1到N的N(1 ...

  5. UVALive 6255:Kingdoms(状压DFS)

    题目链接 题意 给出n个王国和n*n的矩阵,mp[i][j] 代表第 i 个王国欠第 j 个王国 mp[i][j] 块钱.如果当前的王国处于负债状态,那么这个王国就会被消除,和它相连的王国的债务都会被 ...

  6. 2018icpc南京网络赛-E AC Challenge(状压+dfs)

    题意: n道题,每道题有ai和bi,完成这道题需要先完成若干道题,完成这道题可以得到分数t*ai+bi,其中t是时间 1s, n<=20 思路: 由n的范围状压,状态最多1e6 然后dfs,注意 ...

  7. hdu 4620 Fruit Ninja Extreme(状压+dfs剪枝)

    对t进行从小到大排序(要记录ID),然后直接dfs. 剪枝的话,利用A*的思想,假设之后的全部连击也不能得到更优解. 因为要回溯,而且由于每次cut 的数目不会超过10,所以需要回溯的下标可以利用一个 ...

  8. ZOJ 3777-Problem Arrangement(状压DP)

    B - Problem Arrangement Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %l ...

  9. HDU - 6341 多校4 Let Sudoku Rotate(状压dfs)

    Problem J. Let Sudoku Rotate Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K ...

随机推荐

  1. ASP.NET Core EF 查询获取导航属性值,使用Include封装

    // 引用 using Microsoft.EntityFrameworkCore; // 摘要: // Specifies related entities to include in the qu ...

  2. [转]MySQL查看数据库相关信息

    原文链接:MySQL查看数据库相关信息 使用MySQL时,需要了解当前数据库的情况,例如当前的数据库大小.字符集.用户等等.下面总结了一些查看数据库相关信息的命令 1:查看显示所有数据库 mysql& ...

  3. spring-boot单元测试

    一.为什么要写单元测试 很多程序员有两件事情不愿意做: 写注释. 写单元测试. 但是在看代码时又会希望有清晰明了的注释,重构代码时能有一套随时可以跑起来的单元测试. 最近在迁移一个项目,从sqlser ...

  4. 前端学习笔记之ES6快速入门

    0x1 let和const let ES6新增了let命令,用于声明变量.其用法类似var,但是声明的变量只在let命令所在的代码块内有效. { let x = 10; var y = 20; } x ...

  5. cordova 插件

    1.移动端WebApp开发,如何实现状态栏沉浸式效果? cordova-plugin-fullscreen 2. cordova热更新插件-不发布应用市场动态更新APP源码 https://githu ...

  6. 20145312 实验二《 Java面向对象程序设计》

    20145312 实验二< Java面向对象程序设计> 实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解 ...

  7. CentOS7系统安装配置samba服务

    # 查询是否已经安装了Samba rpm -qi samba # 安装 yum -y install samba samba-client samba-common # 添加新用户 useradd s ...

  8. ethtool命令详解

    命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0.eth1等等etht ...

  9. ssm中不能访问静态资源问题

    最近用springmvc spring mybatis框架写程序,请求成功并获得数据,唯独css样式不能加载,但路径正确,css文件编码也是utf-8,用火狐debug总是显示未请求到(都快怀疑自己写 ...

  10. 用 SqlConnectionStringBuilder 来写连接字符串,向连接字符串添加设置

    正常情况下写的连接字符串: connStr = "Data Source=127.0.0.1;DataBase=Hydor;UID=***;PWD=***;Pooling=true;Min ...