Travel(最短路)
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 mm lines contains 22integers 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 , m, a, b ,其中 n 代表点数,并且是个完全图,m 是权值为 a 的边数,其余的边权值为 b ,问 1--n 的最短路
题解:如果 1 和 n 之间连边为 a 那么答案一定为 a 与一条最短的全由b组成的路径的较小者,如果 1 和 n 之间连边为b,那么答案一定
为b和一条最短的全由a组成的路径的较小者。对于第1种情况直接bfs就可以,第二种情况由于边数较多,不能直接bfs
从1开始搜索与其相连的边权为b的边,用set维护一下,由于每个点只入队1次,复杂度算是 nlogn ,叉姐的题很有意思
300ms
# include <cstdio>
# include <cstring>
# include <cstdlib>
# include <iostream>
# include <vector>
# include <queue>
# include <stack>
# include <map>
# include <bitset>
# include <sstream>
# include <set>
# include <cmath>
# include <algorithm>
# pragma comment(linker,"/STACK:102400000,102400000")
using namespace std;
# define LL long long
# define pr pair
# define mkp make_pair
# define lowbit(x) ((x)&(-x))
# define PI acos(-1.0)
# define INF 0x3f3f3f3f3f3f3f3f
# define eps 1e-
# define MOD inline int scan() {
int x=,f=; char ch=getchar();
while(ch<''||ch>''){if(ch=='-') f=-; ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-''; ch=getchar();}
return x*f;
}
inline void Out(int a) {
if(a<) {putchar('-'); a=-a;}
if(a>=) Out(a/);
putchar(a%+'');
}
# define MX
/**************************/
struct Edge
{
int v,nex;
}edge[MX*]; int n,m,a,b,ip;
int hlist[MX];
LL dis[MX];
bool vis[MX];
void addedge(int u,int v)
{
edge[ip]= (Edge){v,hlist[u]};
hlist[u]=ip++;
edge[ip]= (Edge){u,hlist[v]};
hlist[v]=ip++;
} void bfsB() // 1-n 连b边
{
dis[n]=INF;
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push();
dis[]=;
vis[]=;
while (!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i=hlist[u];i!=-;i=edge[i].nex)
{
int v = edge[i].v;
if (!vis[v])
{
dis[v]=dis[u]+;
Q.push(v);
vis[v]=;
}
}
if (dis[n]!=INF) break;
}
printf("%lld\n",min(dis[n]*a,(LL)b));
} void bfsA() //1-n 连 a 边
{
dis[n]=INF;
set<int> st,ts;
for (int i=;i<=n;i++) st.insert(i);
set<int>::iterator it;
queue<int> Q;
Q.push();
dis[]=;
while (!Q.empty())
{
int u = Q.front(); Q.pop();
for (int i=hlist[u];i!=-;i=edge[i].nex)
{
int v=edge[i].v;
if (st.count(v)==) continue;
st.erase(v); ts.insert(v);
}
for (it=st.begin();it!=st.end();it++)
{
dis[*it] = dis[u]+;
Q.push(*it);
}
if (dis[n]!=INF) break;
st.swap(ts);
ts.clear();
}
printf("%lld\n",min(dis[n]*b,(LL)a));
} int main()
{
while(scanf("%d%d%d%d",&n,&m,&a,&b)!=EOF)
{
memset(hlist,-,sizeof(hlist));
ip=;
bool flag=;
for (int i=;i<m;i++)
{
int u = scan();
int v = scan();
addedge(u,v);
if (u>v) swap(u,v);
if (u==&&v==n) flag=;
}
if (flag)
{
if (a<b) printf("%d\n",a);
else bfsA();
}
else
{
if (b<a) printf("%d\n",b);
else bfsB();
}
}
return ;
}
Travel(最短路)的更多相关文章
- SCU 4444: Travel(最短路)
Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ...
- [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\),那么对 ...
- 【UVA10816】Travel in Desert (最小瓶颈路+最短路)
UVA10816 Travel in Desert 题目大意 沙漠中有一些道路,每个道路有一个温度和距离,要求s,t两点间的一条路径,满足温度最大值最小,并且长度最短 输入格式 输入包含多组数据. 每 ...
- SCU 4444 Travel (补图最短路)
Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...
- PAT-1030 Travel Plan (30 分) 最短路最小边权 堆优化dijkstra+DFS
PAT 1030 最短路最小边权 堆优化dijkstra+DFS 1030 Travel Plan (30 分) A traveler's map gives the distances betwee ...
- HDU2433—Travel (BFS,最短路)
Travel Time Limit: 10000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Sub ...
随机推荐
- 香蕉派 Banana pi BPI-M1+ 双核开源单板计算机. 板载WIFI
Banana PI BPI-M1+是一款高性能双核开源硬件单板计算机,Banana PI BPI-M1+是一款比树莓派更强悍的双核Android4.4与Linux产品. Banana PI BP ...
- 利用Perlin nosie 完毕(PS 滤镜—— 分成云彩)
%%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...
- Quartz.Net线程处理用到的两个Attribute
1.DisallowConcurrentExecution 加到IJob实现类上,主要防止相同JobDetail并发执行. 简单来说,现在有一个实现了IJob接口的CallJob,触发器设置的时间是每 ...
- 02-3设置第一启动项--进入BIOS设置USB方式启动
设置USB方式启动 https://zhinan.sogou.com/guide/detail/?id=1610014869 如何设置电脑从U盘启动呢?今天小编教大家如何进入BIOS设置USB方式启动 ...
- 锁定窗口,禁止更新的win32函数
[DllImport("user32.dll", EntryPoint = "LockWindowUpdate", SetLastError = true, E ...
- Atitit.cateService分类管理新特性与设计文档说明v1
Atitit.cateService分类管理新特性与设计文档说明v1 1. V2 新特性规划1 2. 分类管理1 3. 分类增加与修改维护2 4. Js控件分类数据绑定2 1. V2 新特性规划 增加 ...
- cookie转coontoin
/// <summary> /// 一个到多个Cookie的字符串添加到CookieCollection集合中[isGood代码] /// </summary> /// < ...
- android studio - 隐藏编辑器上面的竖线
android studio 的代码编辑器上面默认有一条竖线不知道是干什么用的,很难看.可以用下列方法进行隐藏. 取消选中这个复选框即可.
- iOS开发 多屏尺的自动适配(转)
[iOS开发]多屏尺的自动适配 AutoLayout (纯代码方式) (2014-09-19 09:35:47) 转载▼ 标签: autolayout ios开发 xcode ui 分类: Xcode ...
- hdu 4786 Fibonacci Tree(最小生成树)
Fibonacci Tree Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) T ...