scu 4444 Travel
题意:
一个完全图,有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的更多相关文章
- 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
		给你一个一共由两种边的完全图 要求你求1到N的最短路 q队列为前沿队列(已探索过且最外围的点) p队列为未探索队列(未探索过的点) depth这个数组的用法并不是代表实际上这个点在第几层 而是防止死 ... 
- SCU Travel
		Travel The country frog lives in has n towns which are conveniently numbered by 1,2,…,n . Among n(n− ... 
- 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 ... 
随机推荐
- shell脚本中sqlite3命令查询数据库失败返回空,并将错误信息打印到标准错误输出
			shell脚本中sqlite3命令查询数据库失败返回空,并将错误信息打印到标准错误输出 如: #/bin/sh local ret='sqlite3 test.db "select test ... 
- Python pip 如何升级
			场景:部署环境时,在线安装第三方库(pip install flask-bootstrap),提示pip版本过低. 解决方法一: 命令: python -m pip install -- ... 
- vue脚手架 构建豆瓣App  第一天
			课堂笔记: 项目结构分析: 项目入口:index.html(div#app) 全局vue组件:App.vue(template:div#app) 通过相同id的div,index.html与Appvu ... 
- 洛谷P4562 [JXOI2018]游戏 数论
			正解:数论 解题报告: 传送门! 首先考虑怎么样的数可能出现在t(i)那个位置上?显然是[l,r]中所有无法被表示出来的数(就约数不在[l,r]内的数嘛QwQ 所以可以先把这些数筛出来 具体怎么筛的话 ... 
- 洛谷P4587 神秘数 [FJOI2016] 主席树
			正解:主席树 解题报告: 先放下传送门QAQ 首先可以先思考如果只有一组询问,怎么解决 可以这么想,最开始一个数也麻油的时候能表示的最大的数是0嘛 然后先排个序,按顺序每次新加入一个数x,设加入这个数 ... 
- CF891C Envy 最小生成树/虚树
			正解:最小生成树/虚树 解题报告: 传送门! sd如我就只想到了最暴力的想法,一点儿优化都麻油想到,,,真的菜到爆炸了QAQ 然后就分别港下两个正解QAQ 法一,最小生成树 这个主要是要想到关于最小生 ... 
- winform接收全局的快捷键
			public class NativeWIN32 { public NativeWIN32() { } /* ------- using WIN32 Windows API in a C# appli ... 
- 【JMeter】【性能测试】配置元件
			HTTP Cookie Manager 用来存储浏览器产生的用户信息 Clear Cookies each Iteration:每次迭代请求,清空cookies,GUI中定义的任何cookie都不会被 ... 
- php 7 新特性整理小结
			php 7 比php 5 性能提升了很多,php 7 新特性主要表现在:1.变量存储字节减小,减少内存占用,提升变量操作速度:2.改善数组结构,数组元素和hash映射表被分配在同一块内存里,降低了内存 ... 
- ORACLE入门之Linux基础篇
			VIM0 这是数字『0 』:移动到这一行的最前面字符处$ 移动到这一行的最后面字符处G 移动到这个档案的最后一行nG n 为数字.移动到这个档案的第n 行.例如20G 则会移动到这个档 ... 
