最小產生樹的好題!
需要構造只包含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 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;}