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 ...
随机推荐
- valgrind的callgrind工具进行多线程性能分析
1.http://valgrind.org/downloads/old.html 2.yum install valgrind Valgrind的主要作者Julian Seward刚获得了今年的Goo ...
- socket websocket
1.websocket客户端 websocket允许通过JavaScript建立与远程服务器的连接,从而实现客户端与服务器间双向的通信.在websocket中有两个方法: 1.send() 向远程服务 ...
- ijkPlayer 集成
代码地址如下:http://www.demodashi.com/demo/11957.html 概述 ijkplayer 是一款做视频直播的框架,基于FFmpeg,支持Android和iOS.这里介绍 ...
- Jmeter-接口测试(二)
接口测试我们前面已经讲过,此博不做重复,我们主要讲讲如何利用Jmeter做接口测试及参数化. 一.新建项目 1.运行Jmeter.bat打开Jmeter 2.添加线程组(测试计划->添加-> ...
- spring揭秘读书笔记----spring的ioc容器之BeanFactory
spring的ioc容器是一种特殊的Ioc Service Provider(ioc服务提供者),如果把普通的ioc容器认为是工厂模式(其实很相似),那spring的ioc容器只是让这个工厂的功能更强 ...
- C#中怎样将List<自己定义>转为Json格式 及相关函数-DataContractJsonSerializer
对C#和.net使用List<自己定义>和Json格式相互转化的方法进行总结 关于JSON的入门介绍见http://www.json.org/ ,或者百度,这里不赘述,只是通过以下的样例会 ...
- Nginx 0.8.x + PHP 5.2.13(FastCGI)搭建胜过Apache十倍的Web服务器(第6版)(转)
转自:http://blog.s135.com/nginx_php_v6/] 前言:本文是我撰写的关于搭建“Nginx + PHP(FastCGI)”Web服务器的第6篇文章.本系列文章作为国内最早详 ...
- 微信支付和微信支付通知基于sdk的说明
前提是,有微信服务号(必须开通了支付功能,也就是说有了商户后台) (注意商户后台 安全目录 的设置,不然即使你写的没错误,也调用不成功) 公众号h5页面写法: (购物车提交--我们上一步已经生成了订 ...
- APACHE KYLIN™ 概览
APACHE KYLIN™ 概览 Apache Kylin™是一个开源的分布式分析引擎,提供Hadoop之上的SQL查询接口及多维分析(OLAP)能力以支持超大规模数据,最初由eBay Inc. 开发 ...
- centos 启动 nginx
service nginx start https://jingyan.baidu.com/article/bad08e1ec2adc709c85121aa.html