【多校训练】hdu 6166 Senior Pan 最短路径 Dijkstra

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

题意:

一个有向图,选其中k个点,求两点间距离的最小值。


思路:

我们只要求出任意点对的最小值即可,但是暴力做肯定会超时。把k个点分成两个集合,用dijkstra求两个集合间的最小值==求两个集合间的所有点对的最小值。

由于任意两点的二进制不同,我们可以枚举二进制的位,将k分成两个集合。只需要logn次dijkstra就能得到所有点对间的最小值。


//
//  main.cpp
//  1006
//
//  Created by zc on 2017/8/23.
//  Copyright © 2017年 zc. All rights reserved.
//

#include <iostream>
#include<cstdio>
#include<queue>
#include<cstring>
#include<cmath>
#include<algorithm>
#define ll long long
using namespace std;
const int M=440000;
const int N=110000;
const ll INF=0x3f3f3f3f3f3f3f3f;
int n,m,cnt,head[N],k,a[N],v[N];
ll d[N];
struct node
{
    int next,v;
    ll c;
}e[M];

struct node2
{
    int u;
    ll c;
    friend bool operator< (node2 a ,node2 b)
    {
        return a.c>b.c;
    }
};

priority_queue<node2> q;

void add(int x,int y,ll c)
{
    e[cnt].c=c;
    e[cnt].v=y;
    e[cnt].next=head[x];
    head[x]=cnt++;
}

void init()
{
    memset(v,0,sizeof(v));
    memset(d,0x3f,sizeof(d));
    while(!q.empty())   q.pop();
}

ll dijkstra()
{
    while(!q.empty())
    {
        node2 t=q.top();
        q.pop();
        int u=t.u;
        ll c=t.c;
        if(v[u])    return c;
        for(int i=head[u];i+1;i=e[i].next)
        {
            int y=e[i].v;
            if(d[y]>d[u]+e[i].c)
            {
                d[y]=c+e[i].c;
                q.push(node2{y,d[y]});
            }
        }
    }
    return INF;
}

ll solve()
{
    ll ans=INF;
    for(int i=0;i<20;i++)
    {
        init();
        for(int j=0;j<k;j++)
        {
            if(a[j]&(1<<i))
            {
                q.push(node2{a[j],0});
                d[a[j]]=0;
            }
            else
            {
                v[a[j]]=1;
            }
        }
        ans=min(ans,dijkstra());
    }
    return ans;
}

int main(int argc, const char * argv[]) {
    int T,kase=0;
    scanf("%d",&T);
    while(T--)
    {
        cnt=0;
        memset(head,-1,sizeof(head));
        scanf("%d%d",&n,&m);
        for(int i=0;i<m;i++)
        {
            int x,y;
            ll c;
            scanf("%d%d%lld",&x,&y,&c);
            add(x,y,c);
        }
        scanf("%d",&k);
        for(int i=0;i<k;i++)    scanf("%d",&a[i]);
        printf("Case #%d: %lld\n",++kase,solve());
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值