zoukankan      html  css  js  c++  java
  • HDU 1572 下沙小面的(2)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1572

    大意:一点为0,给你m个站,每一步都要经过这些站(如有1,2站。。。可以是0->1->2或0->2->1距离就是累加)

    。。。。求最小总距离。。。。。

    这题是生成排列的应用。。。。

    #include <iostream>
    #include <algorithm>
    using namespace std;

    const int INF = 1<<30;

    int g[33][33];
    int a[11], p[11];

    int main()
    {
    int n, m;
    int i, j;
    int ans, sum;
    while (~scanf("%d", &n), n)
    {
    for (i = 0; i < n; i++)
    {
    for (j = 0; j < n; j++)
    {
    scanf("%d", &g[i][j]);
    }
    }

    scanf("%d", &m);
    a[0] = 0;
    for (i = 1; i <= m; i++)
    {
    scanf("%d", a+i);
    }
    p[0] = 0;
    for (i = 1; i <= m; i++)
    {
    p[i] = i;
    }
    ans = INF;

    do
    {
    sum = 0;
    for (i = 1; i <= m; i++)//一定从0开始,所排列中一定是0开头。。。然后后面的全排列
    {
    sum += g[a[p[i-1]]][a[p[i]]];//核心代码。。。。
    }
    if (ans > sum)
    {
    ans = sum;
    }
    }while (next_permutation(p+1, p+m+1));

    printf("%d\n", ans);
    }
    return 0;
    }
  • 相关阅读:
    python解析网页
    node.js 爬虫
    c++ split实现
    foldl foldr
    爬虫http header gzip
    命令[10]
    命令[08]
    命令[15]
    命令[13]
    命令[11]
  • 原文地址:https://www.cnblogs.com/qiufeihai/p/2432750.html
Copyright © 2011-2022 走看看