前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 3311 - Hie with the Pie

POJ 3311 - Hie with the Pie

作者头像
Reck Zhang
发布2021-08-11 11:58:59
2760
发布2021-08-11 11:58:59
举报
文章被收录于专栏:Reck Zhang

Hie with the Pie

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

代码语言:javascript
复制
3
0 1 10 10
1 0 1 2
10 1 0 10
10 2 10 0
0

Sample Output

代码语言:javascript
复制
8

Solution

代码语言:javascript
复制
#include <iostream>
#include <limits.h>
using namespace std;

int n;
int mp[11][11];
int dp[1<<11][11];

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

void fun() {
    for(int i = 1; i < (1 << n); i++) {
        for(int j = 1; j <= n; j++) {
            int tmp = 1 << (j - 1);
            if(tmp == i) {
                dp[i][j] = mp[0][j];
            } else if(tmp & i) {
                dp[i][j] = INT_MAX;
                for(int k = 1; k <= n; k++) {
                    if(k != j && (i & (1 << (k - 1)))) {
                        dp[i][j] = min(dp[i][j], dp[i ^ tmp][k] + mp[k][j]);
                    }
                }
            }
        }
    }
    int x = (1 << n) - 1;
    int res = INT_MAX;
    for(int i = 1; i <= n; i++) {
        res = min(res, dp[x][i] + mp[i][0]);
    }
    cout << res << endl;
    return ;
}

int main() {
    while(cin >> n && n) {
        for(int i = 0; i <= n; i++) {
            for(int j = 0; j <= n; j++) {
                cin >> mp[i][j];
            }
        }
        floyd();
        fun();
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-08-18,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Hie with the Pie
    • Description
      • Input
        • Output
          • Sample Input
            • Sample Output
              • Solution
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档