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 ...
随机推荐
- Vim - Avoid the escape key
http://vim.wikia.com/wiki/Avoid_the_escape_key
- C#数组的Map、Filter、Reduce操作
在Javascript.Python等语言里,Map.Filter和Reduce是数组的常用方法,可以让你在实现一些数组操作时告别循环,具有很高的实用价值.它们三个的意义大家应该都清楚,有一个十分形象 ...
- win 7 64 安装 MondgoDB 3.4
https://jingyan.baidu.com/article/f3e34a12ac10cef5eb653583.html mongod --dbpath "D:\Program Fil ...
- U盘中了磁碟机病毒怎么办
问题: U盘在中毒了的电脑上使用后,里面的文件夹均消失了,这是因为里面的文件夹属性被改为隐藏属性.通过查看显示隐藏文件夹发现,所有隐藏了的文件夹的隐藏属性被锁定,无法通过鼠标右键查看文件夹属性的方法改 ...
- CSS 图像透明/不透明
CSS 图像透明/不透明 使用CSS很容易创建透明的图像. 注意:CSS Opacity属性是W3C的CSS3建议的一部分. 一.示例一:创建一个透明图像 CSS3中属性的透明度是 opacity. ...
- tomcat结合httpd和nginx
httpd结合tomcat: 前提:httpd版本2.4以上,编译安装 httpd:192.168.223.136 tomcat:192.168.223.146 tomcat简单创建一个额外的weba ...
- 20145322《Java程序设计》第4次实验报告
实验内容 1.搭建Android环境 2.运行Android 3.修改代码并输出自己的学号 实验步骤 搭建Android环境 安装Android,核心是配置JDK.SDK 运行Android 最终结果 ...
- 求CRC16校验
unsigned short DialogSerial::crc_ccitt(unsigned char *q,int len){ unsigned short ccitt_table[256] = ...
- MR案例:多文件输出MultipleOutputs
问题描述:现有 ip-to-hosts.txt 数据文件,文件中每行数据有两个字段:分别是ip地址和该ip地址对应的国家,以'\t'分隔.要求汇总不同国家的IP数,并以国家名为文件名将其输出.解读:M ...
- TensorFlow和深度学习入门教程(TensorFlow and deep learning without a PhD)【转】
本文转载自:https://blog.csdn.net/xummgg/article/details/69214366 前言 上月导师在组会上交我们用tensorflow写深度学习和卷积神经网络,并把 ...