poj 3311 Hie with the Pie(floyd+状态压缩)
来源:互联网 发布:java开发视频网站源码 编辑:程序博客网 时间:2024/05/23 14:57
dp、状压 深似海
枚举所有到达状态(这个人送ps的所有可能),在某个状态i下,枚举这一状态下到达某个城市(包含在状态i下)的最短距离,在枚举某个城市时,需要枚举除这个城市外其他所有城市(包含在状态i下)到这个城市的最短距离(~‘~’)
#include <iostream>#include <stdio.h>#include <algorithm>using namespace std;const int inf=0x3f3f3f3f;int a[12][12];int dp[(1<<11)][12];int main(){ int n,ans; while (scanf("%d",&n)&&n) { for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) scanf("%d",&a[i][j]); for (int i=0;i<=n;i++) for (int j=0;j<=n;j++) for (int k=0;k<=n;k++) if (a[i][k]+a[k][j]<a[i][j]) a[i][j]=a[i][k]+a[k][j]; for (int i=1;i<(1<<n);i++)// 枚举当前状态 for (int j=1;j<=n;j++) //枚举状态下的某个城市 { if (i&(1<<(j-1))) //j-1 从二进制考虑 { if (i==(1<<(j-1))) dp[i][j]=a[0][j];//如果这个状态下只有一个城市,则直接等于从ps店到这个城市 else { dp[i][j]=inf; for (int k=1;k<=n;k++) { if (k!=j&&(i&(1<<(k-1)))) dp[i][j]=min(dp[i-(1<<(j-1))][k]+a[k][j],dp[i][j]);//i-(1<<(j-1)) 意思为除这个城市的状态 } } } } ans=dp[(1<<n)-1][1]+a[1][0]; for (int i=2;i<=n;i++) if (dp[(1<<n)-1][i]+a[i][0]<ans) ans=dp[(1<<n)-1][i]+a[i][0]; printf("%d\n",ans); } return 0;}
阅读全文
0 0
- POJ 3311 Hie with the Pie (Floyd+状态压缩)
- poj 3311 Hie with the Pie(floyd+状态压缩)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj 3311 Hie with the Pie floyd+状态压缩dp
- POJ 3311 Hie with the Pie(Floyd+状态压缩DP)
- poj 3311 Hie with the Pie(状态压缩DP+floyd)
- POJ 3311 Hie with the Pie 状态压缩DP+floyd
- poj 3311 Hie with the Pie (floyd+状态压缩dp~)
- poj 3311 Hie with the Pie (floyd+状态压缩dp)
- POJ 3311 Hie with the Pie(状态压缩DP+Floyd)
- Poj 3311 Hie with the Pie 状态压缩(TSP)
- POJ 3311 Hie with the Pie(状态压缩DP)
- POJ 3311 Hie with the Pie(状态压缩DP)
- POJ 3311 Hie with the Pie(状态压缩dp)
- poj 3311 Hie with the Pie (弗洛伊德+状态压缩)
- poj 3311 Hie with the Pie(状态压缩dp)
- 浅谈Mysql权限控制
- HashMap并发出现的问题
- Java学习——循环结构之跳转语句
- hdu 3172 Virtual Friends
- 第二数学归纳法:硬币问题和堆垛游戏
- poj 3311 Hie with the Pie(floyd+状态压缩)
- 【NOIP模拟】 (11.6) T1 Blash数集
- 转--强大的矩阵奇异值分解(SVD)及其应用
- python3中range函数的变化
- Azure ARM虚拟机部署反恶意软件-安全扩展
- geoserver集成以及部署arcgis server瓦片数据
- 搜索好题集合2
- 中缀表达式 转换为 前缀 后缀表达式
- redis如何修改登录密码?设置的密码为什么无效?以及如何更改端口?