Senior Pan

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1411    Accepted Submission(s): 558

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
题目大意:给定一个n个点m条边的有向带权图,其中有k个特殊点,求这k个特殊点之间两两最短路的最小值。
分析:没做过这类题根本想不到.
   因为是求两两最短路的最小值,可以考虑把dijkstra变成多源的,也就是把这k个特殊点分成两组,一组当作源点,一组当作汇点,因为dijkstra算法每次找距离最小的出来扩展嘛,那么作为源点的一组最先被扩展到的距离就是这次dijkstra算法应该返回的答案.
   但是怎样分组才能不遗漏的计算完呢?
   考虑一种神奇的分组方式--二进制分组.将k个点的编号转化成二进制数.如果第i位1则当作源点,否则当作汇点,然后交换两组再来一次dijkstra.这样一定会考虑所有的组合方式.why?两个不同的编号,必然在二进制上有一位不同.这一位的不同将它们分成两组计算,所以每一组都计算到了,取个min就是答案.
   我认为分类方法可能还有很多,不遗漏是关键.这道题如果能想到把dijkstra变成多源的就基本上做完了.
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; typedef long long ll;
const int maxn = ;
const ll inf = 1e17; int T,cas,n,vis[maxn],m,head[maxn],to[maxn],nextt[maxn],w[maxn],tot = ,K,a[maxn],mark[maxn];
ll d[maxn],ans = inf; struct node
{
int x;
ll len;
bool operator < (const node& a) const {
return len > a.len;
}
};
priority_queue<node> q; void add(int x,int y,int z)
{
w[tot] = z;
to[tot] = y;
nextt[tot] = head[x];
head[x] = tot++;
} ll dijkstra()
{
while (!q.empty())
{
node u = q.top();
q.pop();
int x = u.x;
ll len = u.len;
if (mark[x])
return len;
if (vis[x])
continue;
vis[x] = ;
for (int i = head[x];i;i = nextt[i])
{
int v = to[i];
if (d[v] > d[x] + w[i])
{
d[v] = d[x] + w[i];
node temp;
temp.x = v;
temp.len = d[v];
q.push(temp);
}
}
}
return inf;
} void solve()
{
for (int i = ; i < ; i++)
{
while (!q.empty())
q.pop();
memset(vis,,sizeof(vis));
memset(mark,,sizeof(mark));
memset(d,/,sizeof(d));
for (int j = ; j <= K; j++)
{
if (( << i) & a[j])
mark[a[j]] = ;
else
{
node temp;
temp.x = a[j];
temp.len = ;
d[a[j]] = ;
q.push(temp);
}
}
ans = min(ans,dijkstra());
memset(vis,,sizeof(vis));
memset(mark,,sizeof(mark));
memset(d,/,sizeof(d));
while (!q.empty())
q.pop();
for (int j = ; j <= K; j++)
{
if (( << i) & a[j])
{
node temp;
temp.len = ;
temp.x = a[j];
d[a[j]] = ;
q.push(temp);
}
else
mark[a[j]] = ;
}
ans = min(ans,dijkstra());
}
} int main()
{
scanf("%d",&T);
while (T--)
{
ans = inf;
tot = ;
memset(head,,sizeof(head));
scanf("%d%d",&n,&m);
for (int i = ; i <= m; i++)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z);
}
scanf("%d",&K);
for(int i = ; i <= K; i++)
scanf("%d",&a[i]);
solve();
printf("Case #%d: %lld\n",++cas,ans);
} return ;
}

hdu6166 Senior Pan的更多相关文章

  1. 【最短路】【dijkstra】【二进制拆分】hdu6166 Senior Pan

    题意:给你一张带权有向图,问你某个点集中,两两结点之间的最短路的最小值是多少. 其实就是dijkstra,只不过往堆里塞边的时候,要注意塞进去它是从集合中的哪个起始点过来的,然后在更新某个点的答案的时 ...

  2. hdu 6169 Senior PanⅡ Miller_Rabin素数测试+容斥

    Senior PanⅡ Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Pr ...

  3. HDU 6166 Senior Pan (最短路变形)

    题目链接 Problem Description Senior Pan fails in his discrete math exam again. So he asks Master ZKC to ...

  4. HDU - 6166:Senior Pan(顶点集合最短路&二进制分组)

    Senior Pan fails in his discrete math exam again. So he asks Master ZKC to give him graph theory pro ...

  5. HDU 6166.Senior Pan()-最短路(Dijkstra添加超源点、超汇点)+二进制划分集合 (2017 Multi-University Training Contest - Team 9 1006)

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

  6. HDU 6166 Senior Pan 二进制分组 + 迪杰斯特拉算法

    Senior Pan Time Limit: 12000/6000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Probl ...

  7. 2017多校第9场 HDU 6169 Senior PanⅡ 数论,DP,爆搜

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6169 题意:给了区间L,R,求[L,R]区间所有满足其最小质数因子为k的数的和. 解法: 我看了这篇b ...

  8. HDU 6166 Senior Pan(k点中最小两点间距离)题解

    题意:n个点,m条有向边,指定k个点,问你其中最近的两点距离为多少 思路:这题的思路很巧妙,如果我们直接枚举两点做最短路那就要做C(k,2)次.但是我们换个思路,我们把k个点按照二进制每一位的0和1分 ...

  9. hdu 6166 Senior Pan

    http://acm.hdu.edu.cn/showproblem.php?pid=6166 题意: 给出一张无向图,给定k个特殊点 求这k个特殊点两两之间的最短路 二进制分组 枚举一位二进制位 这一 ...

随机推荐

  1. Liunx expect 基础

    a script for study except #!/usr/bin/expect 声明文件内的语法使用 expect 的语法来执行. send send: 向进程发送字符串,用于模拟用户的输入. ...

  2. 【Shell 开发】Shell 目录

    目录 [第一章]Shell 概述 [第二章]Shell 变量 [第三章]Shell 变量的数值计算 [第四章]Shell 条件测试表达式 [shell 练习1]编写Shell条件句练习 [shell ...

  3. 5.airflow问题

    1. Traceback (most recent call last): File "/usr/bin/airflow", line 28, in <module> ...

  4. 原生js和jquey获取窗口宽高,滚动条,鼠标位置总结

    JQuery获取浏览器窗口的可视区域高度和宽度,滚动条高度   alert($(window).height()); //浏览器时下窗口可视区域高度 alert($(document).height( ...

  5. 03慕课网《进击Node.js基础(一)》API-URL网址解析

    url url.parse(url,query,host);解析域名 url必须,地址字符串 query可选 host 可选:在不清楚协议时正确解析 querystring 字符串和对象之间互相解析 ...

  6. 使用JSon实现三级联动

    JSon实现三级联动 我觉得我这个方法比较麻烦,但是目前技术还比较弱,所以先做个笔记自己理解.目前没有和后台交互,只是在前台页面实现了 jQuery和JSon数据实现的,代码如下: <!DOCT ...

  7. SSH 框架的心得

    使用SSH框架做完了一个普通网站的前后台项目,成热写点心得,免得以后再入坑.其中使用 Strust2  2.3.33 + Spring 4.3.9 + Hibernate 5.2.10 eclipse ...

  8. SVN版本合并技巧

    公司使用了bug管理系统,项目添加新功能时,建一个主工单,再分成多个子工单,将子工单分给多个程序员来开发. 开发人员完成一部分就提交一部分,多个小功能模块就分多次提交到测试主干,然后用测试主干项目发布 ...

  9. 老司机带你解读jQuery插件开发流程

    jquery插件开发模式 jquery插件一般有三种开发方式: 通过$.extend()来扩展jQuery 通过$.fn 向jQuery添加新的方法 通过$.widget()应用jQuery UI的部 ...

  10. EF动态排序

    转载的代码,改天再研究 public PageData<T> FindAll(int PageIndex, int PageSize, Expression<Func<T, b ...