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, ...
随机推荐
- 062-PHP函数按值传参,交换数值函数
<?php function swap($x,$y){ //定义交换数值函数 $temp=$x; $x=$y; $y=$temp; } $m=5; $n=15; echo "交换前:& ...
- K均值聚类算法
k均值聚类算法(k-means clustering algorithm)是一种迭代求解的聚类分析算法,其步骤是随机选取K个对象作为初始的聚类中心,然后计算每个对象与各个种子聚类中心之间的距离,把每个 ...
- Apache NiFi Processor实战
1 前言 Apache NiFi是什么?NiFi官网给出如下解释:“一个易用.强大.可靠的数据处理与分发系统”.通俗的来说,即Apache NiFi 是一个易于使用.功能强大而且可靠的数据处理和分发系 ...
- SHELL学习笔记三
SHELL学习笔记一 SHELL学习笔记二 SHELL学习笔记三 for 命令 读取列表中的复杂值 从变量读取列表 从命令读取值 更改字段分隔符 用通配符读取目录 which 使用多个测试命令 unt ...
- ls 查看文件
1.按文件大小查看文件 a.降序:ls -lsh moudaen@morton:~$ ls -lshtotal 20M 20M -rw-r--r-- 1 moudaen 65536 20M Nov ...
- Windows2008R2安装DNS和SQLServer200r2服务 (9.18第七天)
原文网址:https://www.cnblogs.com/yankaohaitaiwei/p/11538205.html 二.IIS搭建web服务器 1.格式化D盘,一定要选择NTFS!!!不然后面添 ...
- UVA - 1645 Count (统计有根树)(dp)
题意:输入n(n <=1000),统计有多少个n结点的有根树,使得每个深度中所有结点的子结点数相同.输出数目除以109+7的余数. 分析: 1.dp[i],i个结点的有根树个数 2.假设n=7, ...
- 【LeetCode】验证二叉搜索树
[问题]给定一个二叉树,判断其是否是一个有效的二叉搜索树. 假设一个二叉搜索树具有如下特征:节点的左子树只包含小于当前节点的数.节点的右子树只包含大于当前节点的数.所有左子树和右子树自身必须也是二叉搜 ...
- spring 官方文档-片段学习——webflux-ann-controller
spring 官方文档-片段学习总结 片段所在连接:https://docs.spring.io/spring/docs/5.0.4.RELEASE/spring-framework-referenc ...
- UIWindow的那些事
UIView是视图的基类,UIViewController是视图控制器的基类,UIResponder是表示一个可以在屏幕上响应触摸事件的对象: 一.UIWindow是一种特殊的UIView,通常在一个 ...