poj 3311 状压dp+float

来源:互联网 发布:收银软件交流群 编辑:程序博客网 时间:2024/06/14 16:28
Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 8134 Accepted: 4426

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

30 1 10 101 0 1 210 1 0 1010 2 10 00

Sample Output

8

Source

East Central North America 2006



0代表这个点没有走过了。
1代表这个点已经走过了。

float 处理一下图得到最短的路径。

dp[i][j] 代表状态 i 最后走的是  j点

因为倒着走和正着走会有bug 的而且最后要回去。

所以res 需要加上 maps[ end_point]【0】不能是 maps[0][end_point]

#include<stdio.h>#include<string.h>#include<algorithm>#define inf 0x3f3f3f3fusing namespace std;int maps[1003][1003];int dp[1<<12][20];int main(){    int n;    while(scanf("%d",&n),n)    {        memset(maps,0,sizeof(maps));        memset(dp,0,sizeof(dp));        for(int i=0; i<=n; i++)        {            for(int j=0; j<=n; j++)            {                scanf("%d",&maps[i][j]);            }        }        for(int k=0; k<=n; k++)        {            for(int i=0; i<=n; i++)            {                for(int j=0; j<=n; j++)                {                    maps[i][j]=min(maps[i][j],maps[i][k]+maps[k][j]);                }            }        }        for(int i=0; i<(1<<n); i++)        {            for(int j=0; j<=n; j++)            {                if(i&1<<(j-1))                {                    if(i==(1<<(j-1)))                        dp[i][j]=maps[0][j];                    else                    {                        dp[i][j]=inf;                        for(int k=0;k<=n;k++)                        {                            if(k!=j&&(i&1<<(k-1)))                            {                                dp[i][j]=min(dp[i][j],dp[i^(1<<(j-1))][k]+maps[k][j]);                            }                        }                    }                }            }        }        int res=inf;        for(int i=1;i<=n;i++)            res=min(res,dp[(1<<n)-1][i]+maps[i][0]);        printf("%d\n",res);    }}



原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 走路走多了腿酸怎么办 跳完蛙跳大腿疼怎么办 走路走多了脚疼怎么办 走太多路脚酸痛怎么办 走路走的腿酸痛怎么办 路走得太多腿疼怎么办 走路走的足弓疼怎么办 走路走多了膝盖怎么办 2岁宝宝走路一只脚内八字怎么办 6岁宝宝o型腿怎么办 一岁宝宝o型腿怎么办 宝宝一岁o型腿怎么办 宝宝一周两个月走路弓着腿怎么办 宝宝腿走路膝盖弯曲怎么办 两岁宝宝不爱走路怎么办 水鸭子脚不能走了怎么办 鸭子坐着脚走不动怎么办 鸭子步走了腿疼怎么办? 跳爵士舞没感觉怎么办 军校学员体能考核不达标怎么办 俯卧撑新兵连做不动怎么办 宝宝胆小不敢上体能课怎么办 28岁老太太抬头纹剩两颗怎么办 大腿前侧抽筋痛怎么办 电脑看片缓冲慢怎么办 跑步跑的腿疼怎么办 第一次去健身房练瑜伽不会怎么办 跑步过后腿筋疼怎么办 俯卧撑只能做20个怎么办 被裤裆峰咬了怎么办? 新兵5公里不想跑怎么办 老公掉粪坑了你怎么办 在部队被欺负了怎么办 衣服上的标志洗掉了怎么办 整件白衬衣被染怎么办 遇到敲诈小混混该怎么办 纯棉衣服洗长了怎么办 纯棉的衣服洗后长了怎么办 睡觉把脖子扭了怎么办 微信限额10万了怎么办 新兵条令背不下来怎么办