HDU 6166 二进制分解 + 最短路

题目链接


题意:
n 个点m条边的有向图,选出其中 k 个点成为一个集合,集合中任意选一对跑最短路(不能是自己到自己),问其中距离的最小值是多少。


思路:
首先一个明显的思路就是想办法转化成多起点多终点的最短路,然后建立一个超级起点和超级终点,跑一次得到答案。

但此题起点集和终点集都来自一个集合,所以只能想办法将集合进行分组。

考虑二进制分解,对于每两个不等的数,至少有一个二进制位不一样,这样我们可以枚举每一个二进制位,然后按0 1 <script type="math/tex" id="MathJax-Element-5">1</script> 分成两组,再跑最短路,这样答案一定不会有遗漏。

代码:

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;

const ll INF = 1e15 + 10;
const int A = 1e5 + 10;

class Edge{  //存边
public:
    int u,v;
    ll w;
}edge[A<<1];
class Gra{   //存图
public:
    int v,next;
    ll w;
}G[A<<1];
ll dis[A];
bool vis[A];
int head[A],p[A],Belong[A];
int tot,n,m,k,Mx;

void add(int u,int v,ll w){
    G[tot].v = v;
    G[tot].w = w;
    G[tot].next = head[u];
    head[u] = tot++;
}

void build_G(){        //建图
    memset(head,-1,sizeof(head));tot = 0;
    for(int i=0 ;i<m ;i++){
        int  u = Belong[edge[i].u],v = Belong[edge[i].v];
        add(u,v,edge[i].w);
    }
}

ll Spfa(ll st,ll ed){  //跑最短路
    queue<ll> que;
    for(int i=0;i<=n+1 ;i++){
        dis[i] = INF;vis[i] = 0;
    }
    que.push(st);
    dis[st] = 0,vis[st] = 1;
    while(que.size()){
        int u = que.front();que.pop();
        vis[u] = 0;
        for(int i=head[u] ;i!=-1 ;i=G[i].next){
            int v = G[i].v;ll w = G[i].w;
            if(w + dis[u] < dis[v]){
                dis[v] = w + dis[u];
                if(!vis[v]){
                    vis[v] = 1;que.push(v);
                }
            }
        }
    }
    return dis[ed];
}

void solve(){
    for(int i=0 ;i<=n+1 ;i++) Belong[i] = i;
    int S = 0,T = n+1;              //建立超级起点和超级终点
    ll ans = INF;
    for(int i=0 ;(1<<i)<=Mx ;i++){
        for(int j=0 ;j<k ;j++){
            if((p[j]>>i)&1) Belong[p[j]] = S;  //分组
            else            Belong[p[j]] = T;
        }
        build_G();
        ans = min(ans,Spfa(S,T));
        ans = min(ans,Spfa(T,S));
    }
    printf("%I64d\n",ans);
}

int main(){
    int T,_=1;scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        for(int i=0 ;i<m ;i++){
            scanf("%d%d%I64d",&edge[i].u,&edge[i].v,&edge[i].w);
        }
        scanf("%d",&k);Mx = 0;
        for(int i=0 ;i<k ;i++){
            scanf("%d",&p[i]);
            Mx = max(Mx,p[i]);
        }
        printf("Case #%d: ",_++);
        solve();
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值