SCU 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
分析:
补图最短路好题;
题意为给一个图,原图的边权为a,补图的边权为b,求在完全图里1到n的最短路;
首先1到n的最短路上的边权只全部由a或b构成;
这样就是原图补图分别求1到n的最短路;
原图bfs即可;
补图考虑到到当前点只会更新和之前的点在原图上有边而和当前点无边的点的情况;
这样更新后点是越来越少的,用set存点判断即可;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <bitset>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define sys system("pause")
const int maxn=1e5+;
const int N=1e3+;
using namespace std;
inline int id(int l,int r){return l+r|l!=r;}
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,a,b,vis[maxn];
ll d[maxn];
vi e[maxn];
int main()
{
int i,j;
while(~scanf("%d%d%d%d",&n,&m,&a,&b))
{
rep(i,,n)e[i].clear(),d[i]=1e18,vis[i]=;
rep(i,,m)scanf("%d%d",&j,&k),e[j].pb(k),e[k].pb(j);
d[]=;
queue<int>pq;
pq.push();
vis[]=;
while(!pq.empty())
{
int p=pq.front();
pq.pop();
for(int i=;i<e[p].size();i++)
{
int to=e[p][i];
if(!vis[to])
{
vis[to]=;
d[to]=d[p]+a;
pq.push(to);
}
}
}
set<int>ok1,ok2;
set<int>::iterator it;
rep(i,,n)ok1.insert(i);
pq.push();
while(!pq.empty())
{
int p=pq.front();
pq.pop();
for(int i=;i<e[p].size();i++)
{
int to=e[p][i];
if(ok1.find(to)!=ok1.end())
{
ok2.insert(to);
ok1.erase(to);
}
}
for(it=ok1.begin();it!=ok1.end();it++)
{
if(d[*it]>d[p]+b)
{
d[*it]=d[p]+b;
pq.push(*it);
}
}
ok1.swap(ok2);
ok2.clear();
}
printf("%lld\n",d[n]);
}
return ;
}
SCU 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 (补图最短路)
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这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...
- POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / SCU 1132 Invitation Cards / ZOJ 2008 Invitation Cards / HDU 1535 (图论,最短路径)
POJ 1511 Invitation Cards / UVA 721 Invitation Cards / SPOJ Invitation / UVAlive Invitation Cards / ...
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- ACM:SCU 4437 Carries - 水题
SCU 4437 Carries Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice ...
- ACM: SCU 4438 Censor - KMP
SCU 4438 Censor Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practice D ...
- ACM: SCU 4440 Rectangle - 暴力
SCU 4440 Rectangle Time Limit:0MS Memory Limit:0KB 64bit IO Format:%lld & %llu Practic ...
随机推荐
- ※交换排序(1)——快速排序(quick sort)
快速排序使用分治策略(Divide and Conquer)来把一个序列分为两个子序列.步骤为: 从序列中挑出一个元素,作为"基准"(pivot). 把所有比基准值小的元素放在基准 ...
- 【HAOI 2008】 糖果传递
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1045 [算法] 环形均分纸牌问题 [代码] #include<bits/std ...
- [python] ThreadPoolExecutor线程池
初识 Python中已经有了threading模块,为什么还需要线程池呢,线程池又是什么东西呢?在介绍线程同步的信号量机制的时候,举得例子是爬虫的例子,需要控制同时爬取的线程数,例子中创建了20个线程 ...
- Android内存管理(13)常见产生内存泄漏的原因
1.集合类泄漏 集合类如果仅仅有添加元素的方法,而没有相应的删除机制,导致内存被占用.如果这个集合类是全局性的变量 (比如类中的静态属性,全局性的 map 等即有静态引用或 final 一直指向它), ...
- jsp里post和get的乱码解决问题
6.乱码问题01:<%reques.setCharacterEncoding("utf-8");%> 02:get请求乱码 001.:String 编码之后的字符串 = ...
- jq-文本框只能输入数字
<input type="text" onKeyUp="value=value.replace(/\D/g,'')" /> onKeyUp: 当输 ...
- win32窗口映射(部分)
先理解一下“窗口”与“视区”的概念.“窗口”是逻辑坐标下的矩形区域,“视区”是设备坐标系下的区域.根据“窗口”和“视区”的大小可以确定x方向和y方向的比例因子. 例子如下: VOID OnPaint( ...
- JS——鼠标跟随
注意事项: 1.pageX.pageY的兼容问题 2.使目标移动鼠标中间位置还必须减去盒子宽度的一半 <!DOCTYPE html> <html lang="en" ...
- Oracle、Db2、SqlServer、MySQL 数据库插入当前系统时间
做易买网项目,由于对数据库插入系统时间不了解,常常遇到的问题: 1.java.sql.SQLException: ORA-01861: 文字与格式字符串不匹配.原因:由于获取系统时间类型不对,应为sy ...
- lnmp环境搭建后续-php安装
安装PHP7: 下载# wget http://PHP.net/get/php-7.0.2.tar.gz/from/a/mirror 建议安装之前先看看安装帮助文件INSTALL 解压安装 # tar ...