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的更多相关文章

  1. SCU 4444: Travel(最短路)

    Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ...

  2. SCU 4444 Travel (补图最短路)

    Travel The country frog lives in has \(n\) towns which are conveniently numbered by \(1, 2, \dots, n ...

  3. scu 4444 Travel

    题意: 一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路. 思路: 首先判断1和n被哪种边连通. 如果是被a连通,那么就需要全部走b的边到达n,选择最小的 ...

  4. 第十五届四川省省赛 SCU - 4444 Travel

    给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点)  p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ...

  5. 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 / ...

  6. 图论 - Travel

    Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...

  7. ACM:SCU 4437 Carries - 水题

    SCU 4437  Carries Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice  ...

  8. ACM: SCU 4438 Censor - KMP

     SCU 4438 Censor Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practice D ...

  9. ACM: SCU 4440 Rectangle - 暴力

     SCU 4440 Rectangle Time Limit:0MS     Memory Limit:0KB     64bit IO Format:%lld & %llu  Practic ...

随机推荐

  1. 【SCOI 2011】 糖果

    [题目链接] 点击打开链接 [算法] 当x = 1时,连边(a,b,0)和(b,a,0) 当x = 2时,连边(a,b,1) 当x = 3时,连边(b,a,0) 当x = 4时,连边(b,a,1) 当 ...

  2. 图练习-BFS-从起点到目标点的最短步数

    http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2830 简单bfs #include <s ...

  3. codevs1369 xth 砍树(线段树)

    1369 xth 砍树  时间限制: 1 s  空间限制: 128000 KB  题目等级 : 钻石 Diamond     题目描述 Description 在一个凉爽的夏夜,xth 和 rabbi ...

  4. 观察者模式-C#实现

    定义对象间的一种一对多的依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都得到通知并被自动更新. 观察者模式有四个角色:抽象主题.具体主题.抽象观察者.具体观察者. 抽象主题:把所有观察者对象 ...

  5. Python基础数据类型(三)list 列表

    3.4列表list [] 列表的格式 lst_l = [1,'123',[1,'www',2],'包青天'] 列表也有索引 print(lst_l[0]) print([-1][0:2]) #包青 切 ...

  6. 《Typecript 入门教程》 2、访问控制符:public、private、protected、readonly

    声明类的属性和方法时可以设置使用访问控制符,访问控制符设置类的属性和方法能不能在类的外部被访问 1. 默认为 public,使用public定义的属性和方法在类的内部和外部都可以访问 2. priva ...

  7. 简单 android popupwindow 实现

    1. popupWindow 设置大小: popupWindow = new  PopupWindow(view, ViewGroup.LayoutParams.FILL_PARENT, ViewGr ...

  8. [转]mysql的约束

    转自:http://blog.csdn.net/kqygww/article/details/8882990 MySQL中约束保存在information_schema数据库的table_constr ...

  9. java 多线程并发系列之 生产者消费者模式的两种实现

    在并发编程中使用生产者和消费者模式能够解决绝大多数并发问题.该模式通过平衡生产线程和消费线程的工作能力来提高程序的整体处理数据的速度. 为什么要使用生产者和消费者模式 在线程世界里,生产者就是生产数据 ...

  10. Android RecyclerView使用 及 滑动时加载图片优化方案

    1.控制线程数量 + 数据分页加载2.重写onScrollStateChanged方法 这个我们后面再谈,下面先来看看RecyclerView控件的使用及我们为什么选择使用它 RecyclerView ...