Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory problems everyday. 

The task is simple : ZKC will give Pan a directed graph every time, and selects some nodes from that graph, you can calculate the minimum distance of every pair of nodes chosen in these nodes and now ZKC only cares about the minimum among them. That is still too hard for poor Pan, so he asks you for help.

Input

The first line contains one integer T, represents the number of Test Cases.1≤T≤5.Then T Test Cases, for each Test Cases, the first line contains two integers n,m representing the number of nodes and the number of edges.1≤n,m≤100000

Then m lines follow. Each line contains three integers xi,yixi,yi representing an edge, and vivi representing its length.1≤xi,yixi,yi≤n,1≤vivi≤100000 

Then one line contains one integer K, the number of nodes that Master Dong selects out.1≤K≤n 

The following line contains K unique integers aiai, the nodes that Master Dong selects out.1≤aiai≤n,aiai!=aj

Output

For every Test Case, output one integer: the answer

Sample Input

1
5 6
1 2 1
2 3 3
3 1 3
2 5 1
2 4 2
4 3 1
3
1 3 5

Sample Output

Case #1: 2

题意:给你一个无向图,然后给你K个数字,然后让你求任意两点之间的最短距离,并输出这些最短距离的最小值;

题解:首先考虑最短路径的算法,Foyld肯定不行O(n^3);Dijkstra与SPFA都是O(nlog(n))。然后由于K的范围是10^5级别的

如果暴力跑Dijkstra的话  n*(n+1)/2次。。。。 我们可以考虑二进制,任意两个数字至少有一位是不同的,所以我们枚举每一位上不同的点(也就17次就够了),将其分别放入两个集合,建立超级源点(0),和超级汇点(n+1),分别将两个集合与超级源点和超级汇点相连,距离为0,这样Dijkstra的单源多汇最短路就变为了,多源多汇最短路了,然后用优先队列优化的Dijkstra跑最短路即可,也就17*2次,O(nlog(n)),时间复杂度满足要求;

参考代码如下:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const LL INF=0x3f3f3f3f3f3f3f3fLL;
const int maxn=1e5+10; int n,m,k,tot;
int u[maxn],v[maxn],vis[maxn],head[maxn],num[maxn];
LL w[maxn],dis[maxn]; struct Edge{
int u,v;
LL w;
Edge(int uu,int vv,LL ww) : u(uu),v(vv),w(ww) { }
}; vector<Edge> vec[maxn]; struct Node{
int id;
LL W;
bool operator < (const Node &b) const
{
return W>b.W;
}
}; void addedge(int u,int v,LL w)
{
vec[u].push_back(Edge(u,v,w));
} priority_queue<Node> q; LL Dijkstra()
{
memset(vis,0,sizeof vis);
memset(dis,INF,sizeof dis);
dis[0]=0;
q.push(Node{0,0} );
while(!q.empty())
{
Node u=q.top(); q.pop();
int Id=u.id;
if(vis[Id]) continue;
vis[Id]=1;
for(int i=0;i<vec[Id].size();i++)
{
if(!vis[vec[Id][i].v] && dis[vec[Id][i].v]>dis[Id]+vec[Id][i].w)
{
dis[vec[Id][i].v]=dis[Id]+vec[Id][i].w;
q.push(Node{vec[Id][i].v,dis[vec[Id][i].v]});
}
}
}
return dis[n+1];
} void Init()
{
tot=0;
memset(head,-1,sizeof head);
for(int i=0;i<=n+1;i++) vec[i].clear();
} int main()
{
int Cas=0,T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++) scanf("%d%d%lld",u+i,v+i,w+i);
scanf("%d",&k);
for(int i=1;i<=k;i++) scanf("%d",num+i);
LL ans=INF;
for(int bit=0;bit<=17;bit++)
{
Init();
for(int i=1;i<=m;i++) addedge(u[i],v[i],w[i]);
for(int i=1;i<=k;i++)
{
if(num[i]&(1<<bit)) addedge(0,num[i],0);
else addedge(num[i],n+1,0);
}
ans=min(ans,Dijkstra()); Init();
for(int i=1;i<=m;i++) addedge(u[i],v[i],w[i]);
for(int i=1;i<=k;i++)
{
if((num[i]&(1<<bit))==0) addedge(0,num[i],0);
else addedge(num[i],n+1,0);
}
ans=min(ans,Dijkstra());
}
printf("Case #%d: %lld\n",++Cas,ans);
} return 0;
}

(全国多校重现赛一)F-Senior Pan的更多相关文章

  1. (全国多校重现赛一)B-Ch's gifts

    Mr. Cui is working off-campus and he misses his girl friend very much. After a whole night tossing a ...

  2. (全国多校重现赛一)D Dying light

    LsF is visiting a local amusement park with his friends, and a mirror room successfully attracts his ...

  3. (全国多校重现赛一) J-Two strings

    Giving two strings and you should judge if they are matched.  The first string contains lowercase le ...

  4. (全国多校重现赛一) H Numbers

    zk has n numbers a1,a2,...,ana1,a2,...,an. For each (i,j) satisfying 1≤i<j≤n, zk generates a new ...

  5. (全国多校重现赛一)E-FFF at Valentine

    At Valentine's eve, Shylock and Lucar were enjoying their time as any other couples. Suddenly, LSH, ...

  6. (全国多校重现赛一)A-Big Binary Tree

    You are given a complete binary tree with n nodes. The root node is numbered 1, and node x's father ...

  7. 长春理工大学第十四届程序设计竞赛(重现赛)F.Successione di Fixoracci

    链接:https://ac.nowcoder.com/acm/contest/912/F 题意: 动态规划(Dynamic programming,简称dp)是一种通过把原问题分解为相对简单的子问题的 ...

  8. 长春理工大学第十四届程序设计竞赛(重现赛)F

    F. Successione di Fixoracci 题目链接:https://ac.nowcoder.com/acm/contest/912/F 题目: 动态规划(Dynamic programm ...

  9. 2019年湘潭大学程序设计竞赛(重现赛)F.Black&White

    传送门 F.Black&White •题意 操作 m 次后,求连续的1或连续的0的最大值,每次操作只能反转一个位置: •思路1(反悔操作) 定义队列q:依次存放两个零之间的1的个数+1: 首先 ...

随机推荐

  1. 019.Kubernetes二进制部署插件dashboard

    一 修改配置文件 1.1 下载解压 [root@k8smaster01 ~]# cd /opt/k8s/work/kubernetes/ [root@k8smaster01 kubernetes]# ...

  2. abp(net core)+easyui+efcore实现仓储管理系统——ABP WebAPI与EasyUI结合增删改查之一(二十七)

    abp(net core)+easyui+efcore实现仓储管理系统目录 abp(net core)+easyui+efcore实现仓储管理系统——ABP总体介绍(一) abp(net core)+ ...

  3. spark和 mapreduce的比较

    网上查阅一些资料,收集整理如下: 1. 通用性 spark更加通用,spark提供了transformation和action这两大类的多个功能api,另外还有流式处理sparkstreaming模块 ...

  4. 在CentOS安装消息中间件RabbitMQ

    一.在安装前,请确认CentOS是否可以联网 1.可以用SecureCRT工具连接centos,这方面请自行百度. 2.CRT连接成功后可以通过ping www.baidu.com 查看是否成功,确认 ...

  5. Json模块和Pickle模块的使用

    在对数据进行序列化和反序列化是常见的数据操作,Python提供了两个模块方便开发者实现数据的序列化操作,即 json 模块和 pickle 模块.这两个模块主要区别如下: json 是一个文本序列化格 ...

  6. vue项目中安装使用vux

    vux是个vue的移动端框架. 目前移动端UI框架这么多,为啥选择vux呢?vux虽然说是个个人维护项目,但是有15000+个star,应该不比其他的团队开源框架差. 最重要的是,目前要做微信公众号和 ...

  7. 2019-9-19:渗透测试,基础学习,html常用标签,笔记

    HTML常用标签<br>:换行<meta charset="utf-8">:编码标签<script type="路径">:插 ...

  8. 【浅析】|白话布隆过滤器BloomFilter

    通过本文将了解到以下内容: 查找问题的一般思路 布隆过滤器的基本原理 布隆过滤器的典型应用 布隆过滤器的工程实现 场景说明: 本文阐述的场景均为普通单机服务器.并非分布式大数据平台,因为在大数据平台下 ...

  9. DPT-RP1 解锁过程整理

    前言 首先,感谢大神HappyZ ,没有他的教程,没有下文了. 其次,要感谢的是润物 ,没有她的教程, 可能要研究好久才能弄明白大神给的工具怎么用. 本人没接触过python,以为在命令行执行Pyth ...

  10. 从零开始入门 K8s | 深入剖析 Linux 容器

    作者 | 唐华敏(华敏)  阿里云容器平台技术专家 本文整理自<CNCF x Alibaba 云原生技术公开课>第 15 讲. 关注"阿里巴巴云原生"公众号,回复关键词 ...