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. css 自定义滚动条

    我遇到的场景: 对于iframe窗口,自带滚动条是整个窗口的大小.有时需要顶部或底部固定,则滚动条不应该触碰到顶部或底部. 那么首先打开iframe时应该去掉滚动条 scrolling="n ...

  2. Python编程题总结

    1 数学篇 1.1 斐波那契数列 def fib(n): if n in (0, 1): return n fib_n1, fib_n2 = 0, 1 for i in range(2, n + 1) ...

  3. filebeat+logstash通过zabbix微信报警

    一.安装软件: 1.在要收集日志的机器上安装filebeat: 1).下载安装: cd /usr/local/src wget https://artifacts.elastic.co/downloa ...

  4. 2018.9 ECNU ICPC/CCPC Trial Round #2 Query On Tree (树链剖分+线段树维护)

    传送门:https://acm.ecnu.edu.cn/contest/105/problem/Q/ 一棵树,支持两种操作:给一条路径上的节点加上一个等差数列;求两点路径上节点和. 很明显,熟练剖分. ...

  5. a=b=c 连等赋值的分析

    首先 先抛出两个例子,大家想想结果是什么? eg1: var a = 1; var b = a; a.x = a = 3; 问 a = ?  |  b = ?  |  a.x = ? eg2: var ...

  6. 多线程中sleep和wait的区别,以及多线程的实现方式及原因,定时器--Timer

    1.  Java中sleep和wait的区别 ① 这两个方法来自不同的类分别是,sleep来自Thread类,和wait来自Object类. sleep是Thread的静态类方法,谁调用的谁去睡觉,即 ...

  7. WCF使用安全证书验证消息加密

    首先安装 服务端安全证书  代码如下:  // 下面第一行是安装证书,第二行是将证书列入信任 makecert.exe -sr LocalMachine -ss MY -a sha1 -n CN=lo ...

  8. 20145312 《Java程序设计》第10周学习总结

    20145312 <Java程序设计>第10周学习总结 学习总结 一. 什么是网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的 ...

  9. 20145328 《Java程序设计》第2周学习总结

    20145328 <Java程序设计>第2周学习总结 教材学习内容总结 掌握了上周没有学会的IDEA的用法 掌握了一些快捷键用法,在用IDEA编写程序的过程中的体验比直接使用cmd进行编写 ...

  10. VS+Qt

    1.安装vs 2.安装qt[带msvc编译器的] 3.安装addin插件 4.新建qt app项目 5.在qt options里添加qt版本 路径添加到msvc那一层,如:E:\Qt5.9\5.9\m ...