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. 关于IPFS, 你想知道的都在这里

        IPFS的的"宏伟"目标是取代HTTP, 那么先来看看IPFS是如何工作的? IPFS为每一个文件分配一个独一无二的哈希值(文件指纹: 根据文件的内容进行创建), 即使是两 ...

  2. 使用guava实现找回密码的tokenCache以及LRU算法

    源码包的简单说明: com.google.common.annotations:普通注解类型. com.google.common.base:基本工具类库和接口. com.google.common. ...

  3. 从 MVC 到前后端分离

    从 MVC 到前后端分离 1 理解 MVC MVC 是一种经典的设计模式,全名为 Model-View-Controller,即 模型-视图-控制器. 其中,模型 是用于封装数据的载体,例如,在 Ja ...

  4. 继续吐槽在net下没有合适的Disk Cache之使用EhCache

    说起缓存,大家可能口若悬河,各种类型的缓存都能一一分析,但在net下找到一款合适的Disk Cache貌似还是有一点难度的. 一:背景 事情是这样的,最近的一个项目中,需要在web端绘制一些报表,因为 ...

  5. linux服务器中毒可疑进程sfewfesfs CPU80%

    我用的是wdlinux, 难免会有漏洞,不知怎么就被莫名其妙地给入侵了,而且还频繁发包.下面是我查看攻击机器的整个过程. 首先跟客户要了root密码登录看,第一个命令是就top cd /proc/25 ...

  6. Linux运维主流架构简单剖析

    随着IT运维的不断发展,尤其的Linux的飞速发展,越来越多的企业开始使用Linux操作系统平台,例如CentOS.RedHat.Ubuntu.Fedora等等,成千上亿个网站涌现在当今互联网,互联网 ...

  7. ubuntu apt-get安装、卸载软件命令及如何查看日志

    linux亮红灯的我,开始学习linux,学习使我快乐,大家一起来学习... 1.安装软件命令 sudo apt-get apache2     安装apache,安装在默认路径下,指定路径安装,其实 ...

  8. java排序算法(四):冒泡排序

    java排序算法(四):冒泡排序 冒泡排序是计算机的一种排序方法,它的时间复杂度是o(n^2),虽然不及堆排序.快速排序o(nlogn,底数为2).但是有两个优点 1.编程复杂度很低.很容易写出代码 ...

  9. [bzoj1565][NOI2009]植物大战僵尸_网络流_拓扑排序

    植物大战僵尸 bzoj1565 题目大意:给你一张网格图,上面种着一些植物.你从网格的最右侧开始进攻.每个植物可以对僵尸提供能量或者消耗僵尸的能量.每个植物可以保护一个特定网格内的植物,如果一个植物被 ...

  10. lua精灵移除报对象非法

    function addLeftCard(isVisible) if self.left_CardSprite == nil then self.left_CardSprite = cc.Sprite ...