题目链接:http://poj.org/problem?id=3311

题目:

题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离。

思路:我们首先用floyd预处理出每两个城市间的最短路,然后采用状压dp来解题。dp[i][j]表示在i这种状压下以j为目标城市的最短距离,i的二进制中x位为1表示到了城市x,为0表示没到城市x,则转移方程为dp[i][j] = min(dp[i][j], dp[i^(1<<(j-1))][k] + dis[k][j]),其中i^(1<<(j-1))表示没到城市j。

代码实现如下:

 #include <set>
#include <map>
#include <queue>
#include <stack>
#include <cmath>
#include <bitset>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, int> pli;
typedef pair<int, ll> pil;;
typedef pair<int, int> pii;
typedef unsigned long long ull; #define lson i<<1
#define rson i<<1|1
#define bug printf("*********\n");
#define FIN freopen("D://code//in.txt", "r", stdin);
#define debug(x) cout<<"["<<x<<"]" <<endl;
#define IO ios::sync_with_stdio(false),cin.tie(0); const double eps = 1e-;
const int mod = ;
const int maxn = 1e6 + ;
const double pi = acos(-);
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f; int n;
int dp[][], dis[][]; void floyd() {
for(int k = ; k <= n; k++) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
} int main() {
//FIN;
while(~scanf("%d", &n) && n) {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
scanf("%d", &dis[i][j]);
}
}
floyd();
memset(dp, -, sizeof(dp));
dp[][] = ;
int tot = << (n+);
for(int i = ; i < tot; i++) {
for(int j = ; j <= n; j++) {
if(i & ( << (j-))) {
if(i == (<<(j-))) dp[i][j] = dis[][j];
else {
dp[i][j] = inf;
for(int k = ; k <= n; k++) {
if(i & ( << (k-)) && j != k) {
dp[i][j] = min(dp[i][j], dp[i^(<<(j-))][k] + dis[k][j]);
}
}
}
}
}
}
int ans = inf;
for(int i = ; i <= n; i++) {
ans = min(ans, dp[(<<n)-][i] + dis[i][]); //最后要从某一座城市回到起点
}
printf("%d\n", ans);
}
return ;
}

Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)的更多相关文章

  1. POJ 3311 Hie with the Pie 最短路+状压DP

    Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11243   Accepted: 5963 ...

  2. 【loj6177】「美团 CodeM 初赛 Round B」送外卖2 Floyd+状压dp

    题目描述 一张$n$个点$m$条边的有向图,通过每条边需要消耗时间,初始为$0$时刻,可以在某个点停留.有$q$个任务,每个任务要求在$l_i$或以后时刻到$s_i$接受任务,并在$r_i$或以前时刻 ...

  3. [hdu5418 Victor and World]floyd + 状压DP 或 SPFA

    题意:给n个点,m条边,每次只能沿边走,花费为边权值,求从1出发经过所有其它点≥1次最后回到1的最小花费. 思路: 状压DP.先用Floyd得到任意两点间的最短距离,转移时沿两个点的最短路转移.此时的 ...

  4. poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp

    题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间 ...

  5. POJ 3311 Hie with the Pie floyd+状压DP

    链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...

  6. hdu5418--Victor and World(floyd+状压dp)

    题目链接:点击打开链接 题目大意:有n个城市.在n个城市之间有m条双向路.每条路有一个距离.如今问从1号城市去游览其他的2到n号城市最后回到1号城市的最短路径(保证1能够直接或间接到达2到n).(n& ...

  7. codevs2800送外卖(floyd+状压dp)

    2800 送外卖  时间限制: 2 s  空间限制: 256000 KB  题目等级 : 钻石 Diamond     题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份东 ...

  8. HDU - 4284 Travel(floyd+状压dp)

    Travel PP loves travel. Her dream is to travel around country A which consists of N cities and M roa ...

  9. poj3311(状压dp)

    题目连接:http://poj.org/problem?id=3311 题意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. 分析:跑一遍Floyd ...

随机推荐

  1. jetty之maven配置

    <!-- jetty 插件配置 --><plugin> <groupId>org.mortbay.jetty</groupId> <artifac ...

  2. [Google] 看雪论坛: 安卓碎片化的情况

    2018年10月28日早间消息,谷歌方面发布了Android各版本的最新份额数据,截止到10月26日.即便是已经推出3个月了,Android 9 Pie系统的用户数仍旧没有超过0.1%,导致未出现在榜 ...

  3. (转)利用PHP的debug_backtrace函数,实现PHP文件权限管理、动态加载 【反射】

    原文地址:http://www.cnblogs.com/melonblog/archive/2013/05/09/3062303.html 原文作者:豆浆油条 - melon 本文示例代码测试环境是W ...

  4. this.AcceptButton = button1的用法:

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  5. 'phantomjs.exe' executable needs to be in PATH. (selenium PhantomJS python)

    今天selenium PhantomJS python用了下,发现报错,提示我:'phantomjs.exe' executable needs to be in PATH. from seleniu ...

  6. 【前端学习笔记03】JavaScript对象相关方法及封装

    //Object.create()创建对象 var obj = Object.create({aa:1,bb:2,cc:'c'}); obj.dd = 4; console.log(obj.cc); ...

  7. HDU 6035 Colorful Tree(dfs)

    题意:一棵有n个点的树,树上每个点都有颜色c[i],定义每条路径的值为这条路径上经过的不同颜色数量和.求所有路径的值的和. 可以把问题转化为对每种颜色有多少条不同的路径至少经过这种颜色的点,然后加和. ...

  8. 51nod 1674 区间的价值V2(思维+拆位+尺取法)

    最近被四区题暴虐... 题意:lyk拥有一个区间. 它规定一个区间的价值为这个区间中所有数and起来的值与这个区间所有数or起来的值的乘积. 例如3个数2,3,6.它们and起来的值为2,or起来的值 ...

  9. 【bzoj5173】[Jsoi2014]矩形并 扫描线+二维树状数组区间修改区间查询

    题目描述 JYY有N个平面坐标系中的矩形.每一个矩形的底边都平行于X轴,侧边平行于Y轴.第i个矩形的左下角坐标为(Xi,Yi),底边长为Ai,侧边长为Bi.现在JYY打算从这N个矩形中,随机选出两个不 ...

  10. [十七]SpringBoot 之 使用自定义的properties

    Springboot使用application.properties默认了很多配置.但需要自己添加一些配置的时候,我们应该怎么做呢. 如果继续在application.properties中添加 如: ...