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. SPOJ CIRU The area of the union of circles

    You are given N circles and expected to calculate the area of the union of the circles ! Input The f ...

  2. Linux内核之网络

    应用层: 套接字将Unix一切都是内核的概念应用到网络连接中,内核跟用户空间套接字之间的接口实现在c的标准库中,使用了socketcall系统调用. socketcall充当了一个多路分解器,将各种任 ...

  3. 【leetcode】Find All Anagrams in a String

    [leetcode]438. Find All Anagrams in a String Given a string s and a non-empty string p, find all the ...

  4. Linux 之 Redis

    Linux 之 Redis 参考教程:[千峰教育] 一.Redis简介: 说明: 1.也是一种类似于Memcached的key-value机制的存储服务 2.是非关系型数据库(NoSQL)的一种 3. ...

  5. SQL盲注工具BBQSQL

    SQL盲注工具BBQSQL   SQL注入是将SQL命令插入到表单.域名或者页面请求的内容中.在进行注入的时候,渗透测试人员可以根据网站反馈的信息,判断注入操作的结果,以决定后续操作.如果网站不反馈具 ...

  6. 转载:P2P技术原理及应用(2)

    转载allen303allen的空间 在Gnutella网络中存在以下问题: 冗余消息多,对带宽的消耗存在一定的浪费.Gnutella网络协议采用泛洪式(Flooding)消息传播机制,这种消息传播机 ...

  7. 第5章 Spring Boot 功能

    Spring Boot 功能 本节将会介绍Spring Boot的一些细节. 在这里,您可以了解您将要使用和自定义的主要功能. 如果还没有准备好,您可能需要阅读第二部分“入门指南”和第三部分“使用 S ...

  8. 卸载ArcGISDesktop低版本程序遇到异常,如何完全卸载?

    [解决办法]:正常情况下,运行 ArcGIS for Desktop 光盘中的 “冲突检测”工具,会自动完全卸载低版本的ArcGIS 程序.如果遇到异常无法卸载(例如安装过非正式版软件),如下位置是A ...

  9. win7 更改同步时间的网址

    Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\D ...

  10. 微服务指南走北(三):Restful API 设计简述

    API的定义取决于选择的IPC通信方式,假设是消息机制(如 AMQP 或者 STOMP).API则由消息频道(channel)和消息类型.假设是使用HTTP机制,则是基于请求/响应(调用http的ur ...