poj 3311 Hie with the Pie (floyd+状态压缩dp)
2014-06-04 21:06
387 查看
求从0点出发 经过每点后回到0点的最小花费。 先用floyd求一下每两点的最短距离 ,然后dp就可以了。略水。。。
#include <iostream> #include <cstring> using namespace std; #define INF 0x3f3f3f3f int dp[3000][12]; int map[12][12]; void floyd(int n) { for (int k=0;k<=n;++k) { for (int i=0;i<=n;++i) { for (int j=0;j<=n;++j) { if (map[i][k]!=INF && map[k][j]!=INF &&map[i][k]+map[k][j]<map[i][j]) { map[i][j]=map[i][k]+map[k][j]; } } } } } int main() { int n; while(cin>>n,n) { int w=1<<n; memset(map,INF,sizeof(map)); memset(dp,INF,sizeof(dp)); for(int i=0;i<=n;i++) { for(int j=0;j<=n;j++) cin>>map[i][j]; } floyd(n); for(int i=1;i<=n;i++) dp[1<<(i-1)][i]=map[0][i]; for(int i=0;i<w;i++) { for(int j=1;j<=n;j++) { if(dp[i][j]==INF) continue; for(int k=1;k<=n;k++) { if(k==j||i&(1<<(k-1))) continue; dp[i+(1<<(k-1))][k]=min(dp[i+(1<<(k-1))][k],dp[i][j]+map[j][k]); } } } int minn=INF; for(int i=1;i<=n;i++) { if(minn>dp[w-1][i]+map[i][0]) minn=dp[w-1][i]+map[i][0]; } cout<<minn<<endl; } return 0; }
相关文章推荐
- 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)
- 状压DP (Floyd+状态压缩 )——Hie with the Pie( POJ 3311 )
- poj 3311 Hie with the Pie (floyd+状态压缩dp~)
- POJ 3311 Hie with the Pie(状态压缩DP)
- poj 3311 Hie with the Pie(状态压缩DP+floyd)
- poj 3311 Hie with the Pie(状态压缩dp)
- poj 3311 Hie with the Pie(floyd+状态压缩)
- 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(floyd+状态压缩)
- POJ 3311 Hie with the Pie(状态压缩DP+Floyd)
- POJ3311——Hie with the Pie(floyd,状态压缩dp,旅行商)
- poj 3311 Hie with the Pie(状态压缩DP)
- 状态压缩DP-Hie With the Pie(POJ 3311)
- POJ 3311 Hie with the Pie (Floyd+状态压缩)
- POJ 3311 Hie with the Pie【状态压缩DP】