HDU 6166 Senior Pan (2017多校9 - Dijkstra + 二进制枚举 [神题])

本文介绍了一种利用Dijkstra算法解决图论中选定节点间的最短路径问题的独特方法。通过巧妙地将选定节点划分为两组,并分别计算这两组间最短路径的方法,有效地找到了所有选定节点间最短距离的最小值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:

给你一个有向图, 告诉你选择的K个点, 问这K个点中最近点对 的最短路是多少?

思路:

大开脑洞一个题目。。。

假设我们能够把K个点分成两个集合的话, 我们一遍Dijkstra 就可以求出两个集合的最近距离。

求完这两个集合后, 那么两个集合中除了最近点对外, 其余点对都不用在算了, 因为都不如这个最近点对最优。

因此 如和合理的划分集合是关键。

因为每一个点的数值都是不同的, 因此任意两个点, 二进制中至少有一位是不同的, 如果按照二进制是1还是0来划分的话, 那么就可以保证

任意两个点 都可以划分到不同集合中, 这样, 任意两个点都可以被计算到。(这里好好理解一下还是挺好想的)

10w以内的数 二进制20位就够了。

因此我们可以20次集合划分, 20次dijkstra 即可。


真的大开脑洞。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;

const int maxn = 100000 + 10;

vector<pair<int,int> >g[maxn];
int a[maxn];
vector<int>one, zero;

int vis[maxn];
long long d[maxn];

struct node{
    int u, d;
    bool operator < (const node& rhs) const {
        return d > rhs.d;
    }
    node(int u = 0,int d = 0):u(u), d(d){}
};
priority_queue<node>q;
int goal[maxn];
long long bfs(vector<int>& one){
    while(!q.empty()) q.pop();
    memset(vis,0,sizeof vis);
    memset(d,-1,sizeof d);
    for (int i = 0; i < one.size(); ++i){
        int v = one[i];
        d[v] = 0;
        q.push(node(v, 0));
    }

    while(!q.empty()){
        node nod = q.top(); q.pop();
        int u = nod.u;
        if (goal[u]) return nod.d;
        if (vis[u]) continue;
        vis[u] = 1;

        for (int i = 0; i < g[u].size(); ++i){
            int v = g[u][i].first;
            int w = g[u][i].second;
            if (!vis[v] && (d[v] == -1 || d[v] > d[u] + w)){
                d[v] = d[u] + w;
                q.push(node(v, d[v]));
            }
        }
    }
    return (long long)2333333333333;
}




int main(){
    int T, ks = 0;
    scanf("%d",&T);
    while(T--){
        int n, m;
        scanf("%d %d",&n, &m);
        for (int i = 1; i <= n; ++i){
            g[i].clear();
        }
        for (int i = 0; i < m; ++i){
            int x, y, w;
            scanf("%d %d %d",&x, &y, &w);
            g[x].push_back(make_pair(y, w));
        }

        int K;
        scanf("%d", &K);
        for (int i = 0; i < K; ++i){
            scanf("%d", &a[i]);
        }

        long long ans = (long long)1e18;
        for (int i = 0; i < 20; ++i){
            one.clear();
            zero.clear();
            for (int j = 0; j < K; ++j){
                goal[a[j] ] = 0;
            }
            for (int j = 0; j < K; ++j){
                int v = a[j];
                if (v & (1<<i)){
                    one.push_back(v);
                }
                else {
                    zero.push_back(v);
                    goal[v] = 1;
                }
            }
            ans = min(ans, bfs(one));

            for (int j = 0; j < K; ++j){
                goal[a[j] ] = 0;
            }

            for (int i = 0; i < one.size(); ++i){
                goal[one[i] ] = 1;

            }
            ans = min(ans, bfs(zero));



        }
        printf("Case #%d: %I64d\n", ++ks, ans);
    }
    return 0;
}





Senior Pan

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


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   6170  6169  6168  6167  6166 
 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值