Gym - 101020H Weekend floyd+next_permutation

Posted COLORFUL

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Gym - 101020H Weekend floyd+next_permutation相关的知识,希望对你有一定的参考价值。

https://vjudge.net/problem/Gym-101020H

题意:正常读取方式给你一个图(双向的),然后给你f个点,让你找一条路从1到n的最短路,要求经过f个点(以任意顺序)。

题解:一开始还想dijkstra+dp,发现根本下不了手。后来看了题解,发现只要floyd+next_permutation .Orz

ac代码:

#include<iostream>
#include<algorithm>
#include<string.h>
#include<cstdio>
using namespace std;
int mp[105][105], d[105][105];
int order[12], pos[12];
int main() {
    int t;
    cin >> t;
    int kase = 1;
    while (t--) {
        int n, m, f;
        cin >> n >> m >> f;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                if (i == j)d[i][j] = 0;
                else d[i][j] = 1e9;
                for (int i = 1; i <= m; i++) {
                    int u, v, z;
                    cin >> u >> v >> z;
                    d[u][v] = d[v][u] = z;
                }
                for (int i = 1; i <= f; i++) {
                    cin >> pos[i];
                }

                for (int k = 1; k <= n; k++)
                    for (int i = 1; i <= n; i++)
                        for (int j = 1; j <= n; j++)
                            d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
                for (int i = 1; i <= f; i++)order[i] = i;
                int ans = 1e9, temp = 0;
                do {
                    temp = d[1][pos[order[1]]] + d[n][pos[order[f]]];
                    for (int i = 1; i < f; i++)temp += d[pos[order[i]]][pos[order[i + 1]]];
                    ans = min(ans, temp);
                } while (next_permutation(order + 1, order + 1 + f));
                printf("Case %d: %d\n", kase++, ans);//cout << ans << endl;
    }
}

 

以上是关于Gym - 101020H Weekend floyd+next_permutation的主要内容,如果未能解决你的问题,请参考以下文章

3 视频里weekend050607的可靠性 + HA原理分析机制 + weekend01020304050607的分布式集群搭建

5 weekend01020304050607的分布式集群的HA测试 + hdfs--动态增加节点和副本数量管理 + HA的java api访问要点

光追起步 Ray Tracing in One Weekend

1 weekend110的hdfs源码跟踪之打开输入流 + hdfs源码跟踪之打开输入流总结

新概念英语(1-67)The weekend

4 weekend110的hive入门