Codeforces 124 E

Source: Internet
Author: User

A good question about the minimal spanning tree!

We need to construct a graph that contains only k vertices. However, if a brute-force graph is used, it will inevitably time out. Therefore, only the edges in the MST can be added, and the minimum spanning tree is obtained at the end.

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
IAndP[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 portalXTo Portal
Y. Note that there exists a path with the same length such
P[I] Changes only once through it. Indeed,
P[X] Bytes = bytesX,
P[Y] Bytes = bytesY, I. e.P[I] Changed on the path. If it happens on edge
,
P[I] Bytes = bytesX, A path will not be longer than the path from
XToY.

AsP[I] Bytes = bytesXAnd
P[I] Bytes = bytesY, We can see that the length of this path will be
D[I] Accept + acceptW(I, Bytes,J) Accept + acceptD[J], Where
W(I, Bytes,J) Is the weight of Edge
(I, Bytes,J). Kruskal's algo will add this value to the answer and merge Portals
XAndY. The shortest-path trees of vertexes
XAndYWill also be merged.

Note, that almost nothing changed. The next edge for Kruskal's algo can be found in a similar way-
. If this edge connects
XAndYAgain, 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], RoleP[J]) Of Weight
D[I] Accept + acceptW(I, Bytes,J) Accept + acceptD[J] For every edge
(I, Bytes,J) Of Weight
W(I, Bytes,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 100100using 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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.