D

原题地址:http://abc073.contest.atcoder.jp/tasks/abc073_d

Time limit : 2sec / Memory limit : 256MB

Score : 400 points

Problem Statement

There are N towns in the State of Atcoder, connected by M bidirectional roads.

The i-th road connects Town Ai and Bi and has a length of Ci.

Joisino is visiting R towns in the state, r1,r2,..,rR (not necessarily in this order).

She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.

If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

Constraints

  • 2≤N≤200
  • 1≤MN×(N−1)⁄2
  • 2≤Rmin(8,N) (min(8,N) is the smaller of 8 and N.)
  • rirj(ij)
  • 1≤Ai,BiN,AiBi
  • (Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(ij)
  • 1≤Ci≤100000
  • Every town can be reached from every town by road.
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

N M R
r1  rR
A1 B1 C1
:
AM BM CM

Output

Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.


Sample Input 1

Copy
3 3 3
1 2 3
1 2 1
2 3 1
3 1 4

Sample Output 1

Copy
2

For example, if she visits the towns in the order of 123, the distance traveled will be 2, which is the minimum possible.


Sample Input 2

Copy
3 3 2
1 3
2 3 2
1 3 6
1 2 2

Sample Output 2

Copy
4

The shortest distance between Towns 1 and 3 is 4. Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4.


Sample Input 3

Copy
4 6 3
2 3 4
1 2 4
2 3 3
4 3 1
1 4 1
4 2 2
3 1 6

Sample Output 3

Copy
3

题目意思:要经过特定的点,要求路径最短:
解题思路:先使用Floyd算法得到每个点之间的最短路;在DFS查找所有可能性
代码1:
#include<iostream>
#include<string>
#include<algorithm>
#include <string.h>
#include <stdio.h>
#include <math.h>
#include <set>
#include <queue>
#include <stack>
#include <map>
 
using namespace std;
typedef long long LL;
 
const int INF = int(1e9);
int N,M,R;
int Map[210][210];
int a[10];
int visit[10];
int min1;
 
void dfs(int n,int i,int sum){
    if(n >= R+1){
        if(min1>sum)
            min1 = sum;
        return;
    }
    for(int j = 0;j<R;j++)if(!visit[j])
    {
        visit[j]=1;
        if(i==-1)dfs(n+1,j,0);
        else dfs(n+1,j,sum+Map[a[i]][a[j]]);
        visit[j]=0;
    }
}
 
int main()
{
    cin>>N>>M>>R;
    for(int i = 0;i<=N;i++)
    for(int j = 0;j<=N;j++)
    if(i!=j) Map[i][j] = INF;
 
    for(int i = 0;i < R;i++) cin>>a[i];
    for(int i = 0;i < M;i++){
        int x,y,len;
        cin>>x>>y>>len;
        if(Map[x][y]>len)
        Map[x][y] = Map[y][x] = len;
    }
 
    for(int k = 1;k<=N;k++)
    for(int i = 1;i<=N;i++)
    for(int j = 1;j<=N;j++)
    Map[i][j] = min(Map[i][j],Map[i][k]+Map[k][j]);
 
    min1 = INF;
    dfs(1,-1,0);
    cout<<min1<<endl;
 
 
    return 0;
}

代码2:这里的DFS实际上是对要去的点进行全排列;也可以使用全排列函数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define inf 0x3f3f3f3f

using namespace std;

int paths[210][210];
int R[210];
int n,m,r;

void floyd()
{
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
               paths[i][j]=min(paths[i][k]+paths[k][j],paths[i][j]);//更新i到j的最短路
}

int main()
{
    while(scanf("%d%d%d",&n,&m,&r)==3)
        for(int i=0;i<=n;i++)
            for(int j=0;j<=n;j++)
                if(i==j)paths[i][j]=0;
                else paths[i][j]=inf;

        for(int i=0;i<r;i++)
            scanf("%d",&R[i]);

        int a,b,c;
        for(int i=1;i<=m;i++)
            scanf("%d%d%d",&a,&b,&c);
            if(paths[a][b]>c||paths[b][a]>c)
                paths[a][b]=paths[b][a]=c;

        floyd();
        sort(R,R+r);
        int ans=inf;
        do
        {
            int sum=0;
            for(int i=0;i<r-1;i++)
                sum+=paths[R[i]][R[i+1]];//计算总路程

            ans=min(ans,sum);//更新最短总路程
        }while(next_permutation(R,R+r));//枚举经过的点的顺序
        printf("%d
",ans);
    }
    return 0;
}






原文地址:https://www.cnblogs.com/a2985812043/p/7739312.html