Codeforces 124 E

最小生成树的好题!

需要构造只包含k个点的图,但是如果暴力去搞必然超时,所以只加入有可能成为MST中的边的边,最后求一遍最小生成树

First of all, we can note that if each graph vertex is portal, the answer will be a sum of all edges' weights in MST (minimal spanning tree). We can find MST by using Kruskal's algo.

In this problem, not an every vertex is portal. Let's fix this.

Start with a precalculation. Run Dijkstra's algo from all the portals, simultaneously. We will get d[i] — a distance between vertex i and p[i] — the nearest portal to vertex i.

Let's trace Kruskal's algo on a graph of portals. On the first iteration, it will choose the edge with the minimal cost, i.e. a shortest path between all the portals in the original graph.

Let the path leads from portal x to portal y. Note that there exists a path with the same length such as p[i] changes only once through it. Indeed, p[x] = x, p[y] = y, i.e. p[i] changed on the path. If it happens on edge , p[i] = x, a path will not be longer than the path from x to y.

As p[i] = x and p[i] = y, we can see that the length of this path will be d[i] + w(i, j) + d[j], where w(i, j) is the weight of edge (i, j). Kruskal's algo will add this value to the answer and merge portals x and y. The shortest-path trees of vertexes x and y will also be merged.

Note, that almost nothing changed. The next edge for Kruskal's algo can be find in a similar way — . If this edge connects x and y again, DSU makes us not to count this edge, otherwise this edge connects a different pair of edges and will be counted in an answer.

We can easily implement this. Just create a new graph of portals, with an edge (p[i], p[j]) of weight d[i] + w(i, j) + d[j] for every edge (i, j) of weight w(i, j) from original graph and run Kruskal's algo.

Finally, note that if the starting vertex is not a portal, we shall add d[1] to the answer.


#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define N 100100
using namespace std;
int n,m;
int head[N],cnt;
bool ok[N],vis[N];
struct Edge{
    int u,v,next;
    long long w;
}edge[N*2];
long long dis[N];
int pre[N],fa[N];
queue<int>q;
void addedge(int u,int v,long long w){
    edge[cnt].u=u;
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
    edge[cnt].u=v;
    edge[cnt].v=u;
    edge[cnt].w=w;
    edge[cnt].next=head[v];
    head[v]=cnt++;
}
void init(){
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(ok,0,sizeof(ok));
    for(int i=1;i<=n;i++)
        fa[i]=i;
    cnt=0;
}
void SPFA(){
    int i,j;
    for(i=1;i<=n;i++)
        if(ok[i]){
            dis[i]=0;
            pre[i]=i;
            vis[i]=1;
            q.push(i);
        }
        else
            dis[i]=-1;
    while(!q.empty()){
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(i=head[u];i!=-1;i=edge[i].next){
            int v=edge[i].v;
            if(dis[v]==-1 || dis[v]>dis[u]+edge[i].w){
                dis[v]=dis[u]+edge[i].w;
                pre[v]=pre[u];
                if(!vis[v]){
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
}
bool cmp(struct Edge a,struct Edge b){
    return a.w<b.w;
}
int find(int u){
    if(fa[u]==u)return u;
    return fa[u]=find(fa[u]);
}
int main(){
    int i,k;
    int u,v,w,tem;
    scanf("%d %d",&n,&m);
    init();
    for(i=1;i<=m;i++){
        scanf("%d %d %d",&u,&v,&w);
        addedge(u,v,(long long)w);
    }
    scanf("%d",&k);
    for(i=1;i<=k;i++){
        scanf("%d",&tem);
        ok[tem]=1;
    }
    SPFA();
    for(i=0;i<m*2;i++){
        edge[i].w+=(dis[edge[i].u]+dis[edge[i].v]);
        edge[i].u=pre[edge[i].u];
        edge[i].v=pre[edge[i].v];
    }
    sort(edge,edge+m*2,cmp);
    long long sum=0;
    for(i=0;i<m*2;i++){
        u=find(edge[i].u);
        v=find(edge[i].v);
        if(u!=v){
            fa[u]=v;
            sum+=edge[i].w;
        }
    }
    printf("%lld\n",sum+dis[1]);
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值