POJ3311 Hie with the Pie 【状压dp/TSP问题】

题目链接:http://poj.org/problem?id=3311

Hie with the Pie
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions:12225 Accepted: 6441

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.

题目大意:从 0 点出发,经过其他 n 点(允许重复经过),最后回到 0 点,要求输出最短距离。

思路:

1.属于经典的旅行商问题(TSP问题):从一个点出发可重复的经过其他至少一次,最后回到该点,问如何选择路线使得路径最短。TSP问题需要用到状压dp来记录状态,这里有一篇博客:TSP问题总结归纳以及例题

2.还需要先跑一遍floyd得到任意两点之间的最短距离。因为图中两点之间并不一定是最短的路径。

代码如下:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define mem(a, b) memset(a, b, sizeof(a))
#define LL long long
using namespace std;
const int MAXN = 15;
const int inf = 0x3f3f3f3f;

int n;
LL map[MAXN][MAXN], dis[MAXN][MAXN];
LL dp[1 << 11][MAXN]; //表示达到 i 状态时,最后访问的位置是 j  

void floyd()
{
    for(int i = 0; i <= n; i ++)
        for(int j = 0; j <= n; j ++)
            for(int k = 0; k <= n; k ++)
                dis[i][j] = min(dis[i][j], map[i][k] + map[k][j]);
}

int main()
{
    while(scanf("%d", &n) != EOF)
    {
        if(n == 0)
            break;
        for(int i = 0; i <= n; i ++)
        {
            for(int j = 0; j <= n; j ++)
            {
                scanf("%lld", &map[i][j]);
            }
        }
        mem(dis, inf);
        floyd(); //处理出任意两点之间的最短距离
        for(int i = 0; i <= ((1 << n) - 1); i ++)//枚举状态 
        {
            int S = i; //state
            for(int j = 1; j <= n; j ++)
            {
                if(S == (1 << (j - 1))) //只经过 j 这个点 
                    dp[S][j] = dis[0][j];
                else
                {
                    dp[S][j] = inf;
                    for(int k = 1; k <= n; k ++) //与 j 不同的点
                    {
                        if(k != j && (S & (1 << (k - 1))))
                            dp[S][j] = min(dp[S][j], dp[S ^ (1 << (j - 1))][k] + dis[k][j]);
                    }
                }
            }
        }
        LL ans = dp[(1 << n) - 1][1] + dis[1][0];
        for(int i = 2; i <= n; i ++)
            ans = min(ans, dp[(1 << n) - 1][i] + dis[i][0]);
        printf("%lld\n", ans);
    }
    return 0;
}
POJ3311

 

转载于:https://www.cnblogs.com/yuanweidao/p/11479513.html

Stkcd [股票代码] ShortName [股票简称] Accper [统计截止日期] Typrep [报表类型编码] Indcd [行业代码] Indnme [行业名称] Source [公告来源] F060101B [净利润现金净含量] F060101C [净利润现金净含量TTM] F060201B [营业收入现金含量] F060201C [营业收入现金含量TTM] F060301B [营业收入现金净含量] F060301C [营业收入现金净含量TTM] F060401B [营业利润现金净含量] F060401C [营业利润现金净含量TTM] F060901B [筹资活动债权人现金净流量] F060901C [筹资活动债权人现金净流量TTM] F061001B [筹资活动股东现金净流量] F061001C [筹资活动股东现金净流量TTM] F061201B [折旧摊销] F061201C [折旧摊销TTM] F061301B [公司现金流1] F061302B [公司现金流2] F061301C [公司现金流TTM1] F061302C [公司现金流TTM2] F061401B [股权现金流1] F061402B [股权现金流2] F061401C [股权现金流TTM1] F061402C [股权现金流TTM2] F061501B [公司自由现金流(原有)] F061601B [股权自由现金流(原有)] F061701B [全部现金回收率] F061801B [营运指数] F061901B [资本支出与折旧摊销比] F062001B [现金适合比率] F062101B [现金再投资比率] F062201B [现金满足投资比率] F062301B [股权自由现金流] F062401B [企业自由现金流] Indcd1 [行业代码1] Indnme1 [行业名称1] 季度数据,所有沪深北上市公司的 分别包含excel、dta数据文件格式及其说明,便于不同软件工具对数据的分析应用 数据来源:基于上市公司年报及公告数据整理,或相关证券交易所、各部委、省、市数据 数据范围:基于沪深北证上市公司 A股(主板、中小企业板、创业板、科创板等)数据整理计算
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值