There is a legendary tale about Dragon Balls on Planet X: if one collects seven Dragon Balls, the Dragon God will show up and help you fulfill your wishes.

One day, you are surprised to discover that the tale might possibly be true: you found a Dragon Ball radar at a flea market! The radar shows you the locations of the seven Dragon Balls on Planet X. You want to waste no time checking the truth of the old legend about wish-granting for yourself!

There are nn cities in total on the Planet X, numbered from 11 to nn. You are currently at city 11. To travel from one city to another, you can take any of mm bidirectional teleport trips, as many times as you like. The ii-th teleporter costs titi coins to use each time, and it can teleport you between cities aiai and bibi. To collect a Dragon Ball, you simply need to visit the city where it’s located, as indicated on your radar. It is possible that multiple Dragon Balls are at the same city; in this case you pick all of them all up at once if you visit that city.

Input

The first line of input contains two space-separated integers nn and mm (1≤n,m≤200000)(1≤n,m≤200000), the number of cities and possible teleport trips. Then follow mm lines containing three space-separated integers aiai, bibi, and titi each (1≤ai,bi≤n,0≤ti≤10000)(1≤ai,bi≤n,0≤ti≤10000), which, as explained above, represent the two cities connected by the teleport trip, and cost to use the teleporter. Then follows one line of seven space-separated integers, representing the city IDs of the seven Dragon Balls showing on the radar. Each ID cc satisfies the bound 1≤c≤n1≤c≤n.

Output

Print the minimum number of coins that you need to spend to collect all seven Dragon Balls shown on the Dragon Ball radar. If there is no way to complete this task, print −1−1 instead.

Sample Input 1 Sample Output 1
10 9
1 2 1
2 3 1
3 4 1
4 5 1
5 6 1
6 7 1
7 8 1
8 9 1
9 10 1
1 2 3 4 5 6 7
6
Sample Input 2 Sample Output 2
5 5
1 2 0
1 3 0
2 3 1
3 4 1
4 5 1
1 2 1 2 3 4 4
1

7个点,每个点当成起点跑一个最短路~~枚举排列方式计算一下

#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<string>
#define met(a,x) memset(a,x,sizeof(a));
#define rep(i,a,b) for(ll i = a;i <= b;i++)
#define bep(i,a,b) for(ll i = a;i >= b;i--)
#define lowbit(x) (x&(-x))
// #define mid ((l + r) >> 1)
// #define len (r - l + 1)
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define pb push_back
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) { return b == ? a : gcd(b, a%b); }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
typedef unsigned long long ull;
typedef pair<ll, ll>P;
typedef pair<ll, pair<ll, ll> > Pii;
const ll inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-);
const ll maxn = ;
const ll mod = ;
int rd(){
int flag=;
int sum=;
char c=getchar();
while(c<''||c>''){
if(c=='-')flag=-;
c=getchar();
}
while(c>=''&&c<=''){
sum=sum*+c-'';
c=getchar();
}
return sum*flag;
} struct node{
ll v,w,net;
}e[maxn];
ll n,m,cnt,top,head[maxn],dis[][maxn],vis[maxn];
ll a[];
void add(ll u,ll v,ll w){
e[cnt] = (node){v,w,head[u]};
head[u] = cnt++;
}
void SPFA(ll now,ll s)
{
rep(i,,n)dis[now][i] = INF,vis[i] = ;
queue<int>que;
que.push(s);
dis[now][s]=;
while(!que.empty())
{
int u = que.front();
que.pop();
vis[u] = ;
for(int i=head[u]; i!=-; i=e[i].net)
{
int v=e[i].v;
int w=e[i].w;
if(dis[now][v]>dis[now][u]+w)
{
dis[now][v]=dis[now][u]+w;
if(!vis[v]){
vis[v] = ;
que.push(v);
}
}
}
}
}
int main()
{
n = rd(),m = rd();
rep(i,,n)head[i] = -;
rep(i,,m){
ll u = rd(),v = rd(),w = rd();
add(u,v,w);add(v,u,w);
}
SPFA(,);
rep(i,,){
a[i] = rd();
SPFA(i,a[i]);
}
ll path[] = {,,,,,,,};
ll ans = INF;
do{
ll di = ;
for(int i = ;i <= ;i++){
di += dis[path[i-]][a[path[i]]];
}
ans = min(ans,di);
}while(next_permutation(path+,path++));
cout << ans << endl;
return ;
}

Kattis dragonball1 Dragon Ball I(最短路)的更多相关文章

  1. HDU 4362 Dragon Ball 贪心DP

    Dragon Ball Problem Description   Sean has got a Treasure map which shows when and where the dragon ...

  2. 龙珠 超宇宙 [Dragon Ball Xenoverse]

    保持了动画气氛实现的新时代的龙珠视觉 今年迎来了[龙珠]系列的30周年,为了把他的魅力最大限度的发挥出来的本作的概念,用最新的技术作出了[2015年版的崭新的龙珠视觉] 在沿袭了一直以来优秀的动画世界 ...

  3. HDU-3872 Dragon Ball 线段树+DP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...

  4. HDU 4362 Dragon Ball 线段树

    #include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...

  5. hdu 3635 Dragon Balls(并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  6. hdu 3635 Dragon Balls (带权并查集)

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  7. hdu 3635 Dragon Balls

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tot ...

  8. hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】

    Dragon Balls Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  9. hdu 3635 Dragon Balls(并查集应用)

    Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...

随机推荐

  1. 十四、SAP中定义自定义变量

    一.利用关键字TYPE定义类型,然后在定义此类型的变量,代码如下: 二.效果如下:

  2. 想要写好Synthesis Essay,学会审题很重要

    很多留学生都不太愿意写synthesis essay,因为它的难度要比其他类型的essay要大得多.写一篇synthesis essay需要有能力消化相关的信息,通过语言组织之后再呈现.虽然这种写作技 ...

  3. NASA的10条编码规则

    关于NASA的10条编程规则,他们曾表示:这些规则的作用就像汽车上的安全带:最初,它们可能有点不舒服,但过了一会儿,它们的使用就变成了第二天性,而没有使用它们就变得不可想象. Gerard J. Ho ...

  4. GAN评价指标之mode score

    通过 Inception Score 的公式我们知道,它并没有利用到真实数据集的信息,所有的计算都在生成的图片上计算获得.而 Mode Score 基于此做了改进: 也就是说,想要提高 Mode Sc ...

  5. Node.js 发送Email

    章节 Node.js 介绍 Node.js 入门 Node.js 模块 Node.js HTTP模块 Node.js 文件系统模块 Node.js URL模块 Node.js NPM Node.js ...

  6. HTML隐藏

    JS: $("#department").hide(); JSP: <th id="department" width="5%"> ...

  7. dubbo小教程

    dubbo小教程 先给出阿里巴巴dubbo的主页:http://code.alibabatech.com/wiki/display/dubbo/Home-zh 自己的demo下载地址:http://d ...

  8. 《机实战》第2章 K近邻算法实战(KNN)

    1.准备:使用Python导入数据 1.创建kNN.py文件,并在其中增加下面的代码: from numpy import * #导入科学计算包 import operator #运算符模块,k近邻算 ...

  9. 一天一个设计模式——Strategy策略模式

    一.模式说明 策略模式比较好理解,就是将程序中用到的算法整体的拿出来,并有多个不同版本的算法实现,在程序运行阶段,动态的决定使用哪个算法来解决问题. 举个实际的例子:排序算法的问题,假如我们的程序中需 ...

  10. 手机与Arduino蓝牙串口通讯实验及完整例程

    安卓手机与Arduino之间采用蓝牙串口通讯,是很多智能装置和互动装置常用的控制方法,简单而有效,无需网络环境,很实用的技术. 实验采用Arduino UNO板,加了一块1602LCD屏做显示(因为只 ...