Travel

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

Among \(\frac{n(n - 1)}{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\) (\(2 \leq n \leq 10^5, 0 \leq m \leq 5 \cdot 10^5, 1 \leq a, b \leq 10^9\)). Each of the following \(m\) lines contains \(2\) integers \(u_i, v_i\), which denotes cities \(u_i\) and \(v_i\) are connected by highway. (\(1 \leq u_i, v_i \leq n, u_i \neq v_i\)).

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
【分析】补图最短路。每个点只放进队列一次,set维护。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define met(a,b) memset(a,b,sizeof a)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 1e6+;
int n,m,k;
set<int>s,t;
set<int>::iterator it;
vector<int>edg[N];
int vis[N];
ll dis[N],a,b;
bool ok;
void init(){
s.clear();t.clear();
ok=false;
for(int i=;i<N;i++){
vis[i]=dis[i]=;
edg[i].clear();
}
}
void spfa() {
queue<int>q;
for(int i=;i<N;i++)dis[i]=inf;
dis[]=;
q.push();
vis[]=;
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=;
for(int i=;i<edg[u].size();i++) {
int v=edg[u][i];
if(dis[v]>dis[u]+) {
dis[v]=dis[u]+;
if(!vis[v]) {
vis[v]=;
q.push(v);
}
}
}
}
printf("%lld\n",min(dis[n]*a,b));
}
void bfs(){
queue<int>q;
for(int i=;i<=n;i++){
s.insert(i);
}
q.push();
dis[]=;
dis[n]=inf;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(!s.count(v))continue;
s.erase(v);t.insert(v);
}
for(it = s.begin();it!=s.end();it++){
q.push(*it);
dis[*it]=dis[u]+;
}
s.swap(t);
t.clear();
}
printf("%lld\n",min(dis[n]*b,a));
}
int main(){
int u,v;
while(~scanf("%d%d%lld%lld",&n,&m,&a,&b)){
init();
while(m--){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
if(u>v)swap(u,v);
if(u==&&v==n)ok=true;
}
if(ok)bfs();
else spfa();
}
return ;
}

SCU 4444 Travel (补图最短路)的更多相关文章

  1. SCU 4444: Travel(最短路)

    Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ...

  2. scu 4444 Travel

    题意: 一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路. 思路: 首先判断1和n被哪种边连通. 如果是被a连通,那么就需要全部走b的边到达n,选择最小的 ...

  3. 第十五届四川省省赛 SCU - 4444 Travel

    给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点)  p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...

  4. HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)

    Sparse Graph Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)To ...

  5. hdu-5786(补图最短路)

    题意:给你n个点,m条无向边,问你这n个点构成的完全图,不用那m条边,由一个s出现的单源最短路 解题思路:首先,暴力建图不行,点太多,那么我们就按照它的规则来,把m条边建好,但是建的这个图表示不走的方 ...

  6. 2019.01.22 SCU4444 Travel(最短路+bfs)

    传送门 题意简述:给出一张nnn个点的完全图,有mmm条边边权为aaa其余点边权为bbb,问从111到nnn的最短路. 思路:分类讨论一波即可. (1,n)(1,n)(1,n)的边权为aaa,那么只用 ...

  7. hdu5876 Sparse Graph(补图最短路 bfs)

    题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...

  8. HDU 2433 Travel (最短路,BFS,变形)

    题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...

  9. hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路

    BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...

随机推荐

  1. 数学:BSGS

    先来稍微回顾一下,我们已经会求模线性方程(包括其特殊情况乘法逆元) 我们还会进行幂取模的快速算法(模是质数用费马小定理,模一般情况用欧拉定理) 对于幂中指数特别大的情况,我们还延伸出了拓展欧拉定理来解 ...

  2. [洛谷P3304] [SDOI2013]直径

    洛谷题目链接:[SDOI2013]直径 题目描述 小Q最近学习了一些图论知识.根据课本,有如下定义.树:无回路且连通的无向图,每条边都有正整数的权值来表示其长度.如果一棵树有N个节点,可以证明其有且仅 ...

  3. 【BZOJ】3779 重组病毒

    [算法]Link-Cut Tree+线段树(维护DFS序) [题解]整整三天……T_T 这篇题解比较资瓷:permui 这道题虽然树形态没有变化,但用lct写的原因在于把题目中的操作一进行了神转化:每 ...

  4. Machine Learning(CF940F+带修改莫队)

    题目链接:http://codeforces.com/problemset/problem/940/F 题目: 题意:求次数的mex,mex的含义为某个集合(如{1,2,4,5})第一个为出现的非负数 ...

  5. Java线程(一)

    1. java什么叫线程安全?什么叫不安全? 就是线程同步的意思,就是当一个程序对一个线程安全的方法或者语句进行访问的时候,其他的不能再对他进行操作了,必须等到这次访问结束以后才能对这个线程安全的方法 ...

  6. [Leetcode Week12]Unique Paths II

    Unique Paths II 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/unique-paths-ii/description/ Descrip ...

  7. C语言restrict限定符

    restrict是c99标准引入的,它只可以用于限定和约束指针,并表明指针是访问一个数据对象的唯一且初始的方式.即它告诉编译器,所有修改该指针所指向内存中内容的操作都必须通过该指针来修改,而不能通过其 ...

  8. 64_g4

    gnatcoll-2014-10.fc26.x86_64.rpm 28-Feb-2017 17:44 1738266 gnatcoll-devel-2014-10.fc26.i686.rpm 28-F ...

  9. Python的数值和字符串

    Python数据类型 1.数值 --类型:  1/整型 2/长整型   3/浮点型 -- 0.0, 12.0, -18.8, 3e+7等 4/复数型 -- complex  In []: 0x34al ...

  10. sql server中字符串无法替换空格的问题

    直接上代码: select case when 'workReport'=LTRIM(RTRIM(' workReport ')) then 'trim去空格成功' when 'workReport' ...