Codeforces Round #124 (Div. 1) E. Opening Portals

本文介绍了一种解决游戏地图中快速开启所有传送门问题的算法。通过使用Dijkstra算法和最小生成树方法,实现玩家从起始城市出发,经过所有安装有传送门的城市所需的最短时间计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

E. Opening Portals
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Pavel plays a famous computer game. A player is responsible for a whole country and he can travel there freely, complete quests and earn experience.

This country has n cities connected by m bidirectional roads of different lengths so that it is possible to get from any city to any other one. There are portals in k of these cities. At the beginning of the game all portals are closed. When a player visits a portal city, the portal opens. Strange as it is, one can teleport from an open portal to an open one. The teleportation takes no time and that enables the player to travel quickly between rather remote regions of the country.

At the beginning of the game Pavel is in city number 1. He wants to open all portals as quickly as possible. How much time will he need for that?

Input
The first line contains two space-separated integers n and m (1 ≤ n ≤ 105, 0 ≤ m ≤ 105) that show how many cities and roads are in the game.

Each of the next m lines contains the description of a road as three space-separated integers xi, yi, wi (1 ≤ xi, yi ≤ n, xi ≠ yi, 1 ≤ wi ≤ 109) — the numbers of the cities connected by the i-th road and the time needed to go from one city to the other one by this road. Any two cities are connected by no more than one road. It is guaranteed that we can get from any city to any other one, moving along the roads of the country.

The next line contains integer k (1 ≤ k ≤ n) — the number of portals.

The next line contains k space-separated integers p1, p2, …, pk — numbers of the cities with installed portals. Each city has no more than one portal.

Output
Print a single number — the minimum time a player needs to open all portals.

Please, do not use the %lld specifier to read or write 64-bit integers in C++. It is preferred to use the cin, cout streams or the %I64d specifier.

Examples
input
3 3
1 2 1
1 3 1
2 3 1
3
1 2 3
output
2
input
4 3
1 2 1
2 3 5
2 4 10
3
2 3 4
output
16
input
4 3
1 2 1000000000
2 3 1000000000
3 4 1000000000
4
1 2 3 4
output
3000000000
Note
In the second sample the player has to come to city 2, open a portal there, then go to city 3, open a portal there, teleport back to city 2 and finally finish the journey in city 4.

题意:给出n个点的图,有k个关键点,问从1开始经过所有关键点的最短路,经过了的关键点之前可以连一条权值为0的边,
做法,以k个关键点跑dij,用d数组记录每个点到离他最近的关键点的距离,用p数组记录每个点离他最近的关键点是谁。
对于一个边,把它的两个顶点的p值(p数组)连起来的代价就是两个顶点的d值加上这个边的权值,题目可以转化在这k个点上跑一次最小生成树,然后以每条边的两个顶点的d值和加上边权值为标准排序,跑最小生成树。


#include<bits/stdc++.h>
#define pii pair<long long,int>
using namespace std;
const int N = 1e5+100;
int n,m,k;
long long d[N];
int p[N];
int fa[N];
bool vis[N];
int Find(int x){return x == fa[x] ? x : fa[x] = Find(fa[x]); }
struct edge{
    int v;
    long long c;
};
vector<edge> G[N];
void add_edge(int u,int v,int c){
    G[u].push_back((edge){v,c});
    G[v].push_back((edge){u,c});
}
struct line{
    int u,v,c;
};
vector<line> vec;
bool cmp(line a,line b){
    return d[a.u]+d[a.v]+a.c < d[b.u]+d[b.v]+b.c;
}
void dij(){
    memset(d,0x3f,sizeof(d));
    priority_queue<pii,vector<pii>,greater<pii> > que;
    for(int i= 1;i <= n;i ++){
        if(vis[i]) {
            que.push({0,i});
            d[i] = 0;
            p[i] = i;
        }
    }
    while(!que.empty()){
        pii now = que.top();
        que.pop();
        long long c = now.first;
        int u = now.second;
        if(c > d[u]) continue;
        for(int i = 0;i < G[u].size();i ++){
            edge e = G[u][i];
            if(d[e.v] > c + e.c){
                p[e.v] = p[u];
                d[e.v] = c+e.c;
                que.push({d[e.v],e.v});
            }
        }
    }
}


int main(){
    scanf("%d %d",&n,&m);
    for(int i = 1;i<= m;i ++){
        int a,b,c;
        scanf("%d %d %d",&a,&b,&c);
        add_edge(a,b,c);
        vec.push_back((line){a,b,c});
    }
    scanf("%d",&k);
    memset(vis,false,sizeof(vis));
    for(int i= 1;i <= k;i ++){
        int now;
        scanf("%d",&now);
        vis[now] = true;
    }
    dij();
    memset(vis,false,sizeof(vis));
    long long ans= d[1];
    sort(vec.begin(),vec.end(),cmp);
    for(int i = 1;i <= n;i ++) fa[i] = i;
    for(int i = 0;i < vec.size();i ++){
        line &now= vec[i];
        int aa = Find(p[now.v]);
        int bb = Find(p[now.u]);
        if(aa != bb){
            fa[aa] = bb;
            ans += now.c+d[now.v]+d[now.u];
        }
    }
    cout << ans << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值