题意:

一个完全图,有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. 《HTTP - 概述》

    晚上听着 木小雅<可能否>,让我这暴躁的一天得以回复一下. 学习是长久之事,不能急躁.应该系统的去学习一些东西. 1:HTTP 最严谨的叫法应该是 超文本转移协议,但大家一般都叫做 超文本 ...

  2. Django2.0跨域请求配置

    跨域:通过js或python在不同的域之间进行数据传输或通信,比如用ajax向一个不同的域请求数据,或者通过js获取页面中不同域的框架中(Django)的数据.只要协议.域名.端口有任何一个不同,都被 ...

  3. 如何使用 window api 转换字符集?(std::string与std::wstring的相互转换)

    //宽字符转多字节 std::string W2A(const std::wstring& utf8) { int buffSize = WideCharToMultiByte(CP_ACP, ...

  4. LeetCode-37.Sudok Solver

    Write a program to solve a Sudoku puzzle by filling the empty cells. A sudoku solution must satisfy  ...

  5. (1.8)mysql中的trace

    (1.8)mysql中的trace 以json格式存储

  6. python基础教程 变量/输入输出/if判断

    python的运用越来越多.大数据经常被人谈及,数据从何而来?通过各个平台.app.网站数据的收集,分析,过滤,生成报告,这些都可以用python来处理,并且有很多成熟的库可以直接用了.那还不赶紧深入 ...

  7. Mysql常用的存储引擎

    存储引擎 存储引擎是表级别的概念,不同的存储引擎保存数据和索引的方式是不相同的. MyISAM存储引擎 MyISAM最典型的性能问题就是表锁的问题.  MyISAM只将数据写到内存中,然后等待操作系统 ...

  8. secure CRT常用的查看服务器日志命令

    tail -f  /(日志文件目录)   查看日志命令ctrl+c停止打印ls  -al  查看文件的详细信息路径等vi  /(日志文件目录)  打开日志/搜索字符退出:按ESC后,按“:”,输入q, ...

  9. eclipse 假死

    由于电脑关机,导致Eclipse非正常关闭,之后启动Eclipse发现一直启动不起来,于是从网上找了一些方法如下: 1.删除文件.snap 到<workspace>\.metadata\. ...

  10. PHP中MySQL、MySQLi和PDO的用法和区别

    PHP的MySQL扩展(优缺点) 设计开发允许PHP应用与MySQL数据库交互的早期扩展.mysql扩展提供了一个面向过程 的接口: 并且是针对MySQL4.1.3或更早版本设计的.因此,这个扩展虽然 ...