Senior Pan

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1462    Accepted Submission(s): 573

Problem Description
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,yi representing an edge, and vi representing its length.1≤xi,yi≤n,1≤vi≤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 ai, the nodes that Master Dong selects out.1≤ai≤n,ai!=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
 
Source
    给出N个点和M条边的无向图,从中选出K个互不相同的点组成集合D,问从D中挑选任意两点间的最短路最小是多少。
如果让每个点都跑一次dij的话肯定会T。
       首先我们要知道对于求两个集合之间的最短路只要对每个集合建立一个超级点跑一次dij便可得到。可以想办法把这个集合划分成若两个个小集合然后减少dij运行次数,要满足所有的点对都会分在两个不同的集合中。利用二进制,两个点的编号都不相同,那么对于她们的二进制而言一定是有一位是不同的,点数最大为1e5,最多也就16位足以,这样每次根据第b位为0/1进行集合划分,最后就考虑了所有情况。第一发我让超级点建了双向边(为了省事)结果T了,后来每次都重新建边就3s过了。

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<functional>
using namespace std;
#define LL long long
#define pii pair<int,int>
#define mp make_pair
#define inf 0x3f3f3f3f
int u[],v[],w[];
struct Edge{
int v,w,next;
Edge(){}
Edge(int v,int w,int next):v(v),w(w),next(next){}
}e[];
int first[],tot;
void add(int u,int v,int w){
e[tot]=Edge(v,w,first[u]);
first[u]=tot++;
}
bool vis[];
int d[];
int N,M,K;
void dij(int s){
memset(vis,,sizeof(vis));
memset(d,inf,sizeof(d));
d[s]=;
priority_queue<pii,vector<pii>,greater<pii> >q;
q.push(mp(,s));
while(!q.empty()){
int u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=;
for(int i=first[u];i+;i=e[i].next){
if(d[e[i].v]>d[u]+e[i].w){
d[e[i].v]=d[u]+e[i].w;
q.push(mp(d[e[i].v],e[i].v));
}
}
}
}
int main()
{
int t,i,a,j;
int cas=;
cin>>t;
while(t--){
vector<int>vi;
cin>>N>>M;
tot=;
for(i=;i<=M;++i){
scanf("%d%d%d",u+i,v+i,w+i);
}
scanf("%d",&K);
for(i=;i<=K;++i){
scanf("%d",&a);
vi.push_back(a);
}
printf("Case #%d: ",++cas);
int ans=inf;
for(int b=;b<;++b){
tot=;
memset(first,-,sizeof(first));
for(i=;i<=M;++i) add(u[i],v[i],w[i]);
for(i=;i<vi.size();++i){
if((vi[i]&(<<b))==){
add(,vi[i],);
}
else{
add(vi[i],N+,);
}
}
dij();
ans=min(ans,d[N+]); tot=;
memset(first,-,sizeof(first));
for(i=;i<=M;++i) add(u[i],v[i],w[i]);
for(i=;i<vi.size();++i){
if((vi[i]&(<<b))==){
add(vi[i],,);
}
else{
add(N+,vi[i],);
}
} dij(N+);
ans=min(ans,d[]);
}
cout<<ans<<endl;
}
return ;
}

HDU6166-求集合间的最短路的更多相关文章

  1. 零基础学习java------day14-----泛型,foreach,可变参数,数组和集合间的转换,Set,Map,

    1.泛型(jdk1.5以后出现) https://www.cnblogs.com/lwbqqyumidi/p/3837629.html#!comments (1)为什么要用泛型? 限制集合,让它只能存 ...

  2. DFS算法-求集合的所有子集

    目录 1. 题目来源 2. 普通方法 1. 思路 2. 代码 3. 运行结果 3. DFS算法 1. 概念 2. 解题思路 3. 代码 4. 运行结果 4. 对比 1. 题目来源 牛客网,集合的所有子 ...

  3. boost dijkstra获得两点间的最短路

    需求是只需要得到两点间的最短路,不需要求得单源对于全图的最短路,使用boost中的dijsktra_shortest_path,当得到目标点的最短路时直接throw exception. #inclu ...

  4. 求集合中选一个数与当前值进行位运算的max

    求集合中选一个数与当前值进行位运算的max 这是一个听来的神仙东西. 先确定一下值域把,大概\(2^{16}\),再大点也可以,但是这里就只是写写,所以无所谓啦. 我们先看看如果暴力求怎么做,位运算需 ...

  5. hdu 1856 求集合里元素的个数 输出最大的个数是多少

    求集合里元素的个数 输出最大的个数是多少 Sample Input41 23 45 61 641 23 45 67 8 Sample Output42 # include <iostream&g ...

  6. SQL_求集合中每天最大时间记录的总和

    --问题求 集合中每天最大时间的总和 表中的数据 列: 用户 分数 时间 A 2 2014-01-01 01:00:00 A 2 2014-01-01 02:00:00 A 2 2014-01-01 ...

  7. [C++]boost dijkstra获得两点间的最短路

    需求是只需要得到两点间的最短路,不需要求得单源对于全图的最短路,使用boost中的dijsktra_shortest_path,当得到目标点的最短路时直接throw exception. #inclu ...

  8. [CF1051F]The Shortest Statement (LCA+最短路)(给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路)

    题目:给定一张n个点m条有权边的无向联通图,q次询问两点间的最短路 n≤100000,m≤100000,m-n≤20. 首先看到m-n≤20这条限制,我们可以想到是围绕这个20来做这道题. 即如果我们 ...

  9. AOJ -0189 Convenient Location && poj 2139 Six Degrees of Cowvin Bacon (floyed求任意两点间的最短路)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=78207 看懂题就好. 求某一办公室到其他办公室的最短距离. 多组输入,n表示 ...

随机推荐

  1. js 根据json数组中n个字段排序

    function compare(name, minor) { return function (o, p) { var a, b; if (o && p && typ ...

  2. Code signing is required for product type 'Application' in SDK 'iOS 11.2'

    在打包的时候出现这样一个错误,Code signing is required for product type 'Application' in SDK 'iOS 11.2'  ,就是说代码签名证书 ...

  3. 手把手教 GitHub + Hexo 搭建博客

    前言 在很久以前,博主就想着要有自主的博客专栏或者网站.经历了博客园这个需要所谓的编辑审核,一直比较困惑,这些编辑是什么出身,怎么知道技术博客的价值性. 接下来找到了开源中国,这个可以自由发言的地方. ...

  4. WebBrowser自动填充打开文件对话框

    WebBrowser自动填充打开文件对话框   在使用WebBrowser编写自动表单填写软件的时候,不知道大家是否遇到国填写文件选择表单的情况.遇到这种情况的时候,无法直接队Html元素赋值,必须模 ...

  5. JS:parseInt("08")或parseInt("09")转换返回0的原因

    一.parseInt用法 parseInt(s); parseInt(s,radix) 二.第一个方式不再多说,第二个方式,radix是s所基于的进制.范围为2-36(不在此范围函数将返回NaN). ...

  6. git安装教程(windows安装)

    git下载地址 https://git-scm.com/download/win 选择安装的组件,推荐全选 Additional icons 附加图标 ​ On the Desktop 在桌面上 Wi ...

  7. 前端JS复制特定区域的文本(兼容safari)

    html5的webAPI接口可以很轻松的使用短短的几行代码就实现点击按钮复制区域文本的功能,不需要依赖flash. 代码如下: /* 创建range对象 */ const range = docume ...

  8. 在使用Kafka过程中遇到的错误

    问题一:用kafka命令发送消息时候,一直报WARN Error while fetching metadata with correlation id 0 : {test=UNKNOWN_TOPIC ...

  9. SeekArc

    https://github.com/neild001/SeekArc https://github.com/imflyn/SeekArc

  10. kafka监控搭建

    1. 下载 wget https://github.com/quantifind/KafkaOffsetMonitor/releases/download/v0.2.1/KafkaOffsetMoni ...