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. mybatis 传递参数的两种方式与模糊匹配 很重要

  2. 一本通1648【例 1】「NOIP2011」计算系数

    1648: [例 1]「NOIP2011」计算系数 时间限制: 1000 ms         内存限制: 524288 KB [题目描述] 给定一个多项式 (ax+by)k ,请求出多项式展开后 x ...

  3. Codeforces - 1020B Badge(找环)

    题意: 以每个点为起点,找到第一个出现两次的点 解析: 我是先找出来所有的环  环上的点找出来的肯定是自己 bz[i]  = i; 然后去遍历不在环上的点j  如果通过这个点找到一个已经标记的的点i ...

  4. linux下转换windows文件格式为unix sed -i 's/\r//' <filename> 转化为unix格式

    sed -i 's/\r//' <filename> 转化为unix格式

  5. HGOI20181030 模拟题解

    problem:给定一个序列,问你能不能通过一次交换把他弄成有序 sol: 对于0%的数据,满足数列是一个排列,然后我就打了这档分(自己瞎造的!) 对于100%的数据,显然我们先对数列进行排序然后上下 ...

  6. HGOI20180904(NOIP2018模拟sxn出题)

    sol 输入n和H表示n个人,选H个人gcd最大抓住排列,是x[1,n]的正整数,是连续的整数,假设现在最大的公因数是k其中k一定是在[1,n]那么在排列中最多出现的个数为w那么kw是最大的含有因数k ...

  7. winform里宿主WCF,并传递winform变量给WCF

    最近客户要求把服务器端程序里的二个功能用service的方式提供出来,方便调用.首先想着单独建一个wcf 服务的项目,但是因为要用到server端程序winform里的变量,因此只能在winform里 ...

  8. 【leetcode】 Validate Binary Search Tree

    Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...

  9. 【CSS】定位层

    html:定位层1.概念: >>.定位层是由html元素(标签)形成的一个特殊的box盒子. >>.其重点在于“定位”,而html元素(标签)的定位方式由CSS来控制. 通常情 ...

  10. Object类型的怎么判断空值

    例如 Object result; 我直接这样是不行的 if(result==null) //这样是错的 ... 要这样判断 if(result == System.DBNull.Value) //这 ...