poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp
题目链接
题意
给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点)。
思路
因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间的最短路径。
接下来就是状压dp的部分。
将已经经过的点的状态用\(state\)表示,
则\(dp[state][k]\)表示当前到达点\(k\)后状态为\(state\)时的最短路径长度。
\]
可用记忆化搜索。
Code
#include <cstdio>
#include <iostream>
#include <cstring>
#include <climits>
#define F(i, a, b) for (int i = (a); i < (b); ++i)
#define F2(i, a, b) for (int i = (a); i <= (b); ++i)
#define dF(i, a, b) for (int i = (a); i > (b); --i)
#define dF2(i, a, b) for (int i = (a); i >= (b); --i)
#define maxn 12
#define maxs 1100
using namespace std;
typedef long long LL;
int n, a[maxn][maxn], dp[maxs][maxn];
bool vis[maxs][maxn];
void floyd() {
    F2(k, 0, n) {
        F2(i, 0, n) {
            F2(j, 0, n) {
                if (i==j||i==k||j==k) continue;
                a[i][j] = min(a[i][j],a[i][k]+a[k][j]);
            }
        }
    }
}
int dfs(int state, int p) {
    if (state==(1<<(p-1))) return dp[state][p] = a[0][p];
    if (vis[state][p]) return dp[state][p];
    vis[state][p] = true;
    int ans = INT_MAX, sta = state&~(1<<(p-1));
    F2(i, 1, n) {
        if (state&(1<<(i-1)) && i!=p) {
            ans = min(ans, dfs(sta, i)+a[i][p]);
        }
    }
    return dp[state][p] = ans;
}
void work() {
    memset(dp, 0, sizeof dp);
    memset(vis, 0, sizeof vis);
    F2(i, 0, n) {
        F2(j, 0, n) {
            scanf("%d", &a[i][j]);
        }
    }
    floyd();
    int ans = INT_MAX;
    F2(i, 1, n) ans = min(ans, dfs((1<<n)-1, i)+a[i][0]);
    printf("%d\n", ans);
}
int main() {
    while (scanf("%d", &n) != EOF && n) work();
    return 0;
}
												
											poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 floyd + 状压dp的更多相关文章
- poj 3311    Hie with the Pie
		
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
 - Hie with the Pie(POJ3311+floyd+状压dp+TSP问题dp解法)
		
题目链接:http://poj.org/problem?id=3311 题目: 题意:n个城市,每两个城市间都存在距离,问你恰好经过所有城市一遍,最后回到起点(0)的最短距离. 思路:我们首先用flo ...
 - POJ 3311 Hie with the Pie(状压DP + Floyd)
		
题目链接:http://poj.org/problem?id=3311 Description The Pizazz Pizzeria prides itself in delivering pizz ...
 - poj 3311 Hie with the Pie dp+状压
		
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4671 Accepted: 2471 ...
 - poj 3311 Hie with the Pie (TSP问题)
		
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4491 Accepted: 2376 ...
 - POJ 3311 Hie with the Pie  最短路+状压DP
		
Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11243 Accepted: 5963 ...
 - POJ 3311 Hie with the Pie(DP状态压缩+最短路径)
		
题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...
 - [POJ 3311]Hie with the Pie——谈论TSP难题DP解决方法
		
主题连接:  id=3311">http://poj.org/problem?id=3311 题目大意:有n+1个点,给出点0~n的每两个点之间的距离,求这个图上TSP问题的最小解 ...
 - POJ 3311 Hie with the Pie floyd+状压DP
		
链接:http://poj.org/problem?id=3311 题意:有N个地点和一个出发点(N<=10),给出全部地点两两之间的距离,问从出发点出发,走遍全部地点再回到出发点的最短距离是多 ...
 
随机推荐
- 5.Cisco Packet Tracer里关于交换机或路由器配置文件和系统映像备份与恢复
			
我们会将交换机或路由器的配置文件和系统镜像直接备份到tftp服务器上,所以我们需要准备一台tftp的服务器 1我们需要给服务器配一个ip地址,给路由器的f0/1端口配置一个ip地址,路由器与服务器能相 ...
 - Nginx 配置支持 WAF
			
WAF(Web Application Firewall),中文名叫做“Web应用防火墙” WAF的定义是这样的:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提 ...
 - HDU 1423 Greatest Common Increasing Subsequence(LCIS)
			
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
 - JZOJ 1267. 路障
			
1267. 路障(block.pas/c/cpp) (File IO): input:block.in output:block.out Time Limits: 1000 ms Memory Li ...
 - 使用shell脚本添加用户
			
该文演示如何使用shell脚本完成添加用户,首先进行一个判断,如果用户存在,提示该用户已经存在,否则进行添加新的用户. 示例代码如下: #!/bin/bash grep_user() { R=`gre ...
 - centos7.3网络配置
			
一.关闭NetworkManager 默认状态下最小化安装使用NetworkManager这个服务来控制联网的,但是这个配置在配置生产环境服务器时一般不会使用,而是使用系统自带的network服务,更 ...
 - shell脚本中的交互式输入自动化
			
shell中有时我们需要交互,但是呢我们又不想每次从stdin输入,想让其自动化,这时我们就要使shell交互输入自动化了. 1 利用重定向 重定向的方法应该是最简单的 例: 以下的te ...
 - 笔记-算法-hash以及hashlib使用
			
算法-hash和hash表以及hashlib使用 1. 简介 1.1. hash Hash(散列/哈希),就是把任意长度的输入(预映射pre-image)通过散列算法变换成固定长度的输 ...
 - eclipse快捷键(shift+ctrl+l能出来所有的快捷键)
			
[ALT+/]此快捷键为用户编辑的好帮手,能为用户提供内容的辅助,不要为记不全方法和属性名称犯愁,当记不全类.方法和属性的名字时,多体验一下[ALT+/]快捷键带来的好处吧. 2 [Ctrl+O]显示 ...
 - 配置Spring.NET
			
先引入关键的程序集 Common.Logging.dll Spring.Core.dll 在配置文件配置中: <configSections> ...... <sectionGrou ...