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.

InputThe 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 x i ,y i  xi,yi

representing an edge, and v i  vi

representing its length.1≤x i ,y i  xi,yi

≤n,1≤v i  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 a i  ai

, the nodes that Master Dong selects out.1≤a i  ai

≤n,a i  ai

!=aj
OutputFor every Test Case, output one integer: the answerSample 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

题意:给定有向图,然后给一个集合,让你在这个集合选一个作为起点,一个作为终点,其最短路最小。

思路:上次做过树上最大收益(边权为花费,顶点有价格,求最大差价),每个点连接汇点,权值为负价;每个点连接源点,权值为正价。然后跑最长路。

此题也是一样的套路,但是起点和终点不能是同一点,所以我们需要分组。

这里分组可以随机20次;也可以用“二进制分组法”。:

举例说明,将1 2 3 4 进行分组,四个数的二进制形式为: 001 010 011 100
第一次看第0位为1的数,那么A={1,3},B={2,4}
第一次看第1位为1的数,那么A={2,3},B={1,4}
第一次看第2位为1的数,那么A={4},B={1,2,3}
可以看出,任意一对数字,都至少有一次机会不在同一组中。

由于是有向图,还得反过来来一次。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define ll long long
using namespace std;
const ll inf=;
const int maxn=;
int S,T,Laxt[maxn],Next[maxn];
int in[maxn],To[maxn],Len[maxn],a[maxn],cnt;
ll dis[maxn],ans;
vector<int>G[maxn];
void add(int u,int v,int w){
Next[++cnt]=Laxt[u]; Laxt[u]=cnt; To[cnt]=v; Len[cnt]=w;
}
void SPFA()
{
rep(i,S,T) dis[i]=inf,in[i]=;
dis[S]=; queue<int>q;
q.push(S); in[S]=;
while(!q.empty()){
int u=q.front(); q.pop();
for(int i=Laxt[u];i;i=Next[i]){
int v=To[i]; if(dis[u]+Len[i]<dis[v]) {
dis[v]=dis[u]+Len[i];
if(!in[v]) in[v]=,q.push(v);
}
}
for(int i=;i<G[u].size();i++){
int v=G[u][i]; if(dis[u]<dis[v]) {
dis[v]=dis[u];
if(!in[v]) in[v]=,q.push(v);
}
}
in[u]=;
}
ans=min(ans,dis[T]);
}
int main()
{
int C,Cas=,N,M,tot,u,v,w;
scanf("%d",&C);
while(C--){
scanf("%d%d",&N,&M); T=N+; ans=inf;
rep(j,S,T) Laxt[j]=; cnt=;
rep(i,,M) scanf("%d%d%d",&u,&v,&w),add(u,v,w);
scanf("%d",&tot);
rep(i,,tot) scanf("%d",&a[i]);
rep(i,,){
rep(j,S,T) G[j].clear();
rep(j,,tot)
if(j&(<<i)) G[S].push_back(a[j]);
else G[a[j]].push_back(T);
SPFA();
rep(j,S,T) G[j].clear();
rep(j,,tot)
if(j&(<<i)) G[a[j]].push_back(T);
else G[S].push_back(a[j]);
SPFA();
}
printf("Case #%d: %lld\n",++Cas,ans);
}
return ;
}

HDU - 6166:Senior Pan(顶点集合最短路&二进制分组)的更多相关文章

  1. HDU 6166 Senior Pan(多校第九场 二进制分组最短路)

    题意:给出n个点和m条有向边(有向边!!!!我还以为是无向查了半天),然后给出K个点,问这k个点中最近的两点的距离 思路:比赛时以为有询问,就直接丢了,然后这题感觉思路很棒,加入把所有点分成起点和终点 ...

  2. hdu 6166 Senior Pan

    http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意: 给出一张无向图,给定k个特殊点 求这k个特殊点两两之间的最短路 二进制分组 枚举一位二进制位 这一 ...

  3. HDU 6166.Senior Pan()-最短路(Dijkstra添加超源点、超汇点)+二进制划分集合 (2017 Multi-University Training Contest - Team 9 1006)

    学长好久之前讲的,本来好久好久之前就要写题解的,一直都没写,懒死_(:з」∠)_ Senior Pan Time Limit: 12000/6000 MS (Java/Others)    Memor ...

  4. HDU 6166 Senior Pan (最短路变形)

    题目链接 Problem Description Senior Pan fails in his discrete math exam again. So he asks Master ZKC to ...

  5. HDU 6166 Senior Pan(二进制分组+最短路)

    题意 给出一个\(n\)个点\(m\)条边的有向图\((n,m<=100000)\),从中选择\(k\)个点\((k<=n)\),问这k个点两两之间的最短路最小值是多少? 思路 直接的想法 ...

  6. HDU 6166 Senior Pan 二进制分组 + 迪杰斯特拉算法

    Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Probl ...

  7. 2017多校第9场 HDU 6166 Senior Pan 堆优化Dij

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意:给你一个有向图,然后给你k个点,求其中一个点到另一个点的距离的最小值. 解法:枚举二进制位 ...

  8. HDU 6166 Senior Pan(k点中最小两点间距离)题解

    题意:n个点,m条有向边,指定k个点,问你其中最近的两点距离为多少 思路:这题的思路很巧妙,如果我们直接枚举两点做最短路那就要做C(k,2)次.但是我们换个思路,我们把k个点按照二进制每一位的0和1分 ...

  9. hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥

    Senior PanⅡ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pr ...

随机推荐

  1. 对Java 静态代码块的一些了解

    一 般情况下,如果有些代码必须在项目启动的时候就执行的时候,需要使用静态代码块,这种代码是主动执行的;需要在项目启动的时候就初始化,在不创建对象的情 况下,其他程序来调用的时候,需要使用静态方法,这种 ...

  2. hadoop15---activemq

    java JMS技术 JMS是规范,activeMQ是实现. 用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信. 它类似于JDBC,JDBC 是可以用来访问许多不同关系数据库的 API. ...

  3. 笔记1:Jmeter工作原理及目录结构

    1.基本工作原理 发送request请求到服务器——获取目标服务的统计信息——生成不同格式的报告 2.完整的工作原理 Jmeter模拟用户并发进行性能测试——发送request到目标服务器——服务器返 ...

  4. powerdesign简单应用

    PowerDesigner是一款功能非常强大的建模工具软件,足以与Rose比肩,同样是当今最著名的建模软件之一.Rose是专攻UML对象模型的建模工具,之后才向数据库建模发展,而PowerDesign ...

  5. NFS 安装、管理

    NFS简介 NFS允许一个系统在网络上与他人共享目录和文件.通过使用NFS,用户和程序可以像访问本地文件一样访问远程系统上的文件. 安装NFS 服务端安装 NFS安装包:nfs-utils-lib.i ...

  6. Django如何把数据库里的html格式输出到前端

    只需在HTML页面中加入{% autoescape off %}即可! {% autoescape off %} 需要显示的数据 (% endautoescap %}

  7. 20145235李涛《网络对抗》Exp9 Web安全基础实践

    基础问答 SQL注入攻击原理,如何防御? SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意SQL命令的目的. 对于SQL注入攻击的防范 ...

  8. Book Review of “The practice of programming” (Ⅲ)

    The practice of programming Chapter 3 Design and Implementation In this section, we focus on one kin ...

  9. iOS项目开发优秀文章汇总

    UI界面 iOS和Android 界面设计尺寸规范  http://www.alibuybuy.com/posts/85486.html iPhone app界面设计尺寸规范  http://www. ...

  10. shell编程学习笔记--整数自增

    在Shell脚本中,用于while或for循环中经常要涉及到整数自增的情况,下面罗列下可能的方式 [方式一]declare -i来声明整数变量 root@localhost:~# declare -i ...