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,就是动态性决策规划 ...
随机推荐
- vue条形码+二维码
<template> <section style="height: 100vh;" class="bg"> <div class ...
- 安装go环境
~~~~~~~~~~~~~~~~~~~~~~~~~~~~go项目运行时的依赖包安装问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~1.安装go环境 wget https://dl.goog ...
- linux下使用自带mail发送邮件
linux下使用自带mail发送邮件 mailx工具说明: linux可以通过安装mailx工具,mailx是一个小型的邮件发送程序,一般可以通过该程序在linux系统上,进行监控linux系统状态并 ...
- 如何使用Hive集成Solr?
(一)Hive+Solr简介 Hive作为Hadoop生态系统里面离线的数据仓库,可以非常方便的使用SQL的方式来离线分析海量的历史数据,并根据分析的结果,来干一些其他的事情,如报表统计查询等. So ...
- 2019-7-29-asp-dotnet-core-从-Frp-获取用户真实-IP-地址
title author date CreateTime categories asp dotnet core 从 Frp 获取用户真实 IP 地址 lindexi 2019-07-29 08:28: ...
- 7.spark运行模式
sparkbin目录下 ./pyspark --help http://spark.apache.org/docs/latest/submitting-applications.h ...
- WPF命令好状态刷新机制
https://blog.csdn.net/WPwalter/article/details/90344470 this.DispatcherInvoke(() => { System.Wind ...
- Java 虚拟机 - GC机制
GC机制的一些总结 https://blog.csdn.net/super_qing_/article/details/85263991 https://blog.csdn.net/yhyr_ycy/ ...
- PHP正则使用技巧1
$pattern="/<div class=\"cover g-playicon\">(.*?)>/s"; 意思为抓取<div clas ...
- js面试总结3
异步和单线程 题目: 1.同步和异步的区别? 2.一个关于setTimeout的笔试题. 3.前段使用异步的场景有哪些? 什么是异步? console.log(100) setTimeout(func ...