I. Travel

Time Limit: 3000ms

Memory Limit: 65536KB

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≤10^5,0≤m≤5*10^5,1≤a,b≤10^9). 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,n);

(1)如果之间是铁路,则需要判断公路是不是更快

(2)如果是公路,则需要判断铁路是不是更快

分别bfs一次;

#include <bits/stdc++.h>
#define LL long long
#define fread() freopen("in.in","r",stdin)
#define fwrite() freopen("out.out","w",stdout) using namespace std; const int INF = 0x3f3f3f3f; const int Max = 1e5+100; typedef struct node
{
int x;
int num;
} Node; int n,m; LL A,B; LL Dist[Max]; vector<int>Pn[Max]; bool vis[Max];
bool visb[Max];
void init()
{
for(int i=1; i<=n; i++)
{
Pn[i].clear();
vis[i]=false;
} } void bfsa()//公路
{
queue<int>Q;
int b;
vis[1]=true;
Dist[n]=INF;
Dist[1]=0;
Q.push(1);
while(!Q.empty())
{
b=Q.front();
Q.pop();
int ans = Pn[b].size();
for(int i=0; i<ans; i++)
{
if(!vis[Pn[b][i]])
{ if(Dist[b]+A<=B)
{
Dist[Pn[b][i]]=Dist[b]+A;
vis[Pn[b][i]]=true;
Q.push(Pn[b][i]);
}
else
{
return ;
}
if(Pn[b][i]==n)
{
return ;
}
}
}
}
}
void bfsb()//铁路
{
queue<int>Q;
int b;
Dist[n]=INF;
Dist[1]=0;
vis[1]=true;
Q.push(1);
while(!Q.empty())
{
b=Q.front();
Q.pop();
for(int i=1; i<=n; i++)
{
visb[i]=false;
}
int ans = Pn[b].size();
for(int i=0; i<ans; i++)
{
visb[Pn[b][i]]=true;
}
for(int i=1; i<=n; i++)
{
if(!visb[i]&&!vis[i])
{
if(Dist[b]+B<=A)
{
vis[i]=true;
Dist[i]=Dist[b]+B;
Q.push(i);
}
else
{
return ;
}
if(i==n)
{
return ;
}
}
}
}
} int main()
{
int u,v;
int Dis;
while(~scanf("%d %d %lld %lld",&n,&m,&A,&B))
{
init();
Dis=-1;
for(int i=1; i<=m; i++)
{
scanf("%d %d",&u,&v);
if((u==1&&v==n)||(u==n&&v==1))
{
Dis=A;
}
Pn[u].push_back(v);
Pn[v].push_back(u);
}
if(Dis==-1)
{
bfsa();
printf("%lld\n",min(B,Dist[n]));
}
else
{
bfsb();
printf("%lld\n",min(A,Dist[n]));
}
}
return 0;
}

2015弱校联盟(1) - I. Travel的更多相关文章

  1. 2015弱校联盟(2) - J. Usoperanto

    J. Usoperanto Time Limit: 8000ms Memory Limit: 256000KB Usoperanto is an artificial spoken language ...

  2. 2015弱校联盟(1) - C. Censor

    C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...

  3. 2015弱校联盟(1) - B. Carries

    B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...

  4. 2015弱校联盟(1) -J. Right turn

    J. Right turn Time Limit: 1000ms Memory Limit: 65536KB frog is trapped in a maze. The maze is infini ...

  5. 2015弱校联盟(1) -A. Easy Math

    A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...

  6. 2015弱校联盟(1) - E. Rectangle

    E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...

  7. 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)

    题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem  description In ICPCCamp, there ar ...

  8. (2016弱校联盟十一专场10.3) D Parentheses

    题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...

  9. (2016弱校联盟十一专场10.3) B.Help the Princess!

    题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...

随机推荐

  1. powerdesigner的基本使用转载

    http://www.blogjava.net/wangdetian168/archive/2011/04/07/347847.html

  2. 在HTML文件的表单中添加{%csrf_token%}便可以解决问题

    原因是django为了在用户提交表单时防止跨站攻击所做的保护 只需在HTML文件的表单中添加{%csrf_token%}便可以解决问题 ------------------------if判断{% i ...

  3. WxInput模块则比较彻底的解决了这个问题

    基于wxpython的GUI输入对话框2 在程序输入中,有时会要求同时改变多个参数值,而且类型也不尽相同, 这时TextEntryDialog就显得不适用了.WxInput模块则比较彻底的解决了这个问 ...

  4. Python多行注释

    由于Python的注释只有针对于单行的注释(用#),多行注释时很不方便(要想多行注释只能每行代码前面都加上#).在网上看到一个Python的多行注释方法,分享给大家,其实很简单的,就是使用一对三个双引 ...

  5. java json与对象或者集合互转

    package open_exe; public class User { private int id; private String name; private String gender; pu ...

  6. js串讲回顾

    注:1.xx.nextSibling.css.xxx->xx的下一个元素的css样式;2. window.opener.document.getElementById("cms&quo ...

  7. 佛祖保佑 永无BUG 永不修改

    // // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...

  8. UIView添加手势

    _shareImage.userInteractionEnabled=YES; UITapGestureRecognizer *imagegesture=[[UITapGestureRecognize ...

  9. ROW_NUMBER

    16:23 2015/4/16 删除重复数据,连续被两位同事问到完全相同的重复记录如何删除只保留一条 create table del_samerecords (id )) go insert int ...

  10. Silverlight TreeView 动态绑定Xml 文件

      随着应用程序的不断升级,客户的需求不断增多,程序员不得不对自己的应用程序做出相应的修改,如果修改的内容较多,那么就必须找出一种简便方法,下面就为大家介绍一下在SilverLight 中左边导航栏T ...