SCU - 4444 别样最短路径-大数据完全图

http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=184147

Description

Travel

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

Among  n(n1)2  pairs of towns,  m  of them are connected by bidirectional highway, which needs  a  minutes to travel. The other pairs are connected by railway, which needs  b  minutes to travel.

Find the minimum time to travel from town  1  to town  n .

Input

The input consists of multiple tests. For each test:

The first line contains  4  integers  n,m,a,b  ( 2n105,0m5105,1a,b109 ). Each of the following  m  lines contains  2 integers  ui,vi , which denotes cities  ui  and  vi  are connected by highway. ( 1ui,vin,uivi ).

Output

For each test, write  1  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
/**
SCU - 4444 别样最短路径-大数据完全图
题目大意:给定一个完全图,其中有两种边,长度为a(不超过5e5)或长度为b(剩下的),求有1~n的最短路径(数据范围1e5)
解题思路:如果1和n之间连边为a那么答案一定为a和一条最短的全由b组成的路径的较小者,如果1和n之间连边为b,那么答案一定
           为b和一条最短的全由a组成的路径的较小者。对于第1种情况直接跑spfa就可以了,第二种情况由于边数较多,不能直接spfa
           从1开始搜索与其相连的边权为b的边,用set维护一下,由于每个点只入队1次,复杂度还是允许的。这种处理方法还是第一
           次做,感觉很巧妙
*/
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
typedef long long LL;
const int INF=1e9+7;
const int maxn=100100;
int n,m,vis[maxn];
LL a,b,dis[maxn];
set<int>st,ts;
set<int>::iterator it;
int head[maxn],ip;
void init()
{
    memset(head,-1,sizeof(head));
    ip=0;
}
struct note
{
    int v,next;
}edge[maxn*10];
void addedge(int u,int v)
{
    edge[ip].v=v,edge[ip].next=head[u],head[u]=ip++;
}
void spfa()
{
    queue<int>q;
    for(int i=0;i<=n;i++)dis[i]=INF;
    memset(vis,0,sizeof(vis));
    dis[1]=0;
    q.push(1);
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        vis[u]=0;
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(dis[v]>dis[u]+1)
            {
                dis[v]=dis[u]+1;
                if(!vis[v])
                {
                    vis[v]=1;
                    q.push(v);
                }
            }
        }
    }
    printf("%lld\n",min(dis[n]*a,b));
}
void bfs()
{
    dis[n]=INF;
    st.clear(),ts.clear();
    for(int i=2;i<=n;i++)st.insert(i);
    queue<int>q;
    q.push(1);
    dis[1]=0;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i!=-1;i=edge[i].next)
        {
            int v=edge[i].v;
            if(st.count(v)==0)continue;
            st.erase(v),ts.insert(v);
        }
        for(it=st.begin();it!=st.end();it++)
        {
            q.push(*it);
            dis[*it]=dis[u]+1;
        }
        st.swap(ts);
        ts.clear();
    }
    printf("%lld\n",min(dis[n]*b,a));
}
int main()
{
    while(~scanf("%d%d%lld%lld",&n,&m,&a,&b))
    {
        init();
        int flag=0;
        for(int i=0;i<m;i++)
        {
            int u,v;
            scanf("%d%d",&u,&v);
            if(u>v)swap(u,v);
            addedge(u,v);
            addedge(v,u);
            if(u==1&&v==n)flag=1;
        }
        if(flag)bfs();
        else spfa();
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值