2015弱校联盟(1) - I. Travel
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的更多相关文章
- 2015弱校联盟(2) - J. Usoperanto
J. Usoperanto Time Limit: 8000ms Memory Limit: 256000KB Usoperanto is an artificial spoken language ...
- 2015弱校联盟(1) - C. Censor
C. Censor Time Limit: 2000ms Memory Limit: 65536KB frog is now a editor to censor so-called sensitiv ...
- 2015弱校联盟(1) - B. Carries
B. Carries Time Limit: 1000ms Memory Limit: 65536KB frog has n integers a1,a2,-,an, and she wants to ...
- 2015弱校联盟(1) -J. Right turn
J. Right turn Time Limit: 1000ms Memory Limit: 65536KB frog is trapped in a maze. The maze is infini ...
- 2015弱校联盟(1) -A. Easy Math
A. Easy Math Time Limit: 2000ms Memory Limit: 65536KB Given n integers a1,a2,-,an, check if the sum ...
- 2015弱校联盟(1) - E. Rectangle
E. Rectangle Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class name ...
- 2016弱校联盟十一专场10.2---Around the World(深搜+组合数、逆元)
题目链接 https://acm.bnu.edu.cn/v3/problem_show.php?pid=52305 problem description In ICPCCamp, there ar ...
- (2016弱校联盟十一专场10.3) D Parentheses
题目链接 把左括号看成A右括号看成B,推一下就行了.好久之前写的,推到最后发现是一个有规律的序列. #include <bits/stdc++.h> using namespace std ...
- (2016弱校联盟十一专场10.3) B.Help the Princess!
题目链接 宽搜一下就行. #include <iostream> #include<cstdio> #include<cstring> #include<qu ...
随机推荐
- Oracle RAC的日志体系
Oracle Clusterware 不像数据库那样,有丰富的视图.工具可以用来辅助诊断,它的日志和trace文件是唯一的选择.但不像Oracle只有alert日志和几种trace文件,Oracle ...
- xshell的快捷键(非常实用)
删除 ctrl + d 删除光标所在位置上的字符相当于VIM里x或者dl ctrl + h 删除光标所在位置前的字符相当于VIM里hx或者dh ctrl + k 删除光标 ...
- EDI - Biztalk Setting
1. Applications:
- C++ 画星号图形——空心正方形(核心代码)
//输出m行m列的空心正方矩形. int m;//m用来控制行列数. cout<<"请输入要打印的行列数n(n>=4):\n"; cin>>m; ;i ...
- unity assert server 与 cache server
Asset server 其实就是unity提供的版本控制工具,不过我们都转到P4V了,上午尝试了一下,如果小团队使用还是不错的,使用过程大致如下,具体的还是要大伙去官网看喽 服务器安装文件下载: h ...
- embedded tomcat context.xml
在网络下载相关的embedded tomcat jar.也可直接在maven中检索. 在main方法中,输入以下代码: //新建tomcat实例 Tomcat tomcat = new Tomcat( ...
- EntityFramework 实体拆分与表拆分
摘录自https://msdn.microsoft.com/zh-cn/data/jj591617 * 将实体类型的 CLR 属性映射到数据库中的多个表(实体拆分) 实体拆分允许一个实体类型的属性分散 ...
- zepto源码--核心方法6(显示隐藏)--学习笔记
在不引入zepto插件模块fx_metho其他ds的情况下,zepto默认的显示隐藏的函数只有show, hide, toggle,这里解释有个前提条件,就是没有引入zepto的fx_methods插 ...
- DreamweaverCS6
1.设置文字的格式:<font face=""></font> 字号<font size=""> 颜色<font c ...
- NET 框架基本原理透析⑵
生成.打包.部署及管理应用程序与类型 要生成就离不开程,序集,程序集是包含一个或多个类型定义文件和资源文件的集合.在程序集包含的所有文件中,有一个文件用于保存清单.清单是另外一组元数据表的集合,其中主 ...