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. debian7更换gcc版本的二种方法分享

    debian7更换gcc版本的二种方法分享   最近在编译qt,之前用的是debian6,gcc版本是gcc-4.4,当使用debian7时,编译遇到了很多跟debian6不一样的问题,debian7 ...

  2. 转载 C#开发串口总结,并提炼串口辅助类到公用类库中

    C#开发串口总结,并提炼串口辅助类到公用类库中 开发C#相关的项目有很多年了,一直没有接触串口的开发,近期由于工作的需要,需要了解熟悉对硬件串口的开发,通过对串口的深入了解,串口也不再是什么神秘的东西 ...

  3. openURL的使用

    1)私有方法跳转 /** 私有方法,不建议使用 利用ASCII值进行拼装组合方法.这样可绕过审核. 上面是进入蓝牙界面的方法.也可以有其他的页面可以跳转.设置页面是@"@"Pref ...

  4. 1.MySQL必知必会之数据库基础

    下面这几个是几个关于数据库的关键字的概念,为后面的教程做基础的: 数据库:保存有组织的数据的容器(通常是一个文件或一组文件).   表: 某种特定类型数据的结构化清单.   模式:关于数据库和表的布局 ...

  5. Spring入门学习推荐

    该作者的 spring 博客很好,作为学习用 简介 https://blog.csdn.net/column/details/15088.html?&page=1 https://blog.c ...

  6. SHUOJ Arithmetic Sequence (FFT)

    链接:http://acmoj.shu.edu.cn/problem/533/ 题意:求一个序列中,有多少三元组(其中元素不重复)在任意的排列下能构成等差数列. 分析:等差数列:\(A_j-A_i=A ...

  7. Spring MVC 复习笔记02

    1. 非注解的处理器映射器和适配器 1.1 非注解的处理器映射器 处理器映射器:org.springframework.web.servlet.handler.BeanNameUrlHandlerMa ...

  8. Web服务器端程序的实现

    Web服务器端程序主要是两个部分,一部分是主函数,一部门是命令处理函数.命令处理函数比较好理解就是针对客户端不同的命令进行处理,与客户端进行通信.主函数也有两个主要的功能,第一是要对程序进行初始化,其 ...

  9. oracle中修改表已有数据的某一列的字段类型的方法,数据备份

    1.在开发过程中经常会遇到表中的某一个字段数据类型不对,比如说需要保存的数据带小数,但是在最初设计的时候是给的number(10)类型,开始保存是整数的时候满足要求,后来在保存小数的时候 会发现自动四 ...

  10. IP查找所属网段

    最近同学接到阿里面试题 package io.guangsoft.analysis; /* 数据文件: 1.1.1.0/24,123 1.1.2.0/28,345 1.2.0.0/16,789 */ ...