Hie with the Pie POJ
来源:互联网 发布:怎样在淘宝上联系卖家 编辑:程序博客网 时间:2024/05/22 03:05
Description
The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortunately, due to cutbacks, they can afford to hire only one driver to do the deliveries. He will wait for 1 or more (up to 10) orders to be processed before he starts any deliveries. Needless to say, he would like to take the shortest route in delivering these goodies and returning to the pizzeria, even if it means passing the same location(s) or the pizzeria more than once on the way. He has commissioned you to write a program to help him.
Input
Input will consist of multiple test cases. The first line will contain a single integer n indicating the number of orders to deliver, where 1 ≤ n ≤ 10. After this will be n + 1 lines each containing n + 1 integers indicating the times to travel between the pizzeria (numbered 0) and the n locations (numbers 1 to n). The jth value on the ith line indicates the time to go directly from location i to location j without visiting any other locations along the way. Note that there may be quicker ways to go from i to j via other locations, due to different speed limits, traffic lights, etc. Also, the time values may not be symmetric, i.e., the time to go directly from location i to j may not be the same as the time to go directly from location j to i. An input value of n = 0 will terminate input.
Output
For each test case, you should output a single number indicating the minimum time to deliver all of the pizzas and return to the pizzeria.
Sample Input
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0
Sample Output
8
大致题意:从起点0开始去其他n个点送外卖,然后再回到起点0,每个点可经过多次,问最少花费时间多少
思路:先用floyd求出每个点之间的最短里,然后用状压dp来搞,dp[i][state]表示到达点i时的状态为state时的最少花费。
代码如下
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; #define ll long long int N;int dis[20][20];int dp[16][(1<<16)]; int min(int x,int y){ if(x==-1) return y; if(y==-1) return x; if(x>y) return y; return x;}int main () { while(cin>>N) { if(N==0) break; for(int i=0;i<=N;i++) for(int j=0;j<=N;j++) cin>>dis[i][j]; for(int i=0;i<=N;i++)//用floyd求出两点之间的最短路 for(int j=0;j<=N;j++) if(i!=j) for(int k=0;k<=N;k++) if(k!=i&&k!=j) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); memset(dp,-1,sizeof(dp)); dp[0][0]=0; for(int state=0;state<=(1<<(N+1))-1;state++)//枚举此时所在点的状态 for(int i=0;i<=N;i++)//枚举此时所在点 { if(dp[i][state]!=-1) { for(int j=0;j<=N;j++)//枚举下一步达到的点 { if((1<<j)^state) dp[j][(1<<j)|state]=min(dp[j][(1<<j)|state],dp[i][state]+dis[i][j]); } } } cout<<dp[0][(1<<(N+1))-1]<<endl; } return 0; }
- Hie with the Pie POJ
- Hie with the Pie POJ
- Hie with the Pie POJ
- poj 3311 Hie with the Pie
- POJ 3311 Hie with the Pie
- poj 3311 Hie with the Pie
- poj 3311 Hie with the Pie
- poj 3311 Hie with the Pie (TSP)
- POJ 3311 — Hie with the Pie
- POJ 3311 Hie with the Pie
- poj 3311 Hie with the Pie (DP)
- POJ 3311 Hie with the Pie
- POJ 3311 Hie with the Pie
- POJ - 3311 Hie with the Pie
- POJ 3311 - Hie with the Pie
- POJ 3311 Hie with the Pie
- POJ 3311 Hie with the Pie
- Hie with the Pie
- spring源码分析之FactoryBean
- 在windows10下安装CPU only模式的caffe+vs2013+anaconda笔记
- 循环队列的操作
- HTML基本格式
- js数据的深度迭代
- Hie with the Pie POJ
- Python正则表达式
- Python进阶
- php-DOM部分基础知识总结与实例(V客学院知识分享)
- 9.28 考试总结
- 线性表的建立和删除
- .gitignore无效,不能过滤某些文件
- leetcode
- 图像合成--异质 案例