D - joisino's travel(最短路+排序)

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
这道题基本都是模板,就最后排序比赛的时候一直没想起来。
题意就是给一个带权无向图,然后给出一个人的路线,经过的点的顺序不固定,求出走的最短距离。
先用Floyd求出任意i到j的最短路长度,然后用next_permutation枚举经过的点的顺序即可。

#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\n",ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值