Kattis dragonball1 Dragon Ball I(最短路)
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 |
6 |
| Sample Input 2 | Sample Output 2 |
|---|---|
5 5 |
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(最短路)的更多相关文章
- HDU 4362 Dragon Ball 贪心DP
Dragon Ball Problem Description Sean has got a Treasure map which shows when and where the dragon ...
- 龙珠 超宇宙 [Dragon Ball Xenoverse]
保持了动画气氛实现的新时代的龙珠视觉 今年迎来了[龙珠]系列的30周年,为了把他的魅力最大限度的发挥出来的本作的概念,用最新的技术作出了[2015年版的崭新的龙珠视觉] 在沿袭了一直以来优秀的动画世界 ...
- HDU-3872 Dragon Ball 线段树+DP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3872 题意:有n个龙珠按顺序放在一列,每个龙珠有一个type和一个权值,要求你把这n个龙珠分成k个段, ...
- HDU 4362 Dragon Ball 线段树
#include <cstdio> #include <cstring> #include <cmath> #include <queue> #incl ...
- hdu 3635 Dragon Balls(并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls (带权并查集)
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- hdoj 3635 Dragon Balls【并查集求节点转移次数+节点数+某点根节点】
Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tota ...
- hdu 3635 Dragon Balls(并查集应用)
Problem Description Five hundred years later, the number of dragon balls will increase unexpectedly, ...
随机推荐
- Python 中 unittest 框架加载测试用例的常用方法
unittest 当中为我们提供了许多加载用例的方法,这里说下常用的两种方法...推荐使用第二种 第一种加载测试用例的方法:使用加载器加载两个模块 需要把所有的模块加载到套件中 那么就可以自动的运行所 ...
- jQuery获取display为none的隐藏元素的宽度和高度的解决方案
1.利用给元素添加行内样式:visibility:hidden;display:block 2.让隐藏元素变成有物理尺寸存在,但不可见,获取元素宽高 3.再给它还原成display为none,去除vi ...
- 干货分享|留学Essay怎么写?
留学生活其实就是分割成一个个deadline,留学就是赶完一个又一个deadline.朋友同学的革命情感源自赶一个个deadline时候的不离不弃,相知相守,无数个夜里大家群里打卡,你今天Essay写 ...
- Spring 控制反转容器(Inversion of Control – IOC)
系列教程 Spring 框架介绍 Spring 框架模块 Spring开发环境搭建(Eclipse) 创建一个简单的Spring应用 Spring 控制反转容器(Inversion of Contro ...
- vue学习(二)Vue常用指令
2 Vue常用指令 1. vue的使用要从创建Vue对象开始 var vm = new Vue(); 2. 创建vue对象的时候,需要传递参数,是json对象,json对象对象必须至少有两个属性成员 ...
- JavaScript的运算符、条件判断、循环、类型转换(9.25 第十一天)
JS的运算符 加 + 减 - 乘 * 除 / 取余 % 自增 ++ 自减 -- 赋值运算符 a=3 a+=3 a=a=3 a-=3 a=a-3 a*=3 a=a*2 a/=3 a=a/3 ...
- Ubuntu Vi指令
Ubuntu在不更新源的情况下是没办法使用Vim指令的只能使用Vi指令 所有我也就记录了下来 vi / vim命令: 插入内容: i: 插入光标前一个字符 I: 插入行首 a: 插入光标后一个字符 A ...
- java_05_IO
java_05_IO 1,动手动脑 使用Files. walkFileTree()找出指定文件夹下所有大于指定大小(比如1M)的文件. 分析思路: 1)找到该文件夹下所有文件. 2)找出其中字节数大于 ...
- microsoft help viewer 收藏夹功能
平时重装系统比较多,重装后,microsoft help viewer 2.0里面的收藏就丢失了,要恢复以前的收藏,可以直接在C:\Users\ZR\AppData\Local\Microsoft\H ...
- 1 ~ express ~ 初始化。安装第三方模块express。中间件
一,初始化 二,安装第三方模块express 三,安装中间件 1,bodyParser : 解析 post 请求数据 2,cookies : 读写 cookie 3,swig :模板解析引擎 4,mo ...