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. LightOJ 1097 - Lucky Number 线段树

    http://www.lightoj.com/volume_showproblem.php?problem=1097 题意:一个自然数序列,先去掉所有偶数项,在此基础上的序列的第二项为3,则删去所有3 ...

  2. 汕头市队赛 SRM19 字符题

    从天上掉下来了个这样的问题: 有一个字符串 从中选出两个子串 A,B,求 A+B可以构成的不同串的个数. 还想知道,这么多个串中字典序最大的那一个. 某人捡到了这个问题,并把它扔给了你. [输入] 一 ...

  3. UITableView---iOS-Apple苹果官方文档翻译

    本系列所有开发文档翻译链接地址:iOS7开发-Apple苹果iPhone开发Xcode官方文档翻译PDF下载地址  //转载请注明出处--本文永久链接:http://www.cnblogs.com/C ...

  4. HDU 1422 重温世界杯 (dp)

    题目链接 Problem Description 世界杯结束了,意大利人连本带利的收回了法国人6年前欠他们的债,捧起了大力神杯,成就了4星意大利. 世界杯虽然结束了,但是这界世界杯给我们还是留下许多值 ...

  5. Spring cloud 实战读书笔记

    基础知识 Spring cloud 版本说明 Brixton.SR5 :Brixton 的第5个Release版本 SRX:service releases 简称SRX版本,X版本号 Spring b ...

  6. python实战===百度文字识别sdk

    http://ai.baidu.com/docs#/OCR-Python-SDK/top

  7. angular数据绑定---js全局学习

    <!DOCTYPE html> <html ng-app> <head> <title>Simple app</title> </he ...

  8. python多进程处理数据

    当我们处理大规模数据如ImageNet的时候,单进程显得很吃力耗时,且不能充分利用多核CPU计算机的资源.因此需要使用多进程对数据进行并行处理,然后将结果合并即可.以下给出的是多进程处理的demo代码 ...

  9. Python爬虫音频数据

    一:前言 本次爬取的是喜马拉雅的热门栏目下全部电台的每个频道的信息和频道中的每个音频数据的各种信息,然后把爬取的数据保存到mongodb以备后续使用.这次数据量在70万左右.音频数据包括音频下载地址, ...

  10. 用vue实现登录页面

    vue和mui一起完成登录页面(在hbuilder编辑器) <!DOCTYPE html> <html> <head> <meta charset=" ...