HDU - 6166(102/600)点集之间最短路

本文介绍了一种解决点集间最短路径问题的方法,通过分组将节点分为两组并分别计算每组内节点到另一组节点的最短距离,最终找到所有可能组合中的最小距离。

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

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,yixi,yi representing an edge, and vivi representing its length.1≤xi,yixi,yi≤n,1≤vivi≤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 aiai, the nodes that Master Dong selects out.1≤aiai≤n,aiai!=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

这个题我是走歪了…
这个分组方式是真的服气

然后就是竟然有点集对点集最短路的板子…
服了服了

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<functional>
#include<vector>
#include<cstring>
#include<map>
#include<cmath>
#include<stdio.h>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
using namespace std;
const int inf = 1e9 + 7;
const int N = 100000 + 5;//顶点数
vector<pll>tu[N];
vector<int>kk;
ll daon[N];
void Dijkstra(int n, const vector<int>&s)//点集到点集的最短路板子
{
    fill(daon, daon + n + 1, 1e18);
    priority_queue<pll, vector<pll>, greater<pll> >que;
    for (int x : s) {
        daon[x] = 0;
        que.push({ daon[x],x });
    }
    while (!que.empty()) {
        int u = que.top().second;
        que.pop();
        for (const pll&e : tu[u]) {
            ll v = e.first, w = e.second;
            if (daon[v]>daon[u] + w) {
                daon[v] = daon[u] + w;
                que.push({ daon[v],v });
            }
        }
    }
}
vector<int>s, e;
int main()
{
    int T,u=0;
    cin >> T;
    while(T--)
    {
        int n, m;
        cin >> n >> m;
        for (int a = 1; a <= n; a++)tu[a].clear();
        for (int a = 1; a <= m; a++)
        {
            int q, w, e;
            scanf("%d%d%d", &q, &w, &e);
            tu[q].push_back({ w,e });
        }
        int k;
        cin >> k;
        kk.clear();
        for(int a=1;a<=k;a++)
        {
            int x;
            scanf("%d", &x);
            kk.push_back(x);
        }
        ll dan = 1e18;
        for (int a=1;a<=20;a++) 
        {
            s.clear();
            e.clear();
            for (int j : kk) 
            {
                if (j & 1<<(a-1)) s.push_back(j);
                else e.push_back(j);
            }
            Dijkstra(n, s);//从这开始
            for (auto x : e)dan = min(dan, daon[x]);
            Dijkstra(n, e);
            for (auto x : s)dan = min(dan, daon[x]);//到这里都是求点集之间最短路的
        }
        printf("Case #%d: %lld\n", ++u, dan);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值