Hie with the Pie (POJ 3311) 旅行商问题
昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过。。。
画图模拟一遍原来多个城市可以重复走,然后就放弃思考了。。。
刚刚把这个无聊的问题解决了,简单的Floyd+状压。
所谓Floyd算法,我在暑训的博客里提过,复杂度O(n3),但当时数据量太大不适合使用,这里刚好派上了用场。
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std; const int INF = 0x3f3f3f3f;
int dis[][], n;
int dp[<<][]; // dp[S][u] 从u出发经过S的剩下城市走到0的最短时间 int dfs(int S, int u)
{
if(dp[S][u]>)
return dp[S][u]; if(S==(<<(n+))- && u==)
return dp[S][u] = ; int res = INF;
for(int v=;v<=n;v++)
{
if(v!=u && !((S>>v)&))
res = min(res, dfs(S|(<<v), v)+dis[u][v]); }
return dp[S][u] = res;
}
int main()
{
while(cin>>n)
{
if(n==) break; for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
cin>>dis[i][j]; // 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]);
memset(dp, -, sizeof(dp));
cout<<dfs(, )<<endl;
}
return ;
}
交一发直接AC了感觉索然无味啊。。。还是要多找点难题刷刷 0.0
Hie with the Pie (POJ 3311) 旅行商问题的更多相关文章
- Hie with the Pie POJ - 3311
Hie with the Pie POJ - 3311 The Pizazz Pizzeria prides itself in delivering pizzas to its customers ...
- 状压dp+floyed(C - Hie with the Pie POJ - 3311 )
题目链接:https://cn.vjudge.net/contest/276236#problem/C 题目大意: 给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任 ...
- poj 3311 Hie with the Pie
floyd,旅游问题每个点都要到,可重复,最后回来,dp http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Me ...
- 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 ...
- Hie with the Pie(poj3311)
题目链接:http://poj.org/problem?id=3311 学习博客:https://blog.csdn.net/u013480600/article/details/19692985 H ...
- POJ3311 Hie with the Pie 【状压dp/TSP问题】
题目链接:http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS Memory Limit: 65536K Total ...
- Hie with the Pie
Hie with the Pie poj-3311 题目大意:n+1个点,伪旅行商问题. 注释:n<=10. 想法:咳咳,第一道状压dp,下面我来介绍一下状压dp. 所谓dp,就是动态性决策规划 ...
随机推荐
- Codeforces 479【F】div3
题目链接:http://codeforces.com/problemset/problem/977/F 题意:给你一串数字序列,让你求最长上升子序列,但是这个子序列呢,它的数字得逐渐连续挨着. 题解: ...
- CSIC_716_20191114【生成器、匿名函数、内置函数、三元表达式、列表生成式、生成器表达式】
生成器: 函数与yield连用,凡是函数中有yield的,调用该函数的时候均不会立即执行,而是会返回一个生成器. 生成器本质上是一个迭代器,需要通过 [生成器.__next__()]或者[nex ...
- bzoj2322 梦想封印
题意和题解见思路索引. 标程及易错点: #include<bits/stdc++.h> using namespace std; typedef long long ll; ll read ...
- 软件设计师_朴素模式匹配算法和KMP算法
1.从主字符串中匹配模式字符串(暴力匹配) 2. KMP算法
- EL fmt标签
c:formate 表达式需要传入的对象为date
- 尚学linux课程---5、linux操作系统介绍
尚学linux课程---5.linux操作系统介绍 一.总结 一句话总结: centos开源免费,用的特别多 1.库是什么意思? 没有执行入口的应用程序 2.linux和window下的动态库文件是什 ...
- getResourceAsStream的几种路径配置(properties文件的路径问题)
文章目录 getResourceAsStream有以下几种: getResourceAsStream有以下几种: Class.getResourceAsStream(String path) : pa ...
- PAT甲级——A1124 Raffle for Weibo Followers
John got a full mark on PAT. He was so happy that he decided to hold a raffle(抽奖) for his followers ...
- Spring 声明式事务管理(11)
案例分析 本案例是图书管理系统精简部分,在数据库中有3张表.分别保存图书库存.图书信息和用户信息.下面是建表SQL语句 1 DROP TABLE IF EXISTS store; 2 DROP TAB ...
- UMP系统功能 资源隔离