四川第七届 I Travel(bfs)
Travel
The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n.
Among n(n−1)2n(n−1)2 pairs of towns, mm of them are connected by bidirectional highway, which needs aa minutes to travel. The other pairs are connected by railway, which needs bb minutes to travel.
Find the minimum time to travel from town 11 to town nn.
Input
The input consists of multiple tests. For each test:
The first line contains 44 integers n,m,a,bn,m,a,b (2≤n≤105,0≤m≤5⋅105,1≤a,b≤1092≤n≤105,0≤m≤5⋅105,1≤a,b≤109). Each of the following mmlines contains 22 integers ui,viui,vi, which denotes cities uiui and vivi are connected by highway. (1≤ui,vi≤n,ui≠vi1≤ui,vi≤n,ui≠vi).
Output
For each test, write 11 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
题意:有n个城市,编号为1~n,每个城市都相互连通,其中有m对城市通过公路连通,其他的城市通过铁路连通,经过公路的时间为a,
经过铁路的时间为b,问从1到达n的时间最短为多少.
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
#include<queue>
#include<vector>
#define ll long long
using namespace std;
int n,m;
ll a,b;
vector<int>v[];
struct node
{
int v;
ll t;
};
queue<node>q;
bool bo[];
ll min1(ll a,ll b)
{
if(a>b)return b;
return a;
}
int main()
{
while(~scanf("%d %d %lld %lld",&n,&m,&a,&b))
{
while(!q.empty()) q.pop();
for(ll i=;i<;i++) v[i].clear();
memset(bo,,sizeof(bo));
bool bb=;
for(ll i=;i<=m;i++)
{
int x,y;
scanf("%d %d",&x,&y);
v[x].push_back(y);
v[y].push_back(x);
if((x==&&y==n)||(x==n&&y==))bb=;//可能高速比公路慢
}
if(a>=b)
{//挑出与n没有高速公路的点中存不存在与1也没有高速公路的点
if(bb==)printf("%d\n",b);
else
{
for(int i=;i<n;i++)
{
bool is=;
for(int j=;j<v[i].size();j++)
{
if(v[i][j]==||v[i][j]==n)is=;
}
if(is==)
{
bb=;break;
}
}
if(bb==)printf("%d\n",min(a,*b));
else printf("%d\n",a);
}
continue;
}
node s;
s.v=;
s.t=;
q.push(s);
ll mx=b;
bo[]=;
bool f=;
while(!q.empty())
{
node s;
s=q.front();
q.pop();
for(int i=;i<v[s.v].size();i++)
{
int y=v[s.v].at(i);
if(bo[y]) continue;
if(y==n)
{
mx=min1(mx,(s.t+)*a);
f=;
break;
}
node ss;
ss.t=s.t+;
ss.v=y;
q.push(ss);
}
if(f)break;
}
printf("%lld\n",mx);
}
return ;
}
四川第七届 I Travel(bfs)的更多相关文章
- 四川第七届 D Vertex Cover(二分图最小点覆盖,二分匹配模板)
Vertex Cover frog has a graph with nn vertices v(1),v(2),…,v(n)v(1),v(2),…,v(n) and mm edges (v(a1), ...
- 四川第七届 E Rectangle
Rectangle frog has a piece of paper divided into nn rows and mm columns. Today, she would like to dr ...
- 四川第七届 C Censor (字符串哈希)
Censor frog is now a editor to censor so-called sensitive words (敏感词). She has a long text pp. Her j ...
- 哈尔滨理工大学第七届程序设计竞赛初赛(BFS多队列顺序)
哈尔滨理工大学第七届程序设计竞赛初赛https://www.nowcoder.com/acm/contest/28#question D题wa了半天....(真真正正的半天) 其实D题本来就是一个简单 ...
- 2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告
2016 第七届蓝桥杯 c/c++ B组省赛真题及解题报告 勘误1:第6题第4个 if最后一个条件粗心写错了,答案应为1580. 条件应为abs(a[3]-a[7])!=1,宝宝心理苦啊.!感谢zzh ...
- 山东省第七届ACM省赛------Memory Leak
Memory Leak Time Limit: 2000MS Memory limit: 131072K 题目描述 Memory Leak is a well-known kind of bug in ...
- 山东省第七届ACM省赛------Reversed Words
Reversed Words Time Limit: 2000MS Memory limit: 131072K 题目描述 Some aliens are learning English. They ...
- 山东省第七届ACM省赛------Triple Nim
Triple Nim Time Limit: 2000MS Memory limit: 65536K 题目描述 Alice and Bob are always playing all kinds o ...
- 山东省第七届ACM省赛------The Binding of Isaac
The Binding of Isaac Time Limit: 2000MS Memory limit: 65536K 题目描述 Ok, now I will introduce this game ...
随机推荐
- Python 集合set概念和操作
# 集合 # 概念 # 无序的, 不可随机访问的, 不可重复的元素集合 # 与数学中集合的概念类似,可对其进行交.并.差.补等逻辑运算 # 分为可变集合和非可变集合 # set # 为可变集合 # 增 ...
- 【bzoj1232】[Usaco2008Nov]安慰奶牛cheer(最小生成树)
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=1232 这道题要保留的道路肯定是原图的一棵生成树,因为要保留n-1条边,且使删边后的图连 ...
- mac下安装py第三方库到python3下
python3 -m pip install **** 中间可能碰到超时问题 python3 pip --default-timeout=100 install -U **** 设置默认超时时间即可 ...
- linux基础(4)-常用命令
常用命令ls ls #查看当前目录下的文件和目录 ls -l #显示详细信息 ls -a #显示所有文件 ls -t #按修改时间排序 ls -S #按文件大小排序 常用命令pwd pwd #显示 ...
- rpm卸载软件error preun
这两天,使用ipvsadm -ln总是显示空. 后来,使用strace ipvsadm -ln定位 看来,是ipvsadm模块有问题,卸载了再重新安装吧,结果出现这种问题. 从来没遇到这种问题: er ...
- Codeforces Round #368 (Div. 2) A , B , C
A. Brain's Photos time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- HTTP的长连接和短连接(转)
一.什么是长连接 HTTP1.1规定了默认保持长连接(HTTP persistent connection ,也有翻译为持久连接),数据传输完成了保持TCP连接不断开(不发RST包.不四次握手),等待 ...
- iOS中,Framework和.a的打包及使用
最近在做一个小项目,需要给客户一个demo测试,有一部分核心代码暂时不想让客户知道,就想到了打包成framework或.a库.库有两种: 静态库:.a和.framework 动态库:.tbd和.fra ...
- 偶尔出现button不能点击的情况
原因: 1.没有加点击事件 2.从别的项目或者别的场景导入的预制体,要更换canvas才能点击,不然没反应
- 分布式事务_01_2PC框架raincat快速体验
一.前言 关于2PC的理论知识请见:分布式_理论_03_2PC 这一节我们来看下github上一个优秀的2PC分布式事务开源框架的快速体验. 二.源码 源码请见: https://github.com ...