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 ...
随机推荐
- vue - check-versions.js for chalk
chalk:npm 官网 修改命令输出的文字颜色.粗细等....
- 怎样用Google APIs和Google的应用系统进行集成(4)----获得Access Token以通过一些Google APIs的OAuth2认证
在上篇文章中: "怎样用Google APIs和Google的应用系统进行集成(3)----调用发现Google APIs的RESTful的服务"一文中,我们直接用jdk的java ...
- 微信小程序flex容器属性详解
flex容器属性详解 flex-direction决定元素的排列方向 flex-wrap决定元素如何换行 flex-flow 是 flex-direction 和flex-wrap的简写 justif ...
- 一个我用来上传代码到Github的 Shell 脚本
因为用git老是要敲许多命令.所以写了个小脚本.代码如下: #! /bin/sh echo Going to simpleWebtest... cd ~/softwaredevelopment/wor ...
- MongoDB密码设置(基于windows)
参考文档:http://www.cnblogs.com/zengen/archive/2011/04/23/2025722.html MongoDB部署到Windows上后是默认是无权限限制的的. ...
- ImageBox Control with Zoom/Pan Capability
Download source files - 10.8 Kb Download demo project - 6.81 Kb Introduction This control extends th ...
- 移动端H5实现图片上传
移动端H5实现图片上传 https://segmentfault.com/a/1190000010034177
- unity, iOS集成微信
将微信sdk直接拖进xcode会导致Library Search Paths是错的,需要手动改成如下样子(蓝色选中部分)才能通过编译:
- php 结合md5的加密,解密方法
php 结合md5的加密,解密方法 张映 发表于 2012-06-28 分类目录: php 标签:md5, php 最近在整理代码发现了一个不错的东西,结合md5的加解密算法.网上关于php结合md5 ...
- 【转载】使用SoapUI 测试Web Service
转载地址:使用SoapUI 测试Web Service 如何测试写好的Webservice?你当然可以写代码来测试,但还是太麻烦,你得花时间去学习各语言的关于Webservice调用的相关API. ...