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. rman恢复误删除的一张表(不完全恢复)

    恢复误删除的一张表可以使用很多方法,如日志挖掘.闪回等,rman恢复(不完全恢复)肯定不是最好的,也不建议用, 现在我们只是演示一下这种恢复. 1 RMAN备份数据库 2创建测试表 3查看此时的SCN ...

  2. c# select标签绑定枚举,并以Description做Text显示

    今天在做项目时遇到一个问题: 开发中有些字段是枚举类型如 Dept 企业表中可能有个字段 Property 性质 0:事业单位,1:私企,2:外企,但有时我们不会单独为性质这个字段定义一张表, 而是在 ...

  3. Python 文件编码(文件乱码)

    IndentationError: unindent does not match any outer indentation level 文件未对齐,在记事本打开. 乱码原因:源码文件的编码格式为u ...

  4. IOS网络第二天 - 03-JSON显示数据,调用本地视频播放,数据转模型

    ********HMVideosViewController.m #import "HMVideosViewController.h" #import "MBProgre ...

  5. .htaccess 重写去index.php

    .htaccess 重写去index.php <IfModule mod_rewrite.c>RewriteEngine onRewriteCond %{REQUEST_FILENAME} ...

  6. OpenGL问题拾遗

    1 OpenGL设置好纹理以后显示不出来,显示为黑色 纹理默认会使用 mipmap .如果没有修改filter选项,或没有指定其他level的mipmap数据,就会显示不出来

  7. LoadRunner11.00入门教程出现的问题

    问题1.打不开浏览器 解决办法:打开浏览器工具--Internet 选项--高级--取消启用第三方浏览器扩展. 顺带解决了,有两个浏览器问题. 两个浏览器:一个是自带的IE,一个是其他软件插件. 解决 ...

  8. 利用Sonar规则结合WebStorm进行Code Inspect

    1.目的 在编写代码时会受到公司Sonar规则的限制,不想在编写完成后再对代码进行Inspect,回头再来一个个修正,费时费力. 那么,下面将通过优秀的WebStorm开发工具自身的CodeInspe ...

  9. JVM性能监控与故障处理命令行工具

    JDK命令行工具 Sun公司作为”礼物“赠送给JDK使用者的工具: 这些命令行工具大多是jdk/lib/tools.jar类库的一层薄包装,主要功能代码是在tools类库(不属于java的标准API) ...

  10. linux之php

    /usr/local/php/sbin/php-fpm 却无法启动,提示错误: ERROR: failed to load configuration file '/usr/local/php/etc ...