题意:

一个完全图,有n个点,其中m条边是权值为a的无向边,其它是权值为b的无向边,问从1到n的最短路。

思路:

首先判断1和n被哪种边连通。

如果是被a连通,那么就需要全部走b的边到达n,选择最小的;

被b连通,需要走全部为a的边到达n,选择最小的。

第二种情况,用输入的边跑dijkstra;

但是第一种情况边太多,所以并不能单纯的用最短路。

可以想到的是,对于第二种情况,一个点只会经过一次。

所以用一个set来存还未访问过的点,进行bfs。

每次从队列中取出一个点x,把set中与x以a边相连的点暂时去掉,那么此时set中就是与x以b边相连并且还未访问的点了,这个时候就可以进行松弛了。

之后再对set进行更新,使其为还未访问到的点。

又犯了这个睿智错误,数据还没输入就进行处理了,真是睿智。

自定义的比较宏比algor里面得到快很多啊。

代码:

 #include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#include <set>
#include <queue>
#define mi(x,y) (x) > (y) ? (y) : (x)
using namespace std;
typedef long long ll;
const int N = 1e5 + ;
struct node
{
int to,cost;
node (int a,int b):to(a),cost(b){};
node(){};
};
struct js
{
int x;
ll d;
js(int a,ll b):x(a),d(b){};
js(){};
bool operator < (const js& y) const
{
return y.d < d;
}
};
vector<node> g[N];
int n,m,a,b;
ll dis[N];
void dij(void)
{
for (int i = ;i <= n;i++) dis[i] = 1e18;
dis[] = ;
priority_queue<js> pq;
pq.push(js(,));
while (!pq.empty())
{
js t = pq.top();pq.pop();
if (t.d > dis[t.x]) continue;
int x = t.x;
for (auto v:g[x])
{
if (dis[v.to] > dis[x] + v.cost)
{
dis[v.to] = dis[x] + v.cost;
pq.push(js(v.to,dis[v.to]));
}
}
}
}
void bfs(void)
{
set<int> s,t;
for (int i = ;i <= n;i++) s.insert(i);
queue<int> q;
q.push();
while (!q.empty())
{
int x = q.front();q.pop();
if (x == n) break;
for (auto v:g[x])
{
if (s.count(v.to) == ) continue;
s.erase(v.to);
t.insert(v.to);
}
for (auto y:s)
{
dis[y] = dis[x] + b;
q.push(y);
}
s.swap(t);
t.clear();
}
}
int main()
{
while (scanf("%d%d%d%d",&n,&m,&a,&b) != EOF)
{
for (int i = ;i <= n;i++)
{
g[i].clear();
}
bool f = ;
for (int i = ;i < m;i++)
{
int x,y;
scanf("%d%d",&x,&y);
if (x > y) swap(x,y);
g[x].push_back(node(y,a));
g[y].push_back(node(x,a));
if (x == && y == n) f = ;
}
ll ans;
if (f)
{
bfs();
ans = mi((ll)a,dis[n]);
}
else
{
dij();
ans = mi((ll)b,dis[n]);
}
printf("%lld\n",ans);
}
return ;
}

scu 4444 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

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

  4. SCU Travel

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

  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. 使用Git,如何忽略不需要上传的文件(配置文件)

    步骤1:在目录下,选择GIt Bash Here 2.输入命令 : git update-index --assume-unchanged 文件名 3.再输入指令 git  status 查看修改文件 ...

  2. html5页面与android页面之间通过url传递参数

    html5页面与android页面之间可以通过url传递参数,android将参数放在htm5的url  ?后面,js获取url  ?号后面的参数. 方法一: <scrīpt> /* 用途 ...

  3. ITouch在xcode下提示‘No such file or directory, at ‘/SourceCache/DVTi...'

    版权声明:本文为博主原创文章,转载或又一次发表请先与我联系. https://blog.csdn.net/jonahzheng/article/details/37692733       用一个台老 ...

  4. 在linux下一般用scp这个命令来通过ssh传输文件

    在linux下一般用scp这个命令来通过ssh传输文件. 1.从服务器上下载文件scp username@servername:/path/filename /var/www/local_dir(本地 ...

  5. Cookie和Session的区别?

    1.Cookie和Session都是会话技术,Cookie是运行在客户端,Session是运行在服务器端.      2.Cookie有大小限制以及浏览器在存cookie的个数也有限制,Session ...

  6. linux命令注解

    参考: Linux命令实例练习 -- 实验楼 太懒,就不全抄了,把自己觉得有坑的地方记录下来. ls ls命令的20个实用范例 -- linux.cn 常用参数 参数 描述 -a –all 列出目录下 ...

  7. vue 验证码倒计时

    //html <div class="input-div" v-show="formData.phone"> <input type=&quo ...

  8. ionic 版本内更新问题汇总

    1.签名不一致导致的更新失败 2.解析软件包出现问题 3.当文件下载完.在android 8.0中不能打开apk包的问题 解决方案:在config.xml中添加: <platform name= ...

  9. 一文看懂POS收单中"MCC"是什么意思?

    MCC的前世今生! 是否经常听人提起过“MCC”?听起来如此高大上的词,背后有着怎样的知识内涵呢?您知道吗?今天,我们就一起来了解了解“MCC”的前世今生,让它也“接接地气”吧!商户类别码(简称MCC ...

  10. Editplus 竖选,竖插入技巧

    竖选方法 1,Alt + C, 然后用鼠标拖选 2,按住Alt健,再用鼠标拖选 行首行尾批量添加字符 以及其它常用正则 操作:Ctrl + H, 调出查找窗口,勾选按正则表达式查询 行首批量添加   ...