[SDOI 2010]魔法猪学院
Description
给出一张 \(n\) 个点 \(m\) 条边有向图,询问最多有多少条不同的路径从 \(1\) 到 \(n\) 并且路径长度和 \(\leq E\) 。
\(2\leq n\leq 5000,1\leq m\leq 200000,1\leq E\leq 10^7\)
Solution
由于要求最多多少条,我们有贪心的思想,选取尽可能短的路径不会差。
那么题目就变成了求这张图 \(1\rightarrow n\) 的最短的 \(ans\) 条路径,并且路径的长度和 \(\leq E\) 。
就变成了裸的 \(k\) 短路问题。
由 \(f(x)=g(x)+h(x)\) ,由于让路径尽可能短,那么估价函数 \(h(x)\) 就取点 \(x\) 到 \(n\) 的最短路就好了。
b站上这道题代码写的丑的 \(stl\) 会 \(MLE\) ,像我这种菜鸡肯定过不了,只能手写可并堆了...
Code
//It is made by Awson on 2018.3.1
#include <bits/stdc++.h>
#define LL long long
#define dob complex<double>
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = 5000, M = 200000, INF = ~0u>>1;
void read(int &x) {
char ch; bool flag = 0;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
x *= 1-2*flag;
}
void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
int n, m, vis[N+5]; double dist[N+5], e;
struct node {
double f, g; int u;
bool operator < (const node &b) const {return f > b.f; }
};
struct mergeable_tree {
int ch[1300005][2], dist[1300005], pos, root, cnt; node k[1300005];
queue<int>mem;
int newnode(node x) {
int o; if (!mem.empty()) o = mem.front(), mem.pop(); else o = ++pos;
ch[o][0] = ch[o][1] = dist[o] = 0; k[o] = x; return o;
}
int merge(int a, int b) {
if (!a || !b) return a+b;
if (k[a] < k[b]) Swap(a, b);
ch[a][1] = merge(ch[a][1], b);
if (dist[ch[a][0]] < dist[ch[a][1]]) Swap(ch[a][0], ch[a][1]);
dist[a] = dist[ch[a][1]]+1; return a;
}
node top() {return k[root]; }
void push(node x) {root = merge(root, newnode(x)); ++cnt; }
void pop() {mem.push(root); root = merge(ch[root][0], ch[root][1]); --cnt; }
bool empty() {return !cnt; }
}Q;
struct Graph {
struct tt {int to, next; double cost; }edge[M+5];
int path[N+5], top;
void add(int u, int v, double c) {edge[++top].to = v, edge[top].next = path[u], edge[top].cost = c, path[u] = top; }
void SPFA() {
for (int i = 1; i < n; i++) dist[i] = INF;
queue<int>Q; vis[n] = 1; Q.push(n);
while (!Q.empty()) {
int u = Q.front(); Q.pop(); vis[u] = 0;
for (int i = path[u]; i; i = edge[i].next)
if (dist[edge[i].to] > dist[u]+edge[i].cost) {
dist[edge[i].to] = dist[u]+edge[i].cost;
if (!vis[edge[i].to]) Q.push(edge[i].to), vis[edge[i].to] = 1;
}
}
}
int Astar() {
int ans = 0;
node t, tt; t.f = dist[1], t.g = 0, t.u = 1;
Q.push(t);
while (!Q.empty()) {
t = Q.top(); Q.pop();
if (t.u == n) {if (e >= t.f) {ans++, e -= t.f; continue; } else break; }
for (int i = path[t.u]; i; i = edge[i].next) {
tt.g = t.g+edge[i].cost, tt.u = edge[i].to, tt.f = tt.g+dist[edge[i].to];
Q.push(tt);
}
}
return ans;
}
}g1, g2;
void work() {
read(n), read(m); scanf("%lf", &e); int u, v; double c;
for (int i = 1; i <= m; i++) {
read(u), read(v), scanf("%lf", &c), g1.add(u, v, c), g2.add(v, u, c);
}
g2.SPFA(); writeln(g1.Astar());
}
int main() {
work(); return 0;
}
[SDOI 2010]魔法猪学院的更多相关文章
- 解题:SDOI 2010 魔法猪学院
题面 题外话:神**可持久化左偏树,你谷的人都太神了,学不来 我把这个当做A*模板题的说,先讲一讲个人对A*的理解:如果说普通的BFS是Bellman_Ford,那A*就是一个Dijkstra.以寻找 ...
- BZOJ-1975 魔法猪学院 K短路 (A*+SPFA)
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1323 Solved: 433 [Submit][Statu ...
- Bzoj 1975: [Sdoi2010]魔法猪学院 dijkstra,堆,A*,K短路
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1357 Solved: 446[Submit][Statu ...
- bzoj 1975: [Sdoi2010]魔法猪学院 [k短路]
1975: [Sdoi2010]魔法猪学院 裸题... 被double坑死了 #include <iostream> #include <cstdio> #include &l ...
- BZOJ_1975_[Sdoi2010]魔法猪学院_A*
BZOJ_1975_[Sdoi2010]魔法猪学院_A* Description iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPi ...
- K短路 (A*算法) [Usaco2008 Mar]牛跑步&[Sdoi2010]魔法猪学院
A*属于搜索的一种,启发式搜索,即:每次搜索时加一个估价函数 这个算法可以用来解决K短路问题,常用的估价函数是:已经走过的距离+期望上最短的距离 通常和Dijkstra一起解决K短路 BZOJ1598 ...
- bzoj 1975 [Sdoi2010]魔法猪学院
1975: [Sdoi2010]魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1758 Solved: 557[Submit][Statu ...
- P2483 [SDOI2010]魔法猪学院
P2483 [SDOI2010]魔法猪学院 摘要 --> 题目描述 iPig在假期来到了传说中的魔法猪学院,开始为期两个月的魔法猪训练.经过了一周理论知识和一周基本魔法的学习之后,iPig对猪世 ...
- 【BZOJ1975】【SDOI2010】魔法猪学院 [A*搜索]
魔法猪学院 Time Limit: 10 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description iPig在假期来到了传说中的魔法猪 ...
随机推荐
- [日常] Codeforces Round #441 Div.2 实况
上次打了一发 Round #440 Div.2 结果被垃圾交互器卡掉 $200$ Rating后心情复杂... 然后立了个 Round #441 要翻上蓝的flag QAQ 晚饭回来就开始搞事情, 大 ...
- Java基础学习笔记二十 IO流
转换流 在学习字符流(FileReader.FileWriter)的时候,其中说如果需要指定编码和缓冲区大小时,可以在字节流的基础上,构造一个InputStreamReader或者OutputStre ...
- C#编写一个大字母游戏,详细代码,不懂问博主。。。。
using System; using System.Drawing; using System.Windows.Forms; using System.Media; namespace dazimu ...
- 构建微服务开发环境1————如何安装JDK
[内容指引] 下载JDK: Mac系统安装JDK: Mac系统配置环境变量: Windows系统安装JDK: Windows系统配置环境变量. 一.下载JDK 1.访问Oracle官网 http:// ...
- 如何从RxJava升级到RxJava2
如何从RxJava升级到RxJava2. RxJava2已经推出有一年半的时间,由于之前RxJava已经在现有项目中广泛使用,而RxJava2在除了很多命名外并没有太多革新,所以相信有很多人跟我一样都 ...
- alpha-咸鱼冲刺day4-紫仪
总汇链接 一,合照 emmmmm.自然还是没有的. 二,项目燃尽图 三,项目进展 QAQ具体工作量没啥进展.但是前后端终于可以数据交互了!.. 四,问题困难 日常啥都不会,百度真心玩一年. 还 ...
- 关于from nltk.book import * 报错解决方法
import nltk nltk.download() 在使用上面命令安装了nltk库并运行下载后,再输入from nltk.book import * 往往会出现这样的错误提示: 出现这种错误往往是 ...
- python中使用flask时遇到的markupsafe._compat包缺失的问题与解决
环境:windows7 + python3.6.0 在尝试使用python的flask时,按照flask的tutorial操作,装好flask.venv后,对tutorial中的hello.py进行运 ...
- 【iOS】swift 74个Swift标准库函数
本文译自 Swift Standard Library: Documented and undocumented built-in functions in the Swift standard li ...
- 同样是IT培训,为什么人家月薪过万,你才几千,问题在哪?!
听过一句话"360行,行行转IT",虽然有些夸张,但也不难看出IT行业的火爆程度.从李总理提的"互联网+大数据"开始,中国的这场"互联网+" ...