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. MySQL的order by时区分大小写

    Mysql 查询区分大小写 mysql查询默认是不区分大小写的 如: select * from some_table where str=‘abc'; select * from some_tabl ...

  2. Java之字节流操作-复制文件

    package test_demo.fileoper; import java.io.FileInputStream; import java.io.FileOutputStream; import ...

  3. MT【104】高斯函数找周期

    分析:$t(n)=n-[\frac{n}{2}]-[\frac{n}{3}]-[\frac{n}{6}]$的周期为6,故 $\sum\limits_{n=1}^{2014}(n-t(n))=\sum\ ...

  4. 【题解】 [HEOI2016]排序题解 (二分答案,线段树)

    题目描述 在2016年,佳媛姐姐喜欢上了数字序列.因而他经常研究关于序列的一些奇奇怪怪的问题,现在他在研究一个难题,需要你来帮助他.这个难题是这样子的:给出一个1到n的全排列,现在对这个全排列序列进行 ...

  5. 超实用Image类

    using System; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Runt ...

  6. Vitrualbox 桥接网卡界面名称未指定、Filters currently installed on the system have reached the limit、不能为虚拟电脑 打开一个新任务

    1. 桥接网卡界面名称未指定 http://wenku.baidu.com/link?url=VFG0hknsDX3VPXQoX5f-g1wUX_LBl-lOj0ZqD222kM31iVCPJKVu3 ...

  7. bzoj3612 平衡 (dp)

    设f[i][j]为把i拆成j个不重复的.大于0小于等于N的数的方案数 我们考虑一个方案是怎么来的:(初始状态是f[0][0]=1) 如果这个方案里有1,那它是先把原来的状态的每个数加1.然后再增加一个 ...

  8. Luogu 1613 跑路(最短路径,倍增)

    Luogu 1613 跑路(最短路径,倍增) Description 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是 ...

  9. python+正态分布+蒙特卡洛预测男女身高概率!

    sklearn实战-乳腺癌细胞数据挖掘 https://study.163.com/course/introduction.htm?courseId=1005269003&utm_campai ...

  10. Linux命令(三)远程登录