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. K8S入门系列之集群yum安装(一)

    kubernetes master 节点包含的组件: 1.kube-apiserver :集群核心,集群API接口.集群各个组件通信的中枢:集群安全控制: 2.kube-scheduler: 集群调度 ...

  2. C#winfrom将XML数据保存读取删除

    //创建一个数据集,将其写入xml文件 string name = "1.xml"; System.Data.DataSet ds = new System.Data.DataSe ...

  3. CSS如何设置列表样式属性

    列表样式属性 在HTML中有2种列表.无序列表和有序列表,在工作中无序列表比较常用,无序列表就是ul标签和li标签组合成的称之为无序列表,那什么是有序列表呢?就是ol标签和li标签组合成的称之为有序列 ...

  4. linux日常笔记(1)

    1.SELlinux SELinux是 美国国家安全局 (NSA) 对于 强制访问控制的实现 =>可以使root受限的权限 关闭SELinux=>修改配置文件,永久生效; sed -i ' ...

  5. ACE框架 基于共享内存的分配器 (算法设计)

    继承上一篇<ACE框架 基于共享内存的分配器设计>,本篇分析算法部分的设计. ACE_Malloc_T模板定义了这样一个分配器组件 分配器组件聚合了三个功能组件:同步组件ACE_LOCK, ...

  6. C# XML解析之DOM模型

    DOM的工作方式是:首先将XML文档一次性的装入内存,然后根据文档中定义的元素和属性在内存中创建一个“树型结构”也就是一个文档对象模型,这里的含义其实是把文档对象化,文档中每个节点对应着模型中一个对象 ...

  7. HTML页面插入图片,使用background还是img标签

    很多新手在刚开始学习HTML标签的时候,老师一定会教你 <img src="xxx.png"/> 这种引入图片格式,第二天学习css的时候,老师又会教你给div等元素添 ...

  8. 在input输入值改变reducer里的值

    输入值改变reducer里的值 通过store.dispatch传入reducer中,函数的第二个参数可以接收到 在reducer中 在todolist文件中 然后在把this.state中的值改变

  9. 2019-10-8:渗透测试,基础学习,php基础,会话,文件包含,笔记

    php面向对象基础->调用符号构造函数construct,主要用来创建对象时初始化对象,为成员变量赋初始值,总与new运算符一起使用在创建对象的语句中 析构函数destructor,与构造函数相 ...

  10. 探索 IPv6 网络

    目录 0x00 前言 0x01 探索 服务器配置 IPv6 地址 服务器部署网络代理 客户端配置网络代理 测试访问 IPv6 地址 给博客添加 IPv6 地址 0x00 前言 IPv4 地址枯竭的事情 ...