[AHOI2002]哈利·波特与魔法石
这道题比较简单,就是一个最短路(SSSP)。数据水,用Floyd即可AC。这里用了Dijkstra。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue> using namespace std; #define re register
#define rep(i, a, b) for (re int i = a; i <= b; ++i)
#define repd(i, a, b) for (re int i = a; i >= b; --i)
#define maxx(a, b) a = max(a, b);
#define minn(a, b) a = min(a, b);
#define LL long long
#define inf (1 << 30) inline int read() {
int w = , f = ; char c = getchar();
while (!isdigit(c)) f = c == '-' ? - : f, c = getchar();
while (isdigit(c)) w = (w << ) + (w << ) + (c ^ ''), c = getchar();
return w * f;
} const int maxn = + , maxm = 1e4 + ; struct Edge {
int u, v, w, pre;
}; struct Node {
int u, d;
bool operator < (const Node &rhs) const {
return d > rhs.d;
}
}; int way[] = {, , , , , , , }; priority_queue<Node> Q; struct Graph {
#define iter(i, u) for (re int i = G[u]; i; i = edges[i].pre)
Edge edges[maxm << ];
int n, m;
int G[maxn];
int dis[maxn], vis[maxn];
void init(int n) {
this->n = n;
m = ;
memset(G, , sizeof(G));
}
void AddEdge(int u, int v, int w) {
edges[++m] = (Edge){u, v, way[w], G[u]};
G[u] = m;
edges[++m] = (Edge){v, u, way[w], G[v]};
G[v] = m;
}
void dijkstra(int s) {
memset(vis, , sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
dis[s] = ;
Q.push((Node){s, });
while (!Q.empty()) {
int u = Q.top().u; Q.pop();
if (vis[u]) continue;
vis[u] = ;
iter(i, u) {
Edge &e = edges[i];
if (dis[u] + e.w < dis[e.v]) {
dis[e.v] = dis[u] + e.w;
Q.push((Node){e.v, dis[e.v]});
}
}
}
}
} G; int s, t, m; int main() {
freopen("ques.in", "r", stdin);
freopen("ques.out", "w", stdout); rep(i, , )
if (read()) way[i] >>= ; s = read(), t = read(); G.init(); m = read(); rep(i, , m) {
int u = read(), v = read(), w = read();
G.AddEdge(u, v, w);
} G.dijkstra(s); printf("%d", G.dis[t]); return ;
}
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>
using namespace std;
#define re register#define rep(i, a, b) for (re int i = a; i <= b; ++i)#define repd(i, a, b) for (re int i = a; i >= b; --i)#define maxx(a, b) a = max(a, b);#define minn(a, b) a = min(a, b);#define LL long long#define inf (1 << 30)
inline int read() {int w = 0, f = 1; char c = getchar();while (!isdigit(c)) f = c == '-' ? -1 : f, c = getchar();while (isdigit(c)) w = (w << 3) + (w << 1) + (c ^ '0'), c = getchar();return w * f;}
const int maxn = 100 + 5, maxm = 1e4 + 5;
struct Edge {int u, v, w, pre;};
struct Node {int u, d;bool operator < (const Node &rhs) const {return d > rhs.d;}};
int way[8] = {0, 2, 6, 4, 8, 6, 10, 14};
priority_queue<Node> Q;
struct Graph {#define iter(i, u) for (re int i = G[u]; i; i = edges[i].pre)Edge edges[maxm << 1];int n, m;int G[maxn];int dis[maxn], vis[maxn];void init(int n) {this->n = n;m = 0;memset(G, 0, sizeof(G));}void AddEdge(int u, int v, int w) {edges[++m] = (Edge){u, v, way[w], G[u]};G[u] = m;edges[++m] = (Edge){v, u, way[w], G[v]};G[v] = m;}void dijkstra(int s) {memset(vis, 0, sizeof(vis));memset(dis, 0x3f, sizeof(dis));dis[s] = 0;Q.push((Node){s, 0});while (!Q.empty()) {int u = Q.top().u; Q.pop();if (vis[u]) continue;vis[u] = 1;iter(i, u) {Edge &e = edges[i];if (dis[u] + e.w < dis[e.v]) {dis[e.v] = dis[u] + e.w;Q.push((Node){e.v, dis[e.v]});}}}}} G;
int s, t, m;
int main() {freopen("ques.in", "r", stdin);freopen("ques.out", "w", stdout);
rep(i, 1, 7) if (read()) way[i] >>= 1;
s = read(), t = read();
G.init(100);
m = read();
rep(i, 1, m) {int u = read(), v = read(), w = read();G.AddEdge(u, v, w);}
G.dijkstra(s);
printf("%d", G.dis[t]);
return 0;}
[AHOI2002]哈利·波特与魔法石的更多相关文章
- 洛谷——P2559 [AHOI2002]哈利·波特与魔法石
P2559 [AHOI2002]哈利·波特与魔法石 题目描述 输入输出格式 输入格式: 文件中第一行有七个数,分别是 S1. S2 . …. S7 :第二行有两个数,依次分别是起点城市 i 和终点城市 ...
- 洛谷 P2559 [AHOI2002]哈利·波特与魔法石
P2559 [AHOI2002]哈利·波特与魔法石 题目描述 输入输出格式 输入格式: 文件中第一行有七个数,分别是 S1. S2 . …. S7 :第二行有两个数,依次分别是起点城市 i 和终点城市 ...
- 微软亚洲研究院的“哈利·波特”:Thomas Moscibroda
在微软亚洲研究院,有一位名为Thomas Moscibroda的研究员几乎是无人不知.无人不晓,江湖人送外号"哈利·波特".Thomas认为他这么"红"是因为他 ...
- PTA 07-图4 哈利·波特的考试 (25分)
哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变化的魔咒就是简单地将原来的魔咒倒过来念 ...
- 牛客OI周赛6-提高组 A 大法师与魔法石
大法师与魔法石 思路: 对于一个ai, 它可以构成区间[ai/v, ai] 假设和它相邻的为aj, 那么ai 和 aj 构成的区间为[(ai+aj) / v, ai+aj] 那么这两个区间能合并的条件 ...
- PTA 7-8 哈利·波特的考试(floyd)
哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变化的魔咒就是简单地将原来的魔咒倒过来念 ...
- RQNOJ魔法石之恋
魔法石之恋 (stone.pas/c/cpp) [问题描述] 在<Harry Potter and the Sorcerer's Stone>中,想得到魔法石,必须要通过许许多多的测试和游 ...
- 7-8 哈利·波特的考试(25 分)(图的最短路径Floyd算法)
7-8 哈利·波特的考试(25 分) 哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变 ...
- PTA 哈利·波特的考试 (25分)
PTA 哈利·波特的考试 (25分) 哈利·波特要考试了,他需要你的帮助.这门课学的是用魔咒将一种动物变成另一种动物的本事.例如将猫变成老鼠的魔咒是haha,将老鼠变成鱼的魔咒是hehe等等.反方向变 ...
随机推荐
- Spring Boot (四): Druid 连接池密码加密与监控
在上一篇文章<Spring Boot (三): ORM 框架 JPA 与连接池 Hikari> 我们介绍了 JPA 与连接池 Hikari 的整合使用,在国内使用比较多的连接池还有一个是阿 ...
- [LeetCode]singleNumber
题目:singleNumber Given an array of integers, every element appears twice except for one. Find that si ...
- embedding技术
目录 word2vec 负采样 目标函数 反向梯度 层次softmax NPLM的目标函数和反向梯度 目标函数 反向梯度 GNN(图神经网络) deepwalk node2vec 附录 word2ve ...
- 推荐5款自学手机APP,请低调收藏,让你变得越来越优秀
现在的手机APP真的是太多了,但里面的功能同类性又非常大,很难找到实用并且符合要求的APP.接下来就为小伙伴们推荐5款非常实用的APP软件,保证你会爱不释手,轻松秒变手机达人. 1.清爽视频编辑器 一 ...
- 设计模式之UML类图以及类间关系
类图是描述系统中的类,以及各个类之间的关系的静态视图.能够让我们在正确编写代码以前对系统有一个全面的认识.类图是一种模型类型,确切的说,是一种静态模型类型.类图表示类.接口和它们之间的协作关系. 以下 ...
- 二次编码 深浅拷贝 is和==
1.二次编码 ascii 不支持中文 gbk 支持中文 2个字节 包含ascii Unicode 万国码 python3 内存Unicode utf-8 可变的长度 英文 1字节 欧洲2个字节 亚洲3 ...
- [Machine Learning] Linear regression
1. Variable definitions m : training examples' count \(y\) : \(X\) : design matrix. each row of \(X\ ...
- git一步步上传自己的项目至github,及仓库更新
一.使用git上传项目到github 首先登陆github账号,选择新建一个库,填写项目名称,描述 创建完成之后,跳转到下面的页面,下面红框中的网址要记住,在后面上传代码的时候需要使用 接下来,我们需 ...
- Windows下如何调试驱动程序
Windows内核分析索引目录:https://www.cnblogs.com/onetrainee/p/11675224.html 一.配置Windbg使用双机调试 win10中“windbg+vm ...
- .NetCore WebApi —— Swagger版本控制
目录: .NetCore WebApi——Swagger简单配置 .NetCore WebApi——基于JWT的简单身份认证与授权(Swagger) .NetCore WebApi —— Swagge ...