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 ...
随机推荐
- mapper的namespace
一般情况下mapper的namespace能随便写,不重复即可, 但如果希望使用mybatis动态代理的接口,就需要namespace中的值和需要对应的Mapper(dao)接口的全路径一致.例如:c ...
- mysql增加远程访问
grant all privileges on *.* to 'root'@'%' identified by '123456' with grant option;
- win64 Python下安装PIL出错解决2.7版本 (3.6版本可以使用)
转自:http://blog.csdn.net/lhh31/article/details/51979293 1.软件版本 首先我先安装了 python 2.7 pip是 8.1.2 2.当我要安装 ...
- Linux下安装SVN服务端
安装 使用yum安装非常简单: yum install subversion 配置 2.1. 创建仓库 我们这里在/home下建立一个名为svn的仓库(repository),以后所有代码都放在这个下 ...
- Centos6优化系统服务脚本
#!/bin/bash SysVer=`cat /etc/redhat-release | awk -F'release' '{print $2}' | awk -F'[ .]+' '{print $ ...
- CentOS 6.2配置本地yum源
转载自http://www.cnblogs.com/centoser/articles/2411694.html#undefined 一.挂载本地光盘到系统:把Cent6.2安装光盘放入光驱,在终端命 ...
- 爬虫之动态HTML处理(Selenium与PhantomJS )网站模拟登录
#coding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysimport tim ...
- JAVA初学者(一)
2015-12-15 21:26:17 刚学的java 做个总结: 1.构造函数没有返回值. 2.A对象调用Q的方法,Q方法里的变量就是A的变量 Fraction add(Fraction f) 在 ...
- HttpServletRequest request方法详解
//1.获取请求参数 //获取参数的单个值,如有多个则只返回第一个 String parameter1 = request.getParameter("demo"); //获取参数 ...
- Asp.Net MVC向视图View传值的三种方法
本文将总结Asp.Net MVC向视图View传值的三种常见的方法: ----------------------------------------------------------------- ...