今天模拟赛我给忘了,睡过了......来的时候已经过了一个小时了,然后就没做到这个题,其实特别简单。

ou 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 .

Sample 1

Inputcopy Outputcopy
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
Case 1: 6
Case 2: 20

题意:你是一个司机,你家在1路口,你要接你的几个朋友到路口露营,问最短路

思路:首先,看到n的范围比较小,然后m比较大,所以是稠密图,首先想到Floyd,因为Floyd是n^3的时间复杂度,在这里n是100,所以不会超时,而且它可以直接记录点i到点j的最短路径,所以我们可以用dfs遍历一下所有接送朋友的顺序的情况,然后有用minn记录最短距离。

话不多说,看代码:

#include <bits/stdc++.h>
using namespace std;
#define Maxn 0x3f3f3f3f
int mp[110][110];
int a[110];
int vis[110];
int n,m,num,minn;
void floy()//Floyd代码
{int i,j,k;for(k=1;k<=n;k++){for(i=1;i<=n;i++){for(j=1;j<=n;j++){mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);}}}
}
void dfs(int dis,int last,int level)//level记录这是第几个朋友,dis表示当前司机走过的距离//last表示上一个朋友所在的路口
{if(level>num){minn=min(minn,dis+mp[last][n]);return;}for(int i=1;i<=num;i++){if(vis[i]==0){vis[i]=1;dis+=mp[last][a[i]];dfs(dis,a[i],level+1);dis-=mp[last][a[i]];vis[i]=0;}}
}
void chushi()//Floyd初始化
{for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){if(i==j)mp[i][j]=0;elsemp[i][j]=Maxn;}}
}int main()
{int t,x,y,z;scanf("%d",&t);for(int k=1;k<=t;k++){memset(vis,0,sizeof(vis));scanf("%d %d %d",&n,&m,&num);chushi();minn=Maxn;while(m--){scanf("%d %d %d",&x,&y,&z);mp[x][y]=min(z,mp[x][y]);mp[y][x]=min(z,mp[y][x]);}for(int i=1;i<=num;i++){scanf("%d",&a[i]);}floy();dfs(0,1,1);printf("Case %d: %d\n",k,minn);}return 0;
}

(赛后补题)Gym - 101020 H H - Weekend相关推荐

  1. 2021年度训练联盟热身训练赛第三场赛后补题

    2021年度训练联盟热身训练赛第三场赛后补题 A Circuit Math [题目分析] [代码展示] B Diagonal Cut [题目分析] [代码展示] C Gerrymandering [题 ...

  2. Codeforces Round #701 (Div. 2)赛后补题报告(A~D)

    Codeforces Round #701 (Div. 2)赛后补题报告(A~D) A. Add and Divide 原题信息 http://codeforces.com/contest/1485/ ...

  3. 2018 HDU多校第四场赛后补题

    2018 HDU多校第四场赛后补题 自己学校出的毒瘤场..吃枣药丸 hdu中的题号是6332 - 6343. K. Expression in Memories 题意: 判断一个简化版的算术表达式是否 ...

  4. 2020年湖南中医药大学“华为杯”大学生程序设计竞赛——正式赛(赛后补题)

    目录 A-幸福小组 B-菜鸡驿站 C-TC的火柴 D-n盏灯 E-电子手表 F- TC的steam账号 G- 万圣节 H-最少颜色 I-2048 J-解密 K-TC的门牌号 L-粗心的小明 M-小明的 ...

  5. 赛后补题:2022CCPC绵阳 A. Ban or Pick, What‘s the Trick

    传送门:CF 题目描述: outputstandard output Bobo has recently learned how to play Dota2. In Dota2 competition ...

  6. 2020年湖南中医药大学“华为杯”大学生程序设计竞赛(赛后补题)

    这次院赛给打自闭了,从头到尾一直落在后面,最后只拿到了二等奖尾,还是太菜了,唉(难受).多刷题!!! (未完待续) 目录 A:幸福小组 B:菜鸟驿站 C:TC的火柴 D:n盏灯 E:电子手表 F:TC ...

  7. Codeforces Round #498 (Div. 3) - 赛后补题

    D. Two Strings Swaps PS:没思考清楚,重复算了一些情况. #include<bits/stdc++.h> #include<bitset> #define ...

  8. coderforce Educational Codeforces Round 44 (Rated for Div. 2) C(赛后补题)

    C. Liebig's Barrels time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. HDU 6446 Tree and Permutation(赛后补题)

    >>传送门<< 分析:这个题是结束之后和老师他们讨论出来的,很神奇:刚写的时候一直没有注意到这个是一个树这个条件:和老师讨论出来的思路是,任意两个结点出现的次数是(n-1)!, ...

最新文章

  1. 两条波浪线符号_四年级数学上册第二单元“线的认识”作业单(附带答案)
  2. Centos7 关闭防火墙(转)
  3. html应用中心模板,HTML5--应用网页模板
  4. 终于看腻了黄色!让它五彩斑斓起来!
  5. Xamarin.Android之封装个简单的网络请求类
  6. 应用(指定)进程创建的时候创建Application
  7. 和某ZYC巨佬和XXY巨佬的随机挑战2总结
  8. 工业型交换机相比普通交换机有哪些要求?
  9. Kaggle新上比赛预测地震-总奖池5万美金
  10. 添加 code snippets (转)
  11. 应用安全-安全设备-Waf系列-软Waf-安全狗(Safedog)
  12. 进程创建函数fork
  13. Linux ls按时间排列
  14. macOS Monterey 12.0beta4黑苹果镜像虚拟机版本
  15. win7创建桌面计算机,win7系统添加或删除虚拟桌面的方法介绍
  16. java单例模式——详解JAVA单例模式及8种实现方式
  17. 详解VB对话框InputBox和MsgBox
  18. Pod进阶(容器本质,Pod实现,容器设计模式)
  19. NSWindow纯代码设置
  20. Albert Einstein—一个真正值得敬仰的伟人(什么超级什么星什么一班都靠边站兼滚蛋!)...

热门文章

  1. 基于SpringBoot 的学生选课系统(Java毕业设计)
  2. pop3 邮件列表_什么是邮件列表| 第1部分
  3. JavaScript 面向对象编程思想简介
  4. Dubbo底层原理架构图
  5. php各种主流框架的比较
  6. html如何将div居中置顶层,CSS如何使DIV层居中
  7. 23 CoCos Creator-主菜单
  8. mysql federated 优缺点_MySQL FEDERATED 存储引擎
  9. linux的su,su - 和sudo的区别
  10. 微信小程序云开发调用 腾讯云AI 图像标签