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的更多相关文章

  1. AtCoder Beginner Contest 100 2018/06/16

    A - Happy Birthday! Time limit : 2sec / Memory limit : 1000MB Score: 100 points Problem Statement E8 ...

  2. AtCoder Beginner Contest 052

    没看到Beginner,然后就做啊做,发现A,B太简单了...然后想想做完算了..没想到C卡了一下,然后还是做出来了.D的话瞎想了一下,然后感觉也没问题.假装all kill.2333 AtCoder ...

  3. AtCoder Beginner Contest 053 ABCD题

    A - ABC/ARC Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Smeke has ...

  4. Atcoder regular Contest 073(C - Sentou)

    Atcoder regular Contest 073(C - Sentou) 传送门 每个人对开关的影响区间为a[i]--a[i]+t,因此此题即为将所有区间离散化后求所有独立区间的长度和 #inc ...

  5. Atcoder regular Contest 073(D - Simple Knapsack)

    Atcoder regular Contest 073(D - Simple Knapsack) 传送门 因为 w1≤wi≤w1+3 这个特殊条件,我们可以将每个重量离散化一下,同时多开一维记录选择的 ...

  6. AtCoder Beginner Contest 136

    AtCoder Beginner Contest 136 题目链接 A - +-x 直接取\(max\)即可. Code #include <bits/stdc++.h> using na ...

  7. AtCoder Beginner Contest 137 F

    AtCoder Beginner Contest 137 F 数论鬼题(虽然不算特别数论) 希望你在浏览这篇题解前已经知道了费马小定理 利用用费马小定理构造函数\(g(x)=(x-i)^{P-1}\) ...

  8. AtCoder Beginner Contest 076

    A - Rating Goal Time limit : 2sec / Memory limit : 256MB Score : 100 points Problem Statement Takaha ...

  9. AtCoder Beginner Contest 079 D - Wall【Warshall Floyd algorithm】

    AtCoder Beginner Contest 079 D - Wall Warshall Floyd 最短路....先枚举 k #include<iostream> #include& ...

随机推荐

  1. Keeweb-Linux的密码管理器

    Keeweb-Linux的密码管理器 如今,我们依赖于越来越多的线上服务.我们每注册一个线上服务,就要设置一个密码:如此,我们就不得不记住数以百计的密码.这样对于每个人来说,都很容易忘记密码.那么,下 ...

  2. maven 技术总结

    1.版本统一控制 在 properties中配置一个参数,在添加依赖时 通过 version标签 限定版本 <properties> <org.springframework.ver ...

  3. eclipse的Debug模式下的快捷键

    主要快捷键: F5, F6, F7, F8的使用 F5:  进入当前方法 F6: 一步步执行 F7:  跳出方法, 返回到调用此方法的最后一条语句 F8: 继续执行,跳转到下一个断点的位置 示例: 在 ...

  4. 桶排序/基数排序(Radix Sort)

    说基数排序之前,我们先说桶排序: 基本思想:是将阵列分到有限数量的桶子里.每个桶子再个别排序(有可能再使用别的排序算法或是以递回方式继续使用桶排序进行排序).桶排序是鸽巢排序的一种归纳结果.当要被排序 ...

  5. 【Flask】 WTForm表单编程

    WTForm表单编程 在网页中,为了和用户进行信息交互总是不得不出现一些表单.flask设计了WTForm表单库来使flask可以更加简便地管理操作表单数据.WTForm中最重要的几个概念如下: Fo ...

  6. 四十六、android中的Bitmap

    四十六.android中的Bitmap: http://www.cnblogs.com/linjiqin/archive/2011/12/28/2304940.html 四十七.实现调用Android ...

  7. Notepad++中实现Markdown语法高亮与实时预览

    Notepad ++是一个十分强大的编辑器,除了可以用来制作一般的纯文字说明文件,也十分适合编写计算机程序代码.Notepad ++不仅有语法高亮度显示,也有语法折叠功能,并且支持宏以及扩充基本功能的 ...

  8. 城市安全风险管理项目Postmortem结果

    设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 本系统希望实现快速识别危害因素,使工作人员对风险作出准确的评估.即让使用者熟悉潜在的危险因素,知道 ...

  9. NumPy简介

    NumPy是什么? NumPy(Numerrical Python 的缩写)是一个开源的Python科学计算库.使用NumPy,就可以很自然的使用数组.NumPy包含很多实用的数学函数,涵盖线性代数运 ...

  10. 2017 清北济南考前刷题Day 4 afternoon

    期望得分:30+50+30=110 实际得分:40+0+0=40 并查集合并再次写炸... 模拟更相减损术的过程 更相减损术,差一定比被减数小,当被减数=减数时,停止 对于同一个减数来说,会被减 第1 ...