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 22integers 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

//题意: n , m, a, b ,其中 n 代表点数,并且是个完全图,m 是权值为 a 的边数,其余的边权值为 b ,问 1--n 的最短路

题解:如果 1 和 n 之间连边为 a 那么答案一定为 a 与一条最短的全由b组成的路径的较小者,如果 1 和 n 之间连边为b,那么答案一定

为b和一条最短的全由a组成的路径的较小者。对于第1种情况直接bfs就可以,第二种情况由于边数较多,不能直接bfs

从1开始搜索与其相连的边权为b的边,用set维护一下,由于每个点只入队1次,复杂度算是 nlogn ,叉姐的题很有意思

300ms

 # include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
# define MX
/**************************/
struct Edge
{
int v,nex;
}edge[MX*]; int n,m,a,b,ip;
int hlist[MX];
LL dis[MX];
bool vis[MX];
void addedge(int u,int v)
{
edge[ip]= (Edge){v,hlist[u]};
hlist[u]=ip++;
edge[ip]= (Edge){u,hlist[v]};
hlist[v]=ip++;
} void bfsB() // 1-n 连b边
{
dis[n]=INF;
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push();
dis[]=;
vis[]=;
while (!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i=hlist[u];i!=-;i=edge[i].nex)
{
int v = edge[i].v;
if (!vis[v])
{
dis[v]=dis[u]+;
Q.push(v);
vis[v]=;
}
}
if (dis[n]!=INF) break;
}
printf("%lld\n",min(dis[n]*a,(LL)b));
} void bfsA() //1-n 连 a 边
{
dis[n]=INF;
set<int> st,ts;
for (int i=;i<=n;i++) st.insert(i);
set<int>::iterator it;
queue<int> Q;
Q.push();
dis[]=;
while (!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i=hlist[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if (st.count(v)==) continue;
st.erase(v); ts.insert(v);
}
for (it=st.begin();it!=st.end();it++)
{
dis[*it] = dis[u]+;
Q.push(*it);
}
if (dis[n]!=INF) break;
st.swap(ts);
ts.clear();
}
printf("%lld\n",min(dis[n]*b,(LL)a));
} int main()
{
while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF)
{
memset(hlist,-,sizeof(hlist));
ip=;
bool flag=;
for (int i=;i<m;i++)
{
int u = scan();
int v = scan();
addedge(u,v);
if (u>v) swap(u,v);
if (u==&&v==n) flag=;
}
if (flag)
{
if (a<b) printf("%d\n",a);
else bfsA();
}
else
{
if (b<a) printf("%d\n",b);
else bfsB();
}
}
return ;
}

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. [USACO09JAN]安全出行Safe Travel 最短路,并查集

    题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each ...

  3. 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集

    [BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...

  4. BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)

    题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...

  5. UVa1048 Low Cost Air Travel——最短路

    很好的一道题呀 思路 状态\(d(i,j)\)表示已经经过了行程单中的\(i\)个城市,目前在城市\(j\)的最小代价,直接建边跑最短路就行了 比如机票为\(ACBD\),行程单为\(CD\),那么对 ...

  6. 【UVA10816】Travel in Desert (最小瓶颈路+最短路)

    UVA10816 Travel in Desert 题目大意 沙漠中有一些道路,每个道路有一个温度和距离,要求s,t两点间的一条路径,满足温度最大值最小,并且长度最短 输入格式 输入包含多组数据. 每 ...

  7. SCU 4444 Travel (补图最短路)

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

  8. PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS

    PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...

  9. HDU2433—Travel (BFS,最短路)

    Travel Time Limit: 10000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

随机推荐

  1. React Router V4发布

    React Router V4 正式版发布,该版本相较于前面三个版本有根本性变化,遵循 Just Component 的 API 设计理念. 本次升级的主要变更有: 声明式 Declarative 可 ...

  2. 倍福TwinCAT(贝福Beckhoff)应用教程12.3 TwinCAT控制松下伺服 NC进阶

    在前面一节,我们简单介绍了通过PLC+HMI实现完整控制松下伺服的上使能-运动,采集位置,速度等功能,这里我们会大量简化用到的贝福功能块(为了更加实用).首先依然是对单个轴的封装,我们之前的做法,例如 ...

  3. 安装Python的机器学习包Sklearn 出错解决方法

    1 首先须要安装Cython.网上下载后进行本地安装 python setup.py install 2 下载Sklearn包,https://pypi.python.org/pypi/scikit- ...

  4. 【BIEE】14_开发流程介绍

    以上是BIEE开发的流程图,通过流程图我们可以看出在BIEE中存在以下主要内容: 仪表盘 仪表盘页 分析 仪表盘提示 主题区域 Catalog RPD 以下是一些文件以及资料库存储路径 资料库存储路径 ...

  5. springboot学习(五) 全局异常处理

    创建全局异常处理 /** * 全局异常配置管理 */ @ControllerAdvice public class GlobalExceptionConfig extends ResponseEnti ...

  6. sklearn-GBDT 调参

    1. scikit-learn GBDT类库概述 在sacikit-learn中,GradientBoostingClassifier为GBDT的分类类, 而GradientBoostingRegre ...

  7. Redis之intset数据结构

    0.前言 redis中intset是一个整数集合, 只能存储整数类型的数据, 可以是16位, 32位, 或者是64位, 是以升序排列的数组进行保存数据,下面会介绍具体数据结构和对其操作过程. 1.数据 ...

  8. DMA摘记

    1.特点 PIO模式下硬盘和内存之间的数据传输是由CPU来控制的:而在DMA模式下,CPU只须向DMA控制器下达指令,让DMA控制器来处理数据的传送,数据传送完毕再把信息反馈给CPU,这样就很大程度上 ...

  9. 521. Longest Uncommon Subsequence I【easy】

    521. Longest Uncommon Subsequence I[easy] Given a group of two strings, you need to find the longest ...

  10. unity free asset

    Unity Test Tools https://www.assetstore.unity3d.com/#/content/13802 Sample Assets (beta) https://www ...