状压DP Poj3311 Hie with the Pie

本人水平有限,题解不到为处,请多多谅解

本蒟蒻谢谢大家观看

题目:

Problem C: Poj3311 Hie with the Pie

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 58  Solved: 29
[Submit][Status][Web Board]

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.
给你一个有n+1(1<=n<=10)个点的有向完全图,用矩阵的形式给出任意两个不同点之间的距离。(其中从i到j的距离不一定等于从j到i的距离)现在要你求出从0号点出发,走过1到n号点至少一次,然后再回到0号点所花的最小距离

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

HINT

此题大致类似于上一题:https://www.cnblogs.com/nlyzl/p/11307139.html

读入用从0~n,此题要求了必须从0号点出发,所以初始f[1][0]=0;即0号点选了以0号点结尾的值为0;

i需从1 ~(1<<n+1)-1;

j :  n~0 要逆循环,因为题目没有说不能不走回头路,所以我们要从n逆推,保证当前正着走与反着走的值都知道,以便于我们进行DP取min。

因为k与j我们都从0开始,所以左移时不需要 1<<(j-1)和1<<(k-1)

直接1<<j 和 1<<k即可;

转移:f[i][j]=min(f[i][j],min(f[i^1<<j][k],f[i][k])+dis[k][j]); //即应先从上一个状态与当前状态取min 后再来加上距离,再来转移(因为可以走回头路)

最后统计时我们还要+dis[i][0];  //从任意一点到0的距离

code:

#include<bits/stdc++.h>
using namespace std;
int n;
int dis[21][21];
int ans;
int f[1<<20][21];
inline int read(){
     int x=0,f=1;
     char ch=getchar();
     while(ch<‘0‘||ch>‘9‘){
         if(ch==‘-‘)
             f=-1;
         ch=getchar();
     }
     while(ch>=‘0‘&&ch<=‘9‘){
         x=(x<<1)+(x<<3)+(ch^48);
         ch=getchar();
     }
     return x*f;
}

inline void write(int x){
     char F[200];
     int tmp=x>0?x:-x ;
     if(x<0)putchar(‘-‘) ;
     int cnt=0 ;
        while(tmp>0)
        {
            F[cnt++]=tmp%10+‘0‘;
            tmp/=10;
        }
        while(cnt>0)putchar(F[--cnt]) ;
}

int main()
{
    while(scanf("%d",&n)&&n!=0)
    {
        ans=INT_MAX;
        for(int i=0;i<=n;i++)
        {
            for(int j=0;j<=n;j++)
            {
                dis[i][j]=read();
            }
        }
        memset(f,0x3f,sizeof(f));
        f[1][0]=0;
        for(int i=1;i<=(1<<n+1)-1;i++)
        {
            for(int j=n;j>=0;j--)
            {    

                if(i&1<<j)
                {
                    for(int k=0;k<=n;k++)
                    {
                        if(i&1<<k&&j!=k)
                        {
        f[i][j]=min(f[i][j],min(f[i^1<<j][k],f[i][k])+dis[k][j]);
                        }
                    }
                }
            }
        }
        for(int i=0;i<=n;i++)
            ans=min(ans,f[(1<<n+1)-1][i]+dis[i][0]);
        write(ans);
        cout<<endl;
    }
    return 0;
} 

原文地址:https://www.cnblogs.com/nlyzl/p/11307300.html

时间: 2024-10-18 03:13:53

状压DP Poj3311 Hie with the Pie的相关文章

poj3311(Hie with the Pie)状压dp

题目链接:http://poj.org/problem?id=3311 解法:标准的状压dp类型,先floyd获得两两之间最短距离.然后dp[i][j]表示剩下集合i没走,已经走到j的最短距离: 代码: /****************************************************** * @author:xiefubao *******************************************************/ #pragma comment(

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 (状压DP)

题意: 每个点都可以走多次的TSP问题:有n个点(n<=11),从点1出发,经过其他所有点至少1次,并回到原点1,使得路程最短是多少? 思路: 同HDU 5418 VICTOR AND WORLD (可重复走的TSP问题,状压DP)这道题几乎一模一样. 1 //#include <bits/stdc++.h> 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #includ

poj 3311 Hie with the Pie 经过所有点(可重)的最短路径 状压dp

题目链接 题意 给定一个\(N\)个点的完全图(有向图),求从原点出发,经过所有点再回到原点的最短路径长度(可重复经过中途点). 思路 因为可多次经过同一个点,所以可用floyd先预处理出每两个点之间的最短路径. 接下来就是状压dp的部分. 将已经经过的点的状态用\(state\)表示, 则\(dp[state][k]\)表示当前到达点\(k\)后状态为\(state\)时的最短路径长度. \[ans=min_{i=1}^{n}(dp[(1<<n)-1][i]+dis[i][0])\] 可用记

Hie with the Pie(POJ 3311)状压DP

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

poj3311Hie with the Pie状压dp

tsp,但是它可以每个点经过不止一次,所以求一遍最短路,然后搞. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include <cstdlib> #i

Travel(HDU 4284状压dp)

题意:给n个城市m条路的网图,pp在城市1有一定的钱,想游览这n个城市(包括1),到达一个城市要一定的花费,可以在城市工作赚钱,但前提有工作证(得到有一定的花费),没工作证不能在该城市工作,但可以走,一个城市只能工作一次,问pp是否能游览n个城市回到城市1. 分析:这个题想到杀怪(Survival(ZOJ 2297状压dp) 那个题,也是钱如果小于0就挂了,最后求剩余的最大钱数,先求出最短路和 Hie with the Pie(POJ 3311状压dp) 送披萨那个题相似. #include <

poj3311 Hie with the Pie 旅行商问题(TSP)

题目链接 http://poj.org/problem?id=3311 Hie with the Pie Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 5094   Accepted: 2716 Description The Pizazz Pizzeria prides itself in delivering pizzas to its customers as fast as possible. Unfortuna

POJ3311——Hie with the Pie

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