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, ...
随机推荐
- 一百一十一、SAP的OO-ALV之五,显示ALV表格
一.在屏幕里面有2部分,(PROCESS BEFORE OUTPUT 用于显示, PROCESS AFTER INPUT用于数据处理).我们创建的display_alv函数, 二.display_al ...
- 089-PHP数组运用 - 通过循环函数取出部分成员合并成新数组
<?php function myfunc($arr){ //定义过滤函数 $j=count($arr); for($i=0;$i<$j;$i++){ if($arr[$i]>=0& ...
- 【BZOJ2400】Optimal Marks
题意 定义无向图中的一条边的值为:这条边连接的两个点的值的异或值. 定义一个无向图的值为:这个无向图所有边的值的和. 给你一个有 \(n\) 个结点 \(m\) 条边的无向图.其中的一些点的值是给定的 ...
- 用Git管理项目进行版本控制
一.安装 1.1windows 要在Windows系统中安装Git,请访问http://msysgit.github.io/,并单击Download.安装. 1.2 在 Linux 系统中安装 Git ...
- MFC之拆分窗口
7.3.1 多视图 许多文档只要求单个视图,但每个文档可支持一个以上的视图.为了帮助编程人员实现多个视图,文档对象保留它的视图列表.为添加和移去视图提供成员函数,例如,提供的UpdateAllView ...
- vue-cli 官方脚手架 eslink配置 恢复serve状态下的打印
使用官方脚手架 打印会提示保存,主要是eslink的配置(在package.json文件夹里面修改) 配置说明 下面是抄的 "eslintConfig": { "root ...
- Spring入门之一-------实现一个简单的IoC
一.场景模拟 public interface Human { public void goHome(); } Human:人类,下班了该回家啦 public interface Car { void ...
- android——TextView默认文字开发时显示运行时隐藏
根布局添加属性: xmlns:tools="http://schemas.android.com/tools" textview添加属性: tools:text="默认文 ...
- Python安装和虚拟环境创建以及外部库的安装
Python.虚拟环境.外部库的安装 一 安装Python 1 Windows 到官网下载对应的版本 下载地址 我选择的是Python3.6.8 下载完成后双击运行 !!!勾选Add Python 3 ...
- Android进阶——Android消息机制之Looper、Handler、MessageQueen
Android消息机制可以说是我们Android工程师面试题中的必考题,弄懂它的原理是我们避不开的任务,所以长痛不如短痛,花点时间干掉他,废话不多说,开车啦 在安卓开发中,常常会遇到获取数据后更新UI ...