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 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
随机推荐
- 【CF56E】Domino Principle(线性扫描,伪DP)
每块多米诺骨牌所在的位置设为x,每块多米诺骨牌高度为h.如果将x位置上的多米诺骨牌向右翻到,它就可以影响[x+1, x+h-1]范围内的所有多米诺骨牌,让他们也翻到,同时这些被翻到的多米诺骨牌还能影响 ...
- windows7下如何生成ssh公钥(git相关)
1. 安装git,从程序目录打开 "Git Bash" 2. 键入命令:ssh-keygen -t rsa -C "email@email.com" &q ...
- unity3d自动寻路教程
U3D的自动寻路插件是不少,但是其实U3D的PRO版本就提供了相当实用的自动寻路组件了,以下教程分别讲解自动寻路的路径选择优先,上楼梯跳下的条件判断等等实用方法,教程分三编,但这个教程没有讲到Navm ...
- Objective-C日期相关工具方法
//date根据formatter转换成string +(NSString*)dateToString:(NSString *)formatter date:(NSDate *)date { NSDa ...
- JS快速上手-基础Javascript
1.1背景 1.1.1 ECMAScript与javascript ECMAScript是javascript的官方命名.因为java已经是一个商标.如今,一些早前收到过授权的公司,如Moailla, ...
- java中线程切换的开销
思路: 开三个线程A,B,C 线程A不断的调用LockSupport.park()阻塞自己,一旦发现自己被唤醒,调用Thread.interrupted()清除interrupt标记位,同时增加自增计 ...
- PAT 甲级 1045 Favorite Color Stripe(DP)
题目链接 Favorite Color Stripe 题意:给定$A$序列和$B$序列,你需要在$B$序列中找出任意一个最长的子序列,使得这个子序列也是$A$的子序列 (这个子序列的相邻元素可以重复) ...
- ListView 在设备切换横竖屏时保存状态
比如listview在设备切换横竖屏时,仍然需要保证position, activity - > onSaveInstanceState - > restoreInstanceState ...
- JNA 相关问题
JNA 相关问题 结构体对齐问题 要注意调用的c库字段对齐方式的相关设置. #pragma pack (push,1) #pragma pack(pop) jna中提供了4种对齐方式: /** Use ...
- Java集合——Collections工具类
Java集合——Collections工具类 摘要:本文主要学习了Collections工具类的常用方法. 概述 Collections工具类主要用来操作集合类,比如List和Set. 常用操作 排序 ...