Travel

The country frog lives in has n

towns which are conveniently numbered by 1,2,…,n

.

Among 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≤n≤105,0≤m≤5⋅105,1≤a,b≤109). Each of the following m lines contains 2 integers ui,vi, which denotes cities ui and vi are connected by highway. (1≤ui,vi≤n,ui≠vi

).

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
分两种情况:
1. 1到 n 铁路可以直连 跑一次最短路取 Min(b,dist[n])
2. 1 到n 高速公路连通 那么我们BFS跑最短路 ,每次构一次新图,每个点入队一次.因为是完全图所以每次可以入队的点都非常多.所以可以暴力.
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const LL INF = 1e16;
const LL N = ;
struct Edge{
LL v,next;
LL w;
}edge[*N];
LL head[N];
LL tot,n,m,a,b;
bool vis[N];
LL low[N];
LL MIN ;
void addEdge(LL u,LL v,LL w,LL &k){
edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++;
}
struct Node{
int u;
int step;
};
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void spfa(LL pos){
for(LL i=;i<=n;i++){
low[i] = INF;
vis[i] = false;
}
low[pos] = ;
queue<LL>q;
while(!q.empty()) q.pop();
q.push(pos);
while(!q.empty()){
LL u = q.front();
q.pop();
vis[u] = false;
for(LL k=head[u];k!=-;k=edge[k].next){
LL w = edge[k].w,v = edge[k].v;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
}
bool vis1[N];
int bfs(int pos){
memset(vis,,sizeof(vis));
queue<Node> q;
Node node;
vis[pos] = ;
node.u = pos;
node.step = ;
q.push(node);
while(!q.empty()){
memset(vis1,false,sizeof(vis1));
node = q.front();
int u = node.u;
int step = node.step;
/// if((step+1)*b>a) return -1; TLE
q.pop();
vis1[u] = ;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
vis1[v] = true;
}
Node next;
if(!vis1[n]) return step+;
for(int i=n;i>=;i--){
if(!vis1[i]&&!vis[i]){
if((step+)*b>a) return -;
next.u = i;
next.step = step+;
q.push(next);
vis[i] = true;
}
}
}
return -;
}
int main(){
while(scanf("%lld%lld%lld%lld",&n,&m,&a,&b)!=EOF){
init();
bool flag = false;
for(int i=;i<m;i++){
LL u,v;
scanf("%lld%lld",&u,&v);
addEdge(u,v,a,tot);
addEdge(v,u,a,tot);
if(u==&&v==n||u==n&&v==) flag = ;
}
LL ans = ;
if(!flag){
spfa();
ans = min(low[n],b);
}else{
int step = bfs();
if(step==-) ans = a;
else ans = min(a,step*b);
}
printf("%lld\n",ans);
}
}
 

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

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

    Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, 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. SCU 1095运送物资(最短路)

    SCU 1095运送物资(最短路) X国发生了内战.起义军得到了广大人民的支持.在一次战役中,反动军队结集了大量兵力,围攻起义军的主堡W城.为支援前线,后方各个供给基地城市纷纷准备将物资运往W城.各基 ...

  5. Travel(最短路)

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...

  6. [USACO09JAN]安全出行Safe Travel 最短路,并查集

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

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

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

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

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

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

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

随机推荐

  1. react & monaco editor & vs code

    react & monaco editor & vs code monaco-editor https://microsoft.github.io/monaco-editor/inde ...

  2. developer roadmap

    developer roadmap https://balsamiq.com/ https://balsamiq.com/givingback/free/ https://balsamiq.cloud ...

  3. c#public、private、protected、internal、protected internal

    public 公有访问.不受任何限制.private 私有访问.只限于本类成员访问,子类,实例都不能访问.protected 保护访问.只限于本类和子类访问,实例不能访问.internal 内部访问. ...

  4. 论如何优雅的用bitset来求四维偏序

    四维偏序.. 就是给你一个四维集合.再给你一些询问,请你求出a[i].x1<=ask.x1&&a[i].x2<=ask.x2&&a[i].x3<=as ...

  5. 【bzoj4012】 HNOI2015—开店

    http://www.lydsy.com/JudgeOnline/problem.php?id=4012 (题目链接) 题意 一棵树,每条边有正边权,每个点的点权非负.若干组询问,强制在线,每次查询点 ...

  6. 127. Word Ladder(M)

    127. Word LadderGiven two words (beginWord and endWord), and a dictionary's word list, find the leng ...

  7. Java基础-Java数据类型

    Java基础-Java数据类型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.数据类型的作用 数据类型就是一组值,以及这一组值上的操作,数据类型可以决定数据的存储方式,取值范围 ...

  8. spring JMS在接收消息的时候总是报错

    spring JMS在接收消息的时候总是报错 org.springframework.jms.UncategorizedJmsException: Uncategorized exception oc ...

  9. 何凯文每日一句打卡||DAY6

  10. Javascript摸拟自由落体与上抛运动 说明!

    JavaScript 代码 //**************************************** //名称:Javascript摸拟自由落体与上抛运动! //作者:Gloot //邮箱 ...