Hie with the Pie POJ - 3311

Hie with the Pie POJ - 3311

这题是类TSP,与TSP区别是可以重复经过节点,只需floyed预处理出指定两点间(走过任意数量点)的最短路即可。

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 using namespace std;
 5 int n,anss;
 6 int dis[12][12];
 7 int ans[12][3000];
 8 int main()
 9 {
10     int i,j,k;
11     scanf("%d",&n);
12     while(n!=0)
13     {
14         for(i=0;i<=n;i++)
15             for(j=0;j<=n;j++)
16                 scanf("%d",&dis[i][j]);
17         //memset(dis,0x3f,sizeof(dis));
18         for(k=0;k<=n;k++)
19             for(i=0;i<=n;i++)
20                 for(j=0;j<=n;j++)
21                     dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
22         memset(ans,0x3f,sizeof(ans));
23         anss=0x3f3f3f3f;
24         //ans[0][0]=0;
25         for(i=1;i<=n;i++)
26             ans[i][1<<(i-1)]=dis[0][i];
27         for(i=1;i<(1<<n);i++)
28             for(j=1;j<=n;j++)
29                 for(k=1;k<=n;k++)
30                     //if((i>>(k-1)&1)
31                     if(i&(1<<(k-1)))
32                         ans[j][i]=min(ans[j][i],ans[k][i^(1<<(j-1))]+dis[k][j]);
33         for(i=1;i<=n;i++)
34             anss=min(anss,ans[i][(1<<n)-1]+dis[i][0]);
35         printf("%d\n",anss);
36         scanf("%d",&n);
37     }
38     return 0;
39 }
时间: 2024-11-05 19:42:38

Hie with the Pie POJ - 3311的相关文章

Hie with the Pie (POJ 3311) 旅行商问题

昨天想练习一下状态压缩,百度搜索看到有博客讨论POJ 3311,一看就是简单的旅行商问题,于是快速上手写了状态压缩,死活样例都没过... 画图模拟一遍原来多个城市可以重复走,然后就放弃思考了... 刚刚把这个无聊的问题解决了,简单的Floyd+状压. 所谓Floyd算法,我在暑训的博客里提过,复杂度O(n3),但当时数据量太大不适合使用,这里刚好派上了用场. #include<cstdio> #include<iostream> #include<cstring> #i

POJ 3311 Hie with the Pie (Floyd + 状压dp 简单TSP问题)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5019   Accepted: 2673 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

POJ 3311 Hie with the Pie (状压DP)

状态压缩DP dp[i][j]表示在i状态(用二进制表示城市有没有经过)时最后到达j城市的最小时间 转移方程dp[i][j]=min(dp[i][k]+d[k][j],dp[i][j]) d[k][j]是k城市到j城市的最短距离 要先用flody处理 #include<bits.stdc++.h> using namespace std; int d[20][20],dp[1<<11][20]; int n,m; void flody() { for(int k=0;k<=n

poj 3311 Hie with the Pie 【旅行商+回原点】

题目:poj 3311 Hie with the Pie 题意:就是批萨点小二要送批萨,然后给你每个点的距离,有向的,然后让你就走一次回到原点的最短路. 分析:因为给出的是稠密图,所以要处理一下最短路,floyd 然后TSP就好. 枚举每个状态,对于当前状态的每一个已经走过的点,枚举是从那个点走过来的,更新最短路 状态:dp[st][i] :st状态下走到点 i 的最短路 转移方程:dp[st][i]=min(dp[st&~(1<<i)][j]+mp[j][i],dp[st][i]);

POJ 3311 Hie with the Pie TSP+Floyd

保证每个点访问过一次就行,然后会到原点. 这种情况可以先做一边floyd,然后跑tsp就好. #include <cstdio> #include <cstring> #include <iostream> #include <map> #include <set> #include <vector> #include <string> #include <queue> #include <deque&g

POJ 3311 Hie with the Pie

Hie with the Pie Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on PKU. Original ID: 331164-bit integer IO format: %lld      Java class name: Main The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast a

poj 3311 Hie with the Pie dp+状压

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4671   Accepted: 2471 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

poj 3311 Hie with the Pie (状态压缩+最短路)

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4491   Accepted: 2376 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo

状压DP [POJ 3311] Hie with the Pie

Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 4712   Accepted: 2502 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can affo