HDU 6166 Senior Pan 二进制分组 + 迪杰斯特拉算法
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 二进制分组 + 迪杰斯特拉算法的更多相关文章
- HDU 6166 Senior Pan(二进制分组+最短路)
题意 给出一个\(n\)个点\(m\)条边的有向图\((n,m<=100000)\),从中选择\(k\)个点\((k<=n)\),问这k个点两两之间的最短路最小值是多少? 思路 直接的想法 ...
- HDU 1874畅通工程续(迪杰斯特拉算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) ...
- HDU 6166.Senior Pan()-最短路(Dijkstra添加超源点、超汇点)+二进制划分集合 (2017 Multi-University Training Contest - Team 9 1006)
学长好久之前讲的,本来好久好久之前就要写题解的,一直都没写,懒死_(:з」∠)_ Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memor ...
- HDU6166-Senior Pan-Dijkstra迪杰斯特拉算法(添加超源点,超汇点)+二进制划分集合-2017多校Team09
学长好久之前讲的,本来好久好久之前就要写题解的,一直都没写,懒死_(:з」∠)_ Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memor ...
- HDU 2544最短路 (迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Time Limit: 5000/1000 MS (Java/Others) Me ...
- HDU 3790(两种权值的迪杰斯特拉算法)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=3790 最短路径问题 Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 2680 最短路 迪杰斯特拉算法 添加超级源点
Choose the best route Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- C#迪杰斯特拉算法
C#迪杰斯特拉算法 网上有许多版本的,自己还是写一个理解点 Dijkstra.cs public class Dijkstra { private List<Node> _nodes; p ...
- C++迪杰斯特拉算法求最短路径
一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
随机推荐
- 洛谷 [P2859] 摊位预定
贪心 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm ...
- IP,子网掩码,网关,DNS的关系解析
IP地址: 是给每个连接在Internet上的主机分配的一个32bit地址. 地址有两部分组成,一部分为网络地址,另一部分为主机地址. IP地址分为A.B.C.D.E 5类.常用的是B和C两类. 网络 ...
- linux内核栈与用户栈【转】
转自:http://19880512.blog.51cto.com/936364/274610 最近linux内核的中断部分,总是被书里的栈弄晕,一会儿内核栈,一会儿用户栈的……很是崩溃,在网上goo ...
- TStringList,快速解析 查找测试。。。很有用,再也不用 FOR 循环了
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABKAAAALHCAIAAAA2Gq0zAAAgAElEQVR4nOydeVgUV76wK5OZb5JJZi
- js对象定义的最常用的三种方法
定义对象:属性和方法的结合体(变量和函数的结合体) 1.(***)var obj = {} 2.var obj = new Object(); 3.使用function定义对象 具体例子分别为: // ...
- Java 网络通信【01】TCP
不积跬步,无以至千里:不积小流,无以成江海.——<荀子劝学> JAVA中设计网络编程模式的主要有TCP和UDP两种. TCP是属于即时通信,点对点连接进行通信. UDP是通过数据包来进行通 ...
- 洛谷——P2296 寻找道路
P2296 寻找道路 题目描述 在有向图G 中,每条边的长度均为1 ,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足以下条件: 1 .路径上的所有点的出边所指向的点都直接或间接与终点 ...
- JAVA通过使用sort方法排序
java 代码: 对集合排序: //升序public void listSort1(){ List<Integer> list = new ArrayList<Integer> ...
- JDK1.8日期时间库学习
这周在阅读<阿里巴巴Java开发手册>时,在并发处理一节的日期处理中,其强调SimpleDateFormat 是线程不安全的类,一般不要定义为 static 变量,如果 定义为 stati ...
- mac 安装opencv-python
Mac下安装opencv-python 项目中使用的是opencv 2的版本,因此下面说一下opencv2的一些安装流程. 安装方法 1:如果没有homebrew的话,需要先安装 安装命令: ruby ...