SCU - 4444 別樣最短路徑-大資料完全圖

life4711發表於2015-10-01

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 2integers 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;
}


相關文章