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 ...
随机推荐
- centos安装php
1.安装phpyum install php php-devel重启apache使php生效 2.此时可以在目录:/var/www/html/下建立一个PHP文件代码:<?php phpinfo ...
- html 抽奖代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- A trip through the Graphics Pipeline 2011_05
After the last post about texture samplers, we’re now back in the 3D frontend. We’re done with verte ...
- iostat监控磁盘io
1.安装#yum install sysstat 2.启动#/etc/init.d/sysstat start 3.自启动#checkfig sysstat 4.基本使用#iostat -k 2每两秒 ...
- 任务中使用wget,不保存文件
*/20 * * * * wget --output-document=/dev/null http://www.domain.com 使用wget每过20分钟访问一次,不保存访问文件内容
- 【C51】74HC573芯片
74HC573是一个8位3态带锁存高速的逻辑芯片.下面介绍使用. 参数(仅供参考) Vcc 2~6V I in +-20mA I out +- 35mA 引脚图和引脚作用 ...
- windows下的socket网络编程(入门级)
windows下的socket网络编程 clinet.c 客户端 server.c 服务器端 UDP通信的实现 代码如下 已经很久没有在windows下编程了,这次因为需要做一个跨平台的网络程序,就先 ...
- net-snmp源码VS2013编译添加加密支持(OpenSSL)(在VS里配置编译OpenSSL)
net-snmp源码VS2013编译添加加密支持(OpenSSL) snmp v3 协议使用了基于用户的安全模型,具有认证和加密两个模块. 认证使用的算法是一般的消息摘要算法,例如MD5/SHA等.这 ...
- 12月14日《奥威Power-BI销售计划填报》腾讯课堂开课啦
2016年的最后一个月也过半了,新的一年就要到来,你是否做好了启程的准备?新的一年,有计划,有目标,有方向,才不至于迷茫.规划你的2017,新的一年,遇见更好的自己! 所以 ...
- broadcom代码中httpd进程启动流程介绍
Broadcom代码中包含WEB配置管理媒介, 在嵌入式WEB服务器min_httpd基础上改造实现, 其bin名称为httpd,此httpd可以由管理进程有连接后动态启动,并且当一段时间内没有连接到 ...