SCU 4444 Travel (补图最短路)
Travel
The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n\).
Among \(\frac{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 \leq n \leq 10^5, 0 \leq m \leq 5 \cdot 10^5, 1 \leq a, b \leq 10^9\)). Each of the following \(m\) lines contains \(2\) integers \(u_i, v_i\), which denotes cities \(u_i\) and \(v_i\) are connected by highway. (\(1 \leq u_i, v_i \leq n, u_i \neq v_i\)).
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
【分析】补图最短路。每个点只放进队列一次,set维护。
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define met(a,b) memset(a,b,sizeof a)
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef pair<int,int>pii;
const int N = 1e6+;
int n,m,k;
set<int>s,t;
set<int>::iterator it;
vector<int>edg[N];
int vis[N];
ll dis[N],a,b;
bool ok;
void init(){
s.clear();t.clear();
ok=false;
for(int i=;i<N;i++){
vis[i]=dis[i]=;
edg[i].clear();
}
}
void spfa() {
queue<int>q;
for(int i=;i<N;i++)dis[i]=inf;
dis[]=;
q.push();
vis[]=;
while(!q.empty()) {
int u=q.front();
q.pop();
vis[u]=;
for(int i=;i<edg[u].size();i++) {
int v=edg[u][i];
if(dis[v]>dis[u]+) {
dis[v]=dis[u]+;
if(!vis[v]) {
vis[v]=;
q.push(v);
}
}
}
}
printf("%lld\n",min(dis[n]*a,b));
}
void bfs(){
queue<int>q;
for(int i=;i<=n;i++){
s.insert(i);
}
q.push();
dis[]=;
dis[n]=inf;
while(!q.empty()){
int u=q.front();q.pop();
for(int i=;i<edg[u].size();i++){
int v=edg[u][i];
if(!s.count(v))continue;
s.erase(v);t.insert(v);
}
for(it = s.begin();it!=s.end();it++){
q.push(*it);
dis[*it]=dis[u]+;
}
s.swap(t);
t.clear();
}
printf("%lld\n",min(dis[n]*b,a));
}
int main(){
int u,v;
while(~scanf("%d%d%lld%lld",&n,&m,&a,&b)){
init();
while(m--){
scanf("%d%d",&u,&v);
edg[u].pb(v);
edg[v].pb(u);
if(u>v)swap(u,v);
if(u==&&v==n)ok=true;
}
if(ok)bfs();
else spfa();
}
return ;
}
SCU 4444 Travel (补图最短路)的更多相关文章
- SCU 4444: Travel(最短路)
Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ...
- scu 4444 Travel
题意: 一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路. 思路: 首先判断1和n被哪种边连通. 如果是被a连通,那么就需要全部走b的边到达n,选择最小的 ...
- 第十五届四川省省赛 SCU - 4444 Travel
给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点) p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...
- HDU 5876 Sparse Graph 【补图最短路 BFS】(2016 ACM/ICPC Asia Regional Dalian Online)
Sparse Graph Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)To ...
- hdu-5786(补图最短路)
题意:给你n个点,m条无向边,问你这n个点构成的完全图,不用那m条边,由一个s出现的单源最短路 解题思路:首先,暴力建图不行,点太多,那么我们就按照它的规则来,把m条边建好,但是建的这个图表示不走的方 ...
- 2019.01.22 SCU4444 Travel(最短路+bfs)
传送门 题意简述:给出一张nnn个点的完全图,有mmm条边边权为aaa其余点边权为bbb,问从111到nnn的最短路. 思路:分类讨论一波即可. (1,n)(1,n)(1,n)的边权为aaa,那么只用 ...
- hdu5876 Sparse Graph(补图最短路 bfs)
题目链接:hdu5876 Sparse Graph 详见代码.. #include<cstdio> #include<cstring> #include<algorith ...
- HDU 2433 Travel (最短路,BFS,变形)
题意: 给出一个图的所有边,每次从图中删除一条边,求任意点对的路径总和(求完了就将边给补回去).(有重边) 思路: #include <bits/stdc++.h> using names ...
- hdu 5876 Sparse Graph icpc大连站网络赛 1009 补图最短路
BFS+链表 代码改自某博客 #include<stdio.h> #include<iostream> #include<algorithm> #include&l ...
随机推荐
- 【C++对象模型】第四章 Function 语意学
1.Member的各种调用方式 1.1 Nonstatic Member Functions 实际上编译器是将member function被内化为nonmember的形式,经过下面转化步骤: 1.给 ...
- 从无到有搭建SSM框架
框架 https://www.cnblogs.com/xiaoL/p/7753130.html log4j配置详解 https://www.cnblogs.com/SummerinShire ...
- 【TYVJ】P1038 忠诚
[算法]线段树 #include<cstdio> #include<algorithm> using namespace std; ]; ,inf=0x3f3f3f3f; in ...
- 深入分析Spring 与 Spring MVC容器(山东数漫江湖)
1 Spring MVC WEB配置 Spring Framework本身没有Web功能,Spring MVC使用WebApplicationContext类扩展ApplicationContext, ...
- idea编写的java代码,在cmd运行乱码解决方案
1.解决方案 使用txt打开,另存为的时候选择编码为ANSI 即可.
- C++获取系统时间的方法
//首先是了解这个结构体,_SYSTEMTIME ,然后通过系统函数GetLocalTime往这个结构体的变量中写入当前系统时间typedef struct _SYSTEMTIME { WORD wY ...
- 关于RecylerView:1.在ScrollView的RecylerView滑动事件的处理。2.item之间的距离 小数取整
1.在ScrollView的RecylerView滑动事件的处理. 在布局文件中在RecylerView外包裹一层相对布局 2.RecylerView item之间的距离 (1)编写SpaceItem ...
- HTTPS加密通信原理及数字证书系统
https加密通信原理: 公钥私钥成对,公钥公之于众,私钥只有自己知道. 用公钥加密的信息只能由与之相对应的私钥解密. 甲给乙发送数据时,甲先用乙的公钥加密这段数据,再用自己的私钥对这段数据的特征数据 ...
- python自动开发之第二十四天(Django)
一.ModelForm操作及验证 1.class Meta:class Meta: #注意以下字段不能加逗号 model = models.UserInfo #这里的all代指所用的字段,也可以是一个 ...
- TCP之Nagle算法&&延迟ACK
1. Nagle算法: 是为了减少广域网的小分组数目,从而减小网络拥塞的出现: 该算法要求一个tcp连接上最多只能有一个未被确认的未完成的小分组,在该分组ack到达之前不能发送其他的小分组,tcp需要 ...