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. 帮朋友转发招聘信息 南京知名互联网公司招聘java、测试、前端,具体私聊

    一.java开发 1.5年及以上J2EE方向开发经验 2.精通spring等开源框架 3.熟悉html.javascript.css.jsp/freemarker.jquery的使用 4.熟悉使用my ...

  2. Silverlight:telerik RadControls中RadGridView的一个Bug及解决办法(转载)

    当RadGridView中嵌套RadComboBox,且RadGridView的高度不够出现滚动条时,上下拉动滚动条后,RadComboBox中的选中值将丢失! 如下图: 滚动条未拖动前 滚动条上下拖 ...

  3. Atitit.biz业务系统 面向框架  面向模式---------数据映射imp

    Atitit.biz业务系统 面向框架  面向模式---------数据映射imp 1.1. 面向变量  面向过程  面向对象 面向组件  面向框架  面向服务 面向模式1 1.2. 第2章 架构模式 ...

  4. 阻塞赋值与非阻塞赋值(verilog篇)

    阻塞赋值与非阻塞赋值(verilog篇) 2017-09-30 竹海 相约电子ee 相信刚刚接触verilog的读者,多少对阻塞赋值和非阻塞赋值仍有一些困惑.笔者在这篇文章,带领大家深入的理解这两者的 ...

  5. Shift Register

    /*************************************************** /  Shift Register module /  Programing by seong ...

  6. [ci]持续集成系列

    持续集成一直很蛋疼,感觉没底. 几个方面来写 1,搭建gitlab 配邮箱 域名等使之好用 2,搭建jenkins –yum,安装常见插件 3,搭建sonar,汉化 4,安装sonar-scanner ...

  7. Gradle build.gradle to Maven pom.xml ,终于找到你了。

    尊重原创:https://blog.csdn.net/kevin_luan/article/details/50996109 根据build.gradle 生成maven pox.xml 1.将以下配 ...

  8. C++语言基础(11)-多态

    一.产生背景 先看下面的例子: #include <iostream> using namespace std; //基类People class People{ public: Peop ...

  9. 448. Find All Numbers Disappeared in an Array【easy】

    448. Find All Numbers Disappeared in an Array[easy] Given an array of integers where 1 ≤ a[i] ≤ n (n ...

  10. 李洪强经典面试题47--UNIX常用命令

    可能碰到的iOS笔试面试题(3)--UNIX常用命令 做开发说用不到命令行,那肯定是不可能的.所以记住几个常用的命令还是很有用. cd 改变工作目录 pwd 输出当前工作目录的绝对路径在UNIX中要执 ...