SCU 4444: Travel(最短路)
Travel
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≤105,0≤m≤5⋅105,1≤a,b≤109). 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. 1到 n 铁路可以直连 跑一次最短路取 Min(b,dist[n])
2. 1 到n 高速公路连通 那么我们BFS跑最短路 ,每次构一次新图,每个点入队一次.因为是完全图所以每次可以入队的点都非常多.所以可以暴力.
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const LL INF = 1e16;
const LL N = ;
struct Edge{
LL v,next;
LL w;
}edge[*N];
LL head[N];
LL tot,n,m,a,b;
bool vis[N];
LL low[N];
LL MIN ;
void addEdge(LL u,LL v,LL w,LL &k){
edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++;
}
struct Node{
int u;
int step;
};
void init(){
memset(head,-,sizeof(head));
tot = ;
}
void spfa(LL pos){
for(LL i=;i<=n;i++){
low[i] = INF;
vis[i] = false;
}
low[pos] = ;
queue<LL>q;
while(!q.empty()) q.pop();
q.push(pos);
while(!q.empty()){
LL u = q.front();
q.pop();
vis[u] = false;
for(LL k=head[u];k!=-;k=edge[k].next){
LL w = edge[k].w,v = edge[k].v;
if(low[v]>low[u]+w){
low[v] = low[u]+w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
}
bool vis1[N];
int bfs(int pos){
memset(vis,,sizeof(vis));
queue<Node> q;
Node node;
vis[pos] = ;
node.u = pos;
node.step = ;
q.push(node);
while(!q.empty()){
memset(vis1,false,sizeof(vis1));
node = q.front();
int u = node.u;
int step = node.step;
/// if((step+1)*b>a) return -1; TLE
q.pop();
vis1[u] = ;
for(int k=head[u];k!=-;k=edge[k].next){
int v = edge[k].v;
vis1[v] = true;
}
Node next;
if(!vis1[n]) return step+;
for(int i=n;i>=;i--){
if(!vis1[i]&&!vis[i]){
if((step+)*b>a) return -;
next.u = i;
next.step = step+;
q.push(next);
vis[i] = true;
}
}
}
return -;
}
int main(){
while(scanf("%lld%lld%lld%lld",&n,&m,&a,&b)!=EOF){
init();
bool flag = false;
for(int i=;i<m;i++){
LL u,v;
scanf("%lld%lld",&u,&v);
addEdge(u,v,a,tot);
addEdge(v,u,a,tot);
if(u==&&v==n||u==n&&v==) flag = ;
}
LL ans = ;
if(!flag){
spfa();
ans = min(low[n],b);
}else{
int step = bfs();
if(step==-) ans = a;
else ans = min(a,step*b);
}
printf("%lld\n",ans);
}
}
SCU 4444: Travel(最短路)的更多相关文章
- SCU 4444 Travel (补图最短路)
Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...
- scu 4444 Travel
题意: 一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路. 思路: 首先判断1和n被哪种边连通. 如果是被a连通,那么就需要全部走b的边到达n,选择最小的 ...
- 第十五届四川省省赛 SCU - 4444 Travel
给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点) p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...
- SCU 1095运送物资(最短路)
SCU 1095运送物资(最短路) X国发生了内战.起义军得到了广大人民的支持.在一次战役中,反动军队结集了大量兵力,围攻起义军的主堡W城.为支援前线,后方各个供给基地城市纷纷准备将物资运往W城.各基 ...
- Travel(最短路)
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n1,2,…,n. Amo ...
- [USACO09JAN]安全出行Safe Travel 最短路,并查集
题目描述 Gremlins have infested the farm. These nasty, ugly fairy-like creatures thwart the cows as each ...
- 【BZOJ1576】[Usaco2009 Jan]安全路经Travel 最短路+并查集
[BZOJ1576][Usaco2009 Jan]安全路经Travel Description Input * 第一行: 两个空格分开的数, N和M * 第2..M+1行: 三个空格分开的数a_i, ...
- BZOJ1576: [Usaco2009 Jan]安全路经Travel(最短路 并查集)
题意 给你一张无向图,保证从1号点到每个点的最短路唯一.对于每个点求出删掉号点到它的最短路上的最后一条边(就是这条路径上与他自己相连的那条边)后1号点到它的最短路的长度 Sol emmm,考场上想了个 ...
- UVa1048 Low Cost Air Travel——最短路
很好的一道题呀 思路 状态\(d(i,j)\)表示已经经过了行程单中的\(i\)个城市,目前在城市\(j\)的最小代价,直接建边跑最短路就行了 比如机票为\(ACBD\),行程单为\(CD\),那么对 ...
随机推荐
- win 批处理
前言 批处理文件(batch file)包含一系列 DOS命令,通常用于自动执行重复性任务.用户只需双击批处理文件便可执行任务,而无需重复输入相同指令.编写批处理文件非常简单,但难点在于确保一切按顺序 ...
- maven使用阿里镜像配置文件
方法一: apache-maven-3.5.2\confsetting.xml,添加如下镜像配置: <mirrors> <mirror> <id>alimaven& ...
- 【设计模式】—— 职责链模式ChainOfResponsibility
前言:[模式总览]——————————by xingoo 模式意图 避免请求的发送者,和接受者过度的耦合在一起.一个请求者只需要发送一个请求即可,它的请求具体由后面哪个对象进行响应,并不需要关心.而请 ...
- BZOJ2442 Usaco2011 Open修剪草坪(动态规划+单调队列)
显然可以dp.显然可以单调队列优化一下. #include<iostream> #include<cstdio> #include<cmath> #include& ...
- Different between Telnet/SSH/FTP
http://www.differencebetween.net/category/technology/protocols-formats/ Telnet vs SSH Secure Shell, ...
- 自学Zabbix10.1 Configuration export/import 配置导入导出
自学Zabbix10.1 Configuration export/import 配置导入导出 通过导入/导出zabbix配置文件,我们可以将自己写好的模板等配置在网络上分享,我们也可以导入网络上分享 ...
- Tomcat如何开启SSL配置(https)
一.创建证书 证书用于客户端与服务端安全认证.我们可以使用JDK自带的keytool工具来生成证书.真正在产品环境中使用肯定要去证书提供商去购买,证书认证一般都是由VeriSign认证,官方地址:ht ...
- Python 进程间的通信
#-*-coding:utf-8-*- '''python提供了多种进程间的通信方式,如:Queue,Pipe,Valie+Array等. Queue与Pipe的区别在于Pipe常用来在两个进程间通信 ...
- opencv ---getRotationMatrix2D函数
getRotationMatrix2D函数 主要用于获得图像绕着 某一点的旋转矩阵 Mat getRotationMatrix2D(Point2f center, double angle, dou ...
- decimal, float 和double
阿里的 Java 手册里写着: 6. [强制] 小数类型为 decimal,禁止使用 float 和 double. 说明:float 和 double 在存储的时候,存在精度损失的问题,很可能在值的 ...