Travel set优化

本文探讨了在一个由n个城镇组成的国家中,通过高速公路和铁路连接,寻找从城镇1到城镇n的最短时间的问题。文章介绍了使用宽度优先搜索(BFS)和SPFA算法来解决这个问题的方法,并详细解释了四种不同情况下最短路径的计算过程。
摘要由CSDN通过智能技术生成

Travel
The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n.

Among n(n−1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected by railway, which needs bb minutes to travel.

Find the minimum time to travel from town 11 to town nn.

Input
The input consists of multiple tests. For each test:

The first line contains 44 integers n,m,a,bn,m,a,b (2≤n≤105,0≤m≤5⋅105,1≤a,b≤1092≤n≤105,0≤m≤5⋅105,1≤a,b≤109). Each of the following mm lines contains 22 integers ui,viui,vi, which denotes cities uiui and vivi are connected by highway. (1≤ui,vi≤n,ui≠vi1≤ui,vi≤n,ui≠vi).

Output
For each test, write 11 integer which denotes the minimum time.

Sample Input
3 2 1 3
1 2
2 3
3 2 2 3
1 2
2 3
Sample Output
2
3

这道题应该用宽搜做,刚开始没考虑到还得求全为边长为b的最短距离,所以用的spfa求的全为边长为a的最短距离,然后一直错,最后找到问题所在,发现找其他n * (n -1) / 2 - m边比较多,没想到好的方法来解决,用spfa必超时,所以用宽搜解决,第一个也可以用宽搜(如果刚开始一直用宽搜的话就好了),因为宽搜找到终点,最远只需走n - 1 次,然后下来补题用set实现求补找边,发现会爆内存;网上参考了一下,因为走过的点的就不会走了,所以可以用set那样写,很巧妙,详见代码。
总共分四种情况:
1>a <= b && distance[1][n] = a,直接输出a;
2>a <= b && distance[1][n] = b;比较spfa(边长全为a的图)求出的最短距离与b取最小值,输出;
3>a > b && distance[1][n] = a;宽搜(边长全为b的图)求出的最短距离与a取最小值,输出;
4>a > b && distance[1][n] = b;直接输出b;

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<set>
using namespace std;
typedef long long LL;
const LL inf = 0x7fffffff;
#define mp make_pair
#define pb push_back
#define fi first
#define se second
const int MAXN = 1e5 + 5;

int n,m,num;
LL a,b;
vector<int>ve[MAXN];
bool vis[MAXN];
LL dis[MAXN];
set<int>ss,st;

void spfa()
{
    for(int i = 1;i <= n;++i)
        dis[i] = inf;
    //cout << dis[1] << endl;
    memset(vis,false,sizeof(vis));
    dis[1] = 0;
    vis[1] = true;
    queue<int>que;
    que.push(1);
    while(!que.empty()){
        int p = que.front();
        que.pop();
        vis[p] = false;
        int len = ve[p].size();
        for(int i = 0;i < len;++i){
            int v = ve[p][i];
            if(dis[v] > dis[p] + a){
                dis[v] = dis[p] + a;
                if(!vis[v]){
                    vis[v] = true;
                    que.push(v);
                }
            }
        }
    }
}

//这里没有用vis标记,因为set集合里面的点访问过就被删除了,所以不用vis标记
LL bfs()
{
    dis[n] = inf;
    ss.clear(),st.clear();
    for(int i = 2;i <= n;++i)
        ss.insert(i);
    queue<int>que;
    que.push(1);
    dis[1] = 0;
    while(!que.empty())
    {
        int u = que.front();
        que.pop();
        int len = ve[u].size();
        for(int i = 0;i < len;++i){
            int v = ve[u][i];
            if(ss.count(v) == 0)
                continue;
            ss.erase(v);
            st.insert(v);
        }
        for(set<int>::iterator it = ss.begin();it != ss.end();++it){
            dis[*it] = dis[u] + 1;
            que.push(*it);
        }
        ss.swap(st);
        st.clear();
    }
    return dis[n];
}

int main()
{
    while(~scanf("%d %d %lld %lld",&n,&m,&a,&b)){
        for(int i = 1;i <= n;++i)
            ve[i].clear();
        LL ans = -1;
        for(int i = 0;i < m;++i){
            int x,y;
            scanf("%d %d",&x,&y);
            if((x == 1 && y == n) || (x == n && y == 1)){
                ans = a;
            }
            ve[x].pb(y);ve[y].pb(x);
        }
        if(a <= b && ans != -1){
            printf("%lld\n",a);
        }
        if(a <= b && ans == -1){
            ans = b;
            spfa();
            ans = min(ans,dis[n]);
            printf("%lld\n",ans);
        }
        if(a > b && ans != -1){
            LL x = bfs();
            ans = min(ans,x * b);
            printf("%lld\n",ans);
        }
        if(a > b && ans == -1){
            printf("%lld\n",b);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值