H - Weekend (floyd+next_permutation函数)

H - Weekend Gym - 101020H
You and your friends are going to camping next weekend , because you are the best driver and you are living in this town for ten years , you have to drive the car from your home and pick up each of your friends to take them with you ,then finally go to the camping site. You have the map of the town and the length of every street ,and homes of friends will be at some intersections. you want to start from your home then pick up all of your friends then go to the camping site as fast as you can so you have to find the shortest path to do that. you can pick up your friends in any order but you have to make sure you pick up all of them. NOTE: you can pass by any intersection more than once including home and camping site .

Input
Your program will be tested on one or more test cases. The first line of the input will be a single integer T, the number of test cases (1  ≤  T  ≤  50). Followed by the test cases, the first line of each test case contains three integers N,M,F (3  ≤  N  ≤  100 , 0<M  ≤  (N*(N-1))/2 , 1  ≤  F  ≤  9 ) representing the number of intersections , the number of streets and number of friends respectively. Followed by M lines, each line contains 3 integers separated by a single space X Y Z (1  ≤  X, Y  ≤  N), (1  ≤  Z  ≤  1000) which represent a street between intersection X and intersection Y with length Z in both direction (X and Y will be different). then follow F (1<fr<N) deferent integer representing friends home number(number of intersection where home is located). your home is at intersection number one and camping site is at intersection number N.

Output
For each test case print a single line containing “Case n:” (without quotes) where n is the test case number (starting from 1) followed by a space then the shortest path to go from your home to camping site after picking up all your friends .

Examples
Input
2
4 4 2
1 4 3
1 2 2
2 3 2
3 4 2
2 3
6 8 2
1 2 1
2 3 2
3 6 3
1 4 11
4 5 2
5 6 20
5 3 5
4 3 10
4 5
Output
Case 1: 6
Case 2: 20
Note
in the second test case the shortest path is : 1 > 2 > 3 > 5 > 4 > 5 > 3 > 6
next_permutation全排列函数next_permutation(num,num+n)函数是对数组num中的前n个元素进行全排列,同时并改变num数组的值。函数next_permutation()是按照字典序产生排列的,并且是从数组中当前的字典序开始依次增大直至到最大字典序(降序)。
另外,需要强调的是,next_permutation()在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数

#include <stdio.h>
#include <algorithm>
#include <memory.h>
#define ll long long
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int map[101][101],vis[101];
void floyd()
{
    int k,i,j;
    for(k=1; k<=n; k++)
    {
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(map[i][j]>map[i][k]+map[k][j])
                {
                    map[i][j]=map[i][k]+map[k][j];
                }
            }
        }
    }
}
int a[1001];
int main()
{
    int t,T,i,j,kk,u,v,w,k;
    ll sum,ans;
    scanf("%d",&T);
    for(t=1; t<=T; t++)
    {
        scanf("%d%d%d",&n,&m,&k);
        for(i=1; i<=n; i++)
        {
            for(j=1; j<=n; j++)
            {
                if(i==j) map[i][j]=0;
                else map[i][j]=INF;
            }
        }
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(map[u][v]>w) map[u][v]=map[v][u]=w;
        }
        for(i=0; i<k; i++)
        {
            scanf("%d",&a[i]);
        }
        sort(a,a+k);//先排成升序的
        floyd();
        ans=INF;
        do
        {
            sum=map[1][a[0]];
            kk=a[0];
            for(i=1; i<k; i++)
            {
                sum+=map[kk][a[i]];
                kk=a[i];
            }
            sum+=map[kk][n];
            ans=min(sum,ans);
        }while(next_permutation(a,a+k));//将所有先后接小朋友的顺序排出来
        printf("Case %d: %lld\n",t,ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值