AtCoder Beginner Contest 073
D - joisino's travel
Time Limit: 2 sec / Memory Limit: 256 MB
Score : 400400 points
Problem Statement
There are NN towns in the State of Atcoder, connected by MM bidirectional roads.
The ii-th road connects Town AiAi and BiBi and has a length of CiCi.
Joisino is visiting RR towns in the state, r1,r2,..,rRr1,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≤2002≤N≤200
- 1≤M≤N×(N−1)/21≤M≤N×(N−1)/2
- 2≤R≤min(8,N)2≤R≤min(8,N) (min(8,N)min(8,N) is the smaller of 88 and NN.)
- ri≠rj(i≠j)ri≠rj(i≠j)
- 1≤Ai,Bi≤N,Ai≠Bi1≤Ai,Bi≤N,Ai≠Bi
- (Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(i≠j)(Ai,Bi)≠(Aj,Bj),(Ai,Bi)≠(Bj,Aj)(i≠j)
- 1≤Ci≤1000001≤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:
NN MM RR
r1r1 ...... rRrR
A1A1 B1B1 C1C1
::
AMAM BMBM CMCM
Output
Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.
题意:
一个人旅行,必须经过指定的r个城市,问最短的路程是多少。他可以从任意一个城市开始,任意一个城市结束。保证图是连通的。
思路:
赛上没有写出来,是因为把题给读错了,以为必须经过1和n两个点。后来经过多方问询,把题意理清楚了。首先由于n最大只有200,所以可以用floyed求出两点之间的最短距离。之后,由于r很小所以可以把r的阶乘种的情况给枚举出来,这个时候就用到了dfs,最后取一个最小值即可。
代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int inf = 0x3f3f3f3f;
int rr[];
int mp[][];
bool v[]; int n,m,r;
int ans; void dfs(int c,int las,int dis)
{
if (c == r)
{
ans = min(ans,dis);
return;
} for (int i = ;i < ;i++)
{
if (!v[i])
{
v[i] = ; if (las == -) dfs(c + ,i,dis);
else dfs(c+,i,dis + mp[rr[las]][rr[i]]); v[i] = ;
} }
} int main()
{ ans = inf; memset(mp,inf,sizeof(mp)); scanf("%d%d%d",&n,&m,&r); for (int i = ;i < r;i++)
scanf("%d",&rr[i]); for (int i = ;i < m;i++)
{
int x,y,z; scanf("%d%d%d",&x,&y,&z); if (mp[x][y] > z)
mp[x][y] = mp[y][x] = z;
} for (int k = ;k <= n;k++)
for (int i = ;i <= n;i++)
for (int j = ;j <= n;j++)
mp[i][j] = min(mp[i][j],mp[i][k] + mp[k][j]); dfs(,-,); printf("%d\n",ans); return ;
}
AtCoder Beginner Contest 073的更多相关文章
- AtCoder Beginner Contest 100 2018/06/16
A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...
- AtCoder Beginner Contest 052
没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...
- AtCoder Beginner Contest 053 ABCD题
A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...
- Atcoder regular Contest 073(C - Sentou)
Atcoder regular Contest 073(C - Sentou) 传送门 每个人对开关的影响区间为a[i]--a[i]+t,因此此题即为将所有区间离散化后求所有独立区间的长度和 #inc ...
- Atcoder regular Contest 073(D - Simple Knapsack)
Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...
- AtCoder Beginner Contest 136
AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...
- AtCoder Beginner Contest 137 F
AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...
- AtCoder Beginner Contest 076
A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...
- AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】
AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...
随机推荐
- Omron 论坛软件下载连接
全部软件目录 (更新时间:2017年1月5日) 序号 产品类别 软件名称 1 FA自动化设备 RFID系统 V600-CA5DUSB驱动程序 2 FA自动化设备 可编程控制器 CJ2/CP1USB驱动 ...
- React 组件间通讯
React 组件间通讯 说 React 组件间通讯之前,我们先来讨论一下 React 组件究竟有多少种层级间的关系.假设我们开发的项目是一个纯 React 的项目,那我们项目应该有如下类似的关系: 父 ...
- Webpack模块的导出以及之间的依赖引用
一. 模块化开发模块化开发说白了就不必在html页面,引用所有的js文件.所有的js文件都进行模块化设置,模块之间可以相互引用.Webpack模块化开发是使用module.exports进行相关方法和 ...
- ListView动态刷新adapter.notifyDataSetChanged()无反应
前段时间在做一个动态刷新ListView(模拟聊天),遇到一个问题,调用notifyDataSetChanged()方法,数据源已经存在但是并没有动态刷新! 首先我们需要了解notifyDataSet ...
- Java多线程:线程池
一. 背景 线程是稀缺资源,如果无限制的创建,不仅会消耗系统资源,还会降低系统的稳定性,合理的使用线程池可以对线程进行统一的分配.调优和监控,并有以下好处: 第一:降低资源消耗.通过重复利用已 ...
- linux下安装Sublime Text3并将它的快捷方式放进启动器中
Sublime Text是一个代码编辑器,我主要是用它来编辑python.下面就来简单说明下它在linux的安装过程吧! 1.添加sublime text3的仓库 首先按下快捷键ctrl+alt+t打 ...
- (转)[置顶] Android APK反编译就这么简单 详解(附图) .
在学习Android开发的过程你,你往往会去借鉴别人的应用是怎么开发的,那些漂亮的动画和精致的布局可能会让你爱不释手,作为一个开发者,你可能会很想知道这些效果界面是怎么去实现的,这时,你便可以对改应用 ...
- 使用ADO.NET查询和访问数据库
使用ADO.NET查询和访问数据库步骤 使用ADO.NET查询和访问数据库 连接数据库操作: 1. 定义连接字符串: String connString = "Data Sour ...
- JavaScript(第二十三天)【事件入门】
JavaScript事件是由访问Web页面的用户引起的一系列操作,例如:用户点击.当用户执行某些操作的时候,再去执行一系列代码. 一.事件介绍 事件一般是用于浏览器和用户操作进行交互.最早是IE和 ...
- C语言第七次作业
一.PTA实验作业 题目1:求整数序列中出现次数最多的数 1.本题PTA提交列表 2.设计思路 定义一个整型数组a[1001],i,j 为循环变量,N,定义数组b[1001]={0} 输入N for( ...