At.beginner.073 D - joisino's travel (floyd + 全排列)

D - joisino’s travel
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≤M≤N×(N−1)⁄2
2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)
ri≠rj(i≠j)
1≤Ai,Bi≤N,Ai≠Bi
(Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(i≠j)
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 1, 2, 3, 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

题意:求经过 r1 …rR点,的最短路径长度。

解题:floyd + 全排列

#include<cstdio>
#include<iostream>
#include<map>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
using namespace std;
typedef long long LL;

const int maxn = 2e2 + 5;
LL dis[maxn][maxn];
LL sum,w,inf = 1e14;
int n,m,r,x,y,a[10];

void Init(){
    sum = inf;
    for(int i = 0;i <= n;i++){
        for(int j = 0;j <= n;j++){
            if(i == j) dis[i][j] = 0,dis[j][i] = 0;
            else dis[i][j] = inf,dis[j][i] = inf;
        }
    }
}

void floyd(){//求任意两点最短路。
    for(int k = 1;k <= n;k++){//枚举是否有一点 k ,从点 i 到 点 j 经过点 k 的距离会比原先 i 到 j 的距离更短。
        for(int i = 1;i <= n;i++){
            for(int j = 1;j <= n;j++){
                if(dis[i][k] + dis[k][j] < dis[i][j]){
                    dis[i][j] = dis[i][k] + dis[k][j];
                }
            }
        }
    }
}

void slove(){//枚举所有情况(最多 8!)
    sort(a + 1,a + 1 + r);  
    while (next_permutation(a + 1,a + 1 + r)){//求a[i]的全排列。
        LL all = 0;
        for(int i = 2;i <= r;i++) all += dis[a[i]][a[i - 1]];
        sum = min(sum,all);
    }
}

int main(){
    scanf("%d %d %d",&n,&m,&r);
    Init(); 
    for(int i = 1;i <= r;i++) scanf("%d",&a[i]);
    for(int i = 1;i <= m;i++){
        scanf("%d %d %lld",&x,&y,&w);
        dis[x][y] = w,dis[y][x] = w;
    }
    floyd();
    slove();
    printf("%lld",sum);
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值