2017-08-11 21:10:21

艾教写的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
#include<set>
#include<stack>
using namespace std;
typedef long long LL;
int n;
int dis[][];
int dp[][<<];
int min(int x,int y)
{
if (x==-) return y;
if (y==-) return x;
if (x<=y) return x;
return y;
}
int main()
{
while(scanf("%d",&n) && n)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>dis[i][j];
for(int k=;k<=n;k++)
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
if (i!=j && j!=k && i!=k)
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
n++;
memset(dp,-,sizeof(dp));
dp[][]=;
for(int state=;state<(<<n);state++)
{
for(int i=;i<n;i++)
if (dp[i][state]!=-)
{
for(int j=;j<n;j++)
if (((<<j)&state)==)
{
dp[j][state|(<<j)]=min(dp[j][state|(<<j)],dp[i][state]+dis[i][j]);
}
}
}
int ans=dp[][(<<n)-];
cout<<ans<<endl;
}
}

TSP - 状态压缩dp的更多相关文章

  1. HOJ 2226&POJ2688 Cleaning Robot(BFS+TSP(状态压缩DP))

    Cleaning Robot Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4264 Accepted: 1713 Descri ...

  2. BFS+状态压缩DP+二分枚举+TSP

    http://acm.hdu.edu.cn/showproblem.php?pid=3681 Prison Break Time Limit: 5000/2000 MS (Java/Others)   ...

  3. TSP 旅行商问题(状态压缩dp)

    题意:有n个城市,有p条单向路径,连通n个城市,旅行商从0城市开始旅行,那么旅行完所有城市再次回到城市0至少需要旅行多长的路程. 思路:n较小的情况下可以使用状态压缩dp,设集合S代表还未经过的城市的 ...

  4. POJ 3311 Hie with the Pie(Floyd+状态压缩DP)

    题是看了这位的博客之后理解的,只不过我是又加了点简单的注释. 链接:http://blog.csdn.net/chinaczy/article/details/5890768 我还加了一些注释代码,对 ...

  5. poj 3311 Hie with the Pie(状态压缩dp)

    Description The Pizazz Pizzeria prides itself or more (up to ) orders to be processed before he star ...

  6. Light OJ 1316 A Wedding Party 最短路+状态压缩DP

    题目来源:Light OJ 1316 1316 - A Wedding Party 题意:和HDU 4284 差点儿相同 有一些商店 从起点到终点在走过尽量多商店的情况下求最短路 思路:首先预处理每两 ...

  7. poj 3311(状态压缩DP)

    poj  3311(状态压缩DP) 题意:一个人送披萨从原点出发,每次不超过10个地方,每个地方可以重复走,给出这些地方之间的时间,求送完披萨回到原点的最小时间. 解析:类似TSP问题,但是每个点可以 ...

  8. 学习笔记:状态压缩DP

    我们知道,用DP解决一个问题的时候很重要的一环就是状态的表示,一般来说,一个数组即可保存状态.但是有这样的一些题 目,它们具有DP问题的特性,但是状态中所包含的信息过多,如果要用数组来保存状态的话需要 ...

  9. 最短路+状态压缩dp(旅行商问题)hdu-4568-Hunter

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4568 题目大意: 给一个矩阵 n*m (n m<=200),方格里如果是0~9表示通过它时要花 ...

随机推荐

  1. Python多股票同周期可视化

    import warnings warnings.filterwarnings("ignore") import numpy as np import pandas as pd i ...

  2. 网络编程 - socket通信/粘包/文件传输/udp - 总结

    socket通信 1.简单的套接字通信 import socket phone = socket.socket(socket.AF_INET,socket.SOCK_STREAM) phone.bin ...

  3. 找新朋友---hdu1286(欧拉函数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1286 欧拉函数:对正整数n,欧拉函数是求少于n的数中与n互质的数的数目: 素数(质数)指在一个大于1的 ...

  4. git别名

    git config --global alias.lg 'log --oneline --all --graph --decorate'

  5. python插入记录后获取最后一条数据的id

    python插入记录后取得主键id的方法(cursor.lastrowid和conn.insert_id()) 参考:https://blog.csdn.net/qq_37788558/article ...

  6. Java集合—Set(转载)

    Set集合中包含了三个比较重要的实现类:HashSet.TreeSet和EnumSet.本篇文章将重点介绍这三个类. 一.HashSet类 HashSet简介 HashSet是Set接口的典型实现,实 ...

  7. 小tip: 使用SVG寥寥数行实现圆环loading进度效果

    二.正文 设计师设计了一个图片上传圆环loading进度效果.如下截图: 首先,CSS3是可以实现的,以前写过一篇转大饼的文章:“CSS3实现鸡蛋饼饼状图loading等待转转转”.原理跟这个一模一样 ...

  8. ui-select : There is no "X"(delete button) with selectize theme, when allow-clear="true"

    but add allow-clear="true"For Bootstrap and Select2 themes, it's working perfectly. reason ...

  9. .globl分析

    Uboot中常看到.globl .globl _start _start: b reset .align .globl _TEXT_BASE _TEXT_BASE: .globl _start  /* ...

  10. 转载SQL_trace 和10046使用

    SQL_TRACE是Oracle提供的用于进行SQL跟踪的手段,是强有力的辅助诊断工具.在日常的数据库问题诊断和解决中,SQL_TRACE是非常常用的方法.本文就SQL_TRACE的使用作简单探讨,并 ...