POJ 3311 Hie with the Pie TSP问题 -
来源:互联网 发布:莫言生死疲劳淘宝 编辑:程序博客网 时间:2024/05/16 14:55
题目地址:http://poj.org/problem?id=3311
从0出发 遍历所有其他城市再回到0,求最短路径和
因为每个城市可以走多次,所以要事先用Floyd算法求出任意两点最短路径
代码如下:
#include<iostream>#include<cstring>#include<string>#include<cstdio>#include<algorithm>using namespace std;const int INF=0x3f3f3f3f;int G[10+5][10+5];int d[1<<11][11];int dist[10+5][10+5];void Floyd(int n){for(int i=0;i<n;i++)for(int j=0;j<n;j++)dist[i][j]=G[i][j];for(int k=0;k<n;k++) //中间经过k-1个点for(int i=0;i<n;i++) for(int j=0;j<n;j++) dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);}int main(){int n; while(cin>>n&&n){for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)cin>>G[i][j];Floyd(n+1);int t=(1<<(n+1))-1;int s=1;memset(d,INF,sizeof(d));d[s][0]=0;for(int i=1;i<=t;i+=2) //i 状态 走过的城市 for(int j=0;j<=n;j++) //i-1<<j 到i状态时,最后一步走到(1<<j)那里了 {if(i&(1<<j))for(int k=0;k<=n;k++) // 对于i-1<<j状态,最后走到1<<k了 if(j!=k&&(i&(1<<k))) d[i][j]=min(d[i][j],d[i-(1<<j)][k]+dist[k][j]);}int ans=INF;for(int i=0;i<=n;i++) ans=min(ans,d[t][i]+dist[i][0]);cout<<ans<<endl;}return 0;}
0 0
- poj 3311 Hie with the Pie (TSP)
- poj 3311 Hie with the Pie (TSP问题)
- 【DP】 POJ 3311 Hie with the Pie 状压 TSP问题
- POJ 3311 Hie with the Pie(TSP问题DP解)
- POJ 3311 Hie with the Pie TSP问题 -
- POJ 3311 Hie with the Pie(经典TSP问题)
- POJ 3311 Hie with the Pie(经典TSP问题)
- POJ 3311--Hie with the Pie(状压dp+TSP)
- POJ 3311 Hie with the Pie -- TSP 状态压缩dp
- Poj 3311 Hie with the Pie 状态压缩(TSP)
- poj 3311 Hie with the Pie(TSP 状态压缩)
- POJ 3311-Hie with the Pie(floyd+TSP 状压DP)
- POJ 3311 Hie with the Pie - 状压dp【TSP】
- Poj 3311 Hie with the Pie 【TSP+floyd】
- poj 3311 Hie with the Pie(TSP ,状压dp)
- Pku oj 3311 Hie with the Pie(TSP问题)
- HDU 3311 Hie with the Pie(状压dp+TSP问题)
- POJ 3311 Hie with the Pie (TSP问题 状压DP解法)
- [Leetcode] Peeking Iterator
- bzoj 2818: Gcd 线性筛求欧拉函数
- 读thinking in java笔记(九):内部类
- Gulp 自动化你的前端
- POJ 1753 Flip Game(枚举+回溯法)
- POJ 3311 Hie with the Pie TSP问题 -
- Nginx源码分析 - Event事件篇 - Event模块的进程初始化ngx_event_process_init
- Linux 基础学习
- day14_字典
- 虚拟机开新端口
- 教程:简单十步,在 iTunes 申请 App Store 退款
- epoll实现高并发聊天室
- 常见的java线程面试题
- Activity透明/半透明效果的设置transparent(两种实现方法)