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 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...
随机推荐
- 【CF1017B】The Bits(模拟)
题意:给定两个二进制数字a,b,可以任意交换a中的两个bit位,求进行这样一次操作,最多可产生多少种不同的a or b n<=1e5 思路:模拟,分类讨论 #include<cstdio& ...
- 【POJ2104】K-th Number(主席树)
题意:有n个数组成的序列,要求维护数据结构支持在线的下列两种操作: 1:单点修改,将第x个数修改成y 2:区间查询,询问从第x个数到第y个之间第K大的数 n<=100000,a[i]<=1 ...
- es6总结(四)--对象
- WebRTC VideoEngine综合应用示例(一)——视频通话的基本流程(转)
本系列目前共三篇文章,后续还会更新 WebRTC VideoEngine综合应用示例(一)——视频通话的基本流程 WebRTC VideoEngine综合应用示例(二)——集成OPENH264编解码器 ...
- linux 下 异步IO
方法一:使用fcntl来置O_ASYNC位. 这个方法的效果是,当输入缓存中的输入数据就绪时(输入数据可读),内核向用F_SETOWN来绑定的那个进程发送SIGIO信号.此时程序应该用getchar等 ...
- locust性能测试安装
Locust简介 Locust是一款易于使用的分布式用户负载测试工具.它用于对网站(或其他系统)进行负载测试,并确定系统可以处理多少并发用户.这个想法是,在测试期间,一群蝗虫(Locust)会攻击你的 ...
- Devexpress控件中gridcontrol Drag a column header here to group by that column 更换
参照网站:http://documentation.devexpress.com/#WPF/DevExpressXpfGridDataViewBase_RuntimeLocalizationStrin ...
- SQL 列转行与行转列
假设有张学生成绩表(tb)如下:Name Subject Result张三 语文 74张三 数学 83张三 物理 93李四 语文 74李四 数学 84李四 物理 94*/ -------------- ...
- ubuntu 配置 django
安装 安装Apache sudo apt-get install apache2 安装Django 下载Django 安装mod_wsgi sudo apt-get install libapache ...
- Linux进程调度(3):进程切换分析
3.调度函数schedule()分析 当kernel/sched.c:sched_tick()执行完,并且时钟中断返回时,就会调用kernel/sched.c:schedule()完成进程切换.我们 ...