hdu 6166 Senior Pan(多源最短路径)(二进制划分集合)

Senior Pan

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个点进行分类,若第i位为1则分到s1集合中,否则分到集合s2中,同理反着在再来一次(有向图嘛)

这样就保证了枚举所有的情况(假设点s到点t的距离最短,点s肯定不等于点t,也就是说点s和点t在二进制上至少有一位不同)

代码:

#include<bits/stdc++.h>
using namespace std;

typedef long long LL;
const LL inf=0x3f3f3f3f3f3f3f3f;
const int maxn=1e5+10;
struct node
{
    int u,v;
    LL w;
} p[maxn];
struct edge
{
    int v,next;
    LL w;
} E[maxn<<1];

int first[maxn],num[maxn];
LL dis[maxn];
bool inq[maxn];
int n,m,k,len;
LL Min_ans;

void init()
{
    len=0;
    memset(first,-1,sizeof(first));
    memset(dis,inf,sizeof(dis));
    memset(inq,false,sizeof(inq));
}

void add_edge(int u,int v,LL w)
{
    E[len].v=v,E[len].w=w,E[len].next=first[u],first[u]=len++;
}

void Build_edge()
{
    init();
    for(int j=0; j<m; ++j)
        add_edge(p[j].u,p[j].v,p[j].w);
}

LL spfa(int st,int ed)
{
    queue<int>q;
    q.push(st),inq[st]=true,dis[st]=0;
    while(!q.empty())
    {
        st=q.front();
        inq[st]=false,q.pop();
        for(int i=first[st]; ~i; i=E[i].next)
        {
            int v=E[i].v;
            LL w=E[i].w;
            if(dis[v]>dis[st]+w)
            {
                dis[v]=dis[st]+w;
                if(!inq[v])
                {
                    inq[v]=true;
                    q.push(v);
                }
            }
        }
    }
    return dis[ed];
}

int main()
{
    int t,Case=0;
    scanf("%d",&t);
    while(++Case<=t)
    {
        Min_ans=inf;
        scanf("%d%d",&n,&m);
        for(int i=0; i<m; ++i)
            scanf("%d%d%lld",&p[i].u,&p[i].v,&p[i].w);
        scanf("%d",&k);
        for(int i=1; i<=k; ++i)
            scanf("%d",&num[i]);
        int res=log(n*1.0)/log(2.0);
        int cnt=res==(int)res?res:res+1;
        for(int i=0; i<=res; ++i)
        {
            int tot=1<<i;
            Build_edge();
            for(int j=1; j<=k; ++j)
            {
                if(num[j]&tot)
                    add_edge(0,num[j],0);
                else
                    add_edge(num[j],n+1,0);
            }
            Min_ans=min(Min_ans,spfa(0,n+1));
            Build_edge();
            for(int j=1; j<=k; ++j)
            {
                if(num[j]&tot)
                    add_edge(num[j],n+1,0);
                else
                    add_edge(0,num[j],0);
            }
            Min_ans=min(Min_ans,spfa(0,n+1));
        }
        printf("Case #%d: %lld\n",Case,Min_ans);
    }
    return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值