Senior Pan

Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)

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

题解:

  答案路径的起点终点,必然是不同的点,也就是在二进制下存在一位是不同,根据每一位分组,0,1分别分为起点集和终点集

  跑一遍多源多汇最短路即可,就是40次dij

#include <bits/stdc++.h>
inline int read(){int x=,f=;char ch=getchar();while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}return x*f;}
using namespace std; const int N = ; vector<pair<int,int > > G[N];
int vis[N], n, m, go[N], k, dis[N];
void init() {
memset(vis,,sizeof(vis));
for(int i = ; i <= n; ++i) G[i].clear();
}
priority_queue<pair<int,int> , vector<pair<int,int> > , greater<pair<int,int> > > q;
void dij() {
for(int i = ; i <= n; ++i)
dis[i] = ;
for(int i = ; i <= n; ++i)
if(go[i] == )
{
dis[i] = ;
q.push(make_pair(,i));
}
while(!q.empty()) {
pair<int,int >now = q.top();
q.pop();
int v = now.second;
for(auto son : G[v]) {
int to = son.first;
if(dis[to] > dis[v] + son.second){
dis[to] = dis[v] + son.second;
q.push(make_pair(dis[to],to));
}
}
}
} int main() {
int T, cas = , x, u, v, w;
cin >> T;
while(T--) {
cin >> n >> m;
init();
for(int i = ; i <= m; ++i)
{
scanf("%d%d%d",&u,&v,&w);
G[u].push_back(make_pair(v,w));
}
cin >> k;
for(int i = ; i <= k; ++i)
scanf("%d",&x), vis[x] = ;
int ans = ;
for(int j = ; j < ; ++j) {
for(int i = ; i <= n; ++i)
{
go[i] = ;
if(!vis[i]) continue;
if((i>>j)&) go[i] = ;
else go[i] = -;
}
dij();
for(int i = ; i <= n; ++i)
{
if(!vis[i]) continue;
if(go[i] == -)
ans = min(ans,dis[i]);
}
for(int i = ; i <= n; ++i)
{
go[i] = ;
if(!vis[i]) continue;
if((i>>j)&) go[i] = -;
else go[i] = ;
}
dij();
for(int i = ; i <= n; ++i)
{
if(!vis[i]) continue;
if(go[i] == -)
ans = min(ans,dis[i]);
}
}
cout << "Case #" << (cas++) << ":" << " " << ans << endl;
}
}

HDU 6166 Senior Pan 二进制分组 + 迪杰斯特拉算法的更多相关文章

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

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

  2. HDU 1874畅通工程续(迪杰斯特拉算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others)     ...

  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. HDU6166-Senior Pan-Dijkstra迪杰斯特拉算法(添加超源点,超汇点)+二进制划分集合-2017多校Team09

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

  5. HDU 2544最短路 (迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others)    Me ...

  6. HDU 3790(两种权值的迪杰斯特拉算法)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others)    ...

  7. HDU 2680 最短路 迪杰斯特拉算法 添加超级源点

    Choose the best route Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  8. C#迪杰斯特拉算法

    C#迪杰斯特拉算法 网上有许多版本的,自己还是写一个理解点 Dijkstra.cs public class Dijkstra { private List<Node> _nodes; p ...

  9. C++迪杰斯特拉算法求最短路径

    一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...

随机推荐

  1. utilize HttpClient to generate a SSL access and generate REST access to fetch data, async programming? cool and brief

    WebRequestHandler handler = new WebRequestHandler(); try { X509Certificate2 certificate = new X509Ce ...

  2. php validator classes

    <?php /** * 验证类 */ class Validator { /* 函数名称:isNumber 简要描述:检查输入的是否为数字 输入:string 输出:boolean */ pub ...

  3. 论epoll的实现

    论epoll的实现 上一篇博客 论select的实现 里面已经说了为什么 select 比较慢.poll 的实现和 select 类似,只是少了最大 fd 限制,如果有兴趣可以自己去看代码.我这里来简 ...

  4. python 修饰器 最好的讲解

    Python的修饰器的英文名叫Decorator,修饰器就是对一个已有的模块做一些“修饰工作”,比如在现有的模块加上一些小装饰(一些小功能,这些小功能可能好多模块都会用到),但又不让这个小装饰(小功能 ...

  5. 如何快速定位TempDB产生问题

    步骤1.TempDB压力诊断 等待类型诊断 TempDB的争用压力在等待篇中已经简单介绍,等待的表现为 pagelatch_类等待,等待的资源是 “2: X :X ” tempDB所在磁盘的响应时间 ...

  6. 咦?Oracle归档文件存哪了?

    实验环境:RHEL 5.4 + Oracle 11.2.0.3 现象:日志切换后没找到归档日志目录. 1.查看归档日志路径 2.日志切换后并未找到归档目录 3.创建归档目录后再次观察 引申知识 1.查 ...

  7. Nginx+keepalived双机热备(主主模式)

    IP说明: master机器(master-node):10.0.0.5/172.16.1.5   VIP1:10.0.0.3slave机器(slave-node): 10.0.0.6/172.16. ...

  8. Springboot 集成 Thymeleaf 及常见错误

    Thymeleaf模板引擎是springboot中默认配置,与freemarker相似,可以完全取代jsp,在springboot中,它的默认路径是src/main/resources/templat ...

  9. javascript --- 移除DOM节点

    在IE中移除容器类节点,会引起内存泄露,最好是创建一个新的节点,比如div,然后将要删除的节点放入这个div中,再将div的innerHTML清空.其它的直接removeChild就可以了. var ...

  10. Android 源码编译记录

    问题1:Can't locate Switch.pm in @INC (you may need to install the Switch module) (@INC contains: /etc/ ...