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中内表插入的三种方法之三,INSERT的使用,用于指定位置插入

    一.代码如下 二.需要注意的时候,如果内表和工作区同名,这可以用隐式插入,不需要什么工作区INTO到什么表,INDEX为位置,效果图如下:

  2. oracle数据泵导出导入

    先创建一个目录:比如 Create  or Replace directory  DATA_PUMP_DIR as 'D:\DataPipe';   然后给导入导出的用户赋权限: Grant read ...

  3. 干货分享:深度解析Supplement Essay写作

    今天Hotessay小编给同学们介绍下附加文书的创作思路.因为附加文书基本上都是短essay,所以简洁才是硬道理! 通常,我们可以把美国大学的附加文书分为以下几类: 1.Tell us about y ...

  4. 干货分享|留学Essay怎么写?

    留学生活其实就是分割成一个个deadline,留学就是赶完一个又一个deadline.朋友同学的革命情感源自赶一个个deadline时候的不离不弃,相知相守,无数个夜里大家群里打卡,你今天Essay写 ...

  5. Python写一个简单的爬虫

    code #!/usr/bin/env python # -*- coding: utf-8 -*- import requests from lxml import etree class Main ...

  6. spring boot集成mybatis(1)

    Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...

  7. 前端01 HTML5

    01工具使用 Sublime插件安装和使用 2Sublime插件安装和使用.pcf 3Sublime插件安装与卸载.pcf 4Sublime安装markdown插件.pcf 使用typora生成pdf ...

  8. php mysql 由于目标计算机积极拒绝,无法连接 错误原因

    除了在网上百度的那些外,我的机器发生了这个错误 我需要用php远程连接mysql,在目标机上已经给了客户机权限,可是还是发生“ 由于目标计算机积极拒绝,无法连接”错误 在客户机上直接用终端连接目标机的 ...

  9. MongoDB三-高级操作

    复制来自:http://www.cnblogs.com/huangxincheng/archive/2012/02/21/2361205.html 今天跟大家分享一下mongodb中比较好玩的知识,主 ...

  10. 阅读笔记--java内存模型原理

    在阅读本篇文章之前,我所理解的和上网了解到的java内存模型原理如下: 不同架构的物理计算机可以有不一样的内存模型,Java 虚拟机也有自己的内存模型.Java 虚拟机规范中试图定义一种 Java 内 ...