洛谷P1608路径统计
这个提示一个简单的最短路计数,除了用数组存上最短路的个数的做法以外,还有可以在得出最短路之后,搜索加剪枝的方法来通过该题。
可以反向搜索用A*的方法来通过,但是这个题的去重十分的恶心,需要一些玄学操作。
\(Code\)
// luogu-judger-enable-o2
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cstdlib>
using namespace std;
struct cym {
int to, len, nex;
}e[6000010], ed[6000010];
int lin[100100], lind[100100], vis[100100], dis[100100], dist[100100], map[2010][2010], n, m, minn = 2147483647, ans, cnt;
inline void add(int f, int t, int l)
{
e[++cnt].to = t;
e[cnt].len = l;
e[cnt].nex = lin[f];
lin[f] = cnt;
ed[cnt].to = f;
ed[cnt].len = l;
ed[cnt].nex = lind[t];
lind[t] = cnt;
}
void dfs(int now, int sum)
{
if (now == n && sum == minn)
{
ans++;
return;
}
if (now == n || dist[now] + sum > minn)
return;
if (sum >= minn) return;
for (int i = lin[now]; i; i = e[i].nex)
{
dfs(e[i].to, sum + e[i].len);
}
}
inline void spfa()
{
queue <int> q;
for (int i = 2; i <= n; i++)
dis[i] = 2147483647;
dis[1] = 0;
q.push(1);
while (!q.empty())
{
int cur = q.front();
q.pop(), vis[cur] = 0;
for (int i = lin[cur]; i; i = e[i].nex)
{
int to = e[i].to;
if (dis[cur] + e[i].len < dis[to])
{
dis[to] = dis[cur] + e[i].len;
if (!vis[to])
vis[to] = 1, q.push(to);
}
}
}
}
inline void spfad()
{
memset(vis, 0, sizeof(vis));
queue <int> q;
for (int i = 1; i <= n; i++)
dist[i] = 2147483647;
dist[n] = 0;
q.push(n);
while (!q.empty())
{
int cur = q.front();
q.pop(), vis[cur] = 0;
for (int i = lind[cur]; i; i = ed[i].nex)
{
int to = ed[i].to;
if (dist[cur] + ed[i].len < dist[to])
{
dist[to] = dist[cur] + ed[i].len;
if (!vis[to])
vis[to] = 1, q.push(to);
}
}
}
}
int main()
{
// freopen("hh.txt", "r", stdin);
scanf("%d%d", &n, &m);
for (int i = 1; i <= m; i++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (map[a][b] == 0)
map[a][b] = 214746;
if (map[a][b] <= c) continue;
add(a, b, c);
map[a][b] = c;
}
spfa();
spfad();
/*for (int i = 1; i <= n; i++)
{
printf("%d ", dist[i]);
}
return 0;*/
minn = dis[n];
dfs(1, 0);
if (m == 0 || minn == 2147483647)
printf("No answer"), exit(0);
printf("%d %d", minn, ans);
}
洛谷P1608路径统计的更多相关文章
- 洛谷——P1608 路径统计
P1608 路径统计 题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方, ...
- 洛谷 P1608 路径统计
P1608 路径统计 题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方, ...
- 洛谷P1608 路径计数
题目简介 题目描述 给你一个N点M边的有向图,求第一个点到第n个点的最短路和最短路条数 题目分析 很明显直接Dijkstra求最短路,加一个最短路计数 如下: if(dis[y]>dis[x]+ ...
- [洛谷U40581]树上统计treecnt
[洛谷U40581]树上统计treecnt 题目大意: 给定一棵\(n(n\le10^5)\)个点的树. 定义\(Tree[l,r]\)表示为了使得\(l\sim r\)号点两两连通,最少需要选择的边 ...
- 【luogu P1608 路径统计】 题解
题目链接:https://www.luogu.org/problemnew/show/P1608 补上一发最短路计数! 感谢王强qwqqqq @Lance1ot #include <queue& ...
- luogu P1608 路径统计
题目描述 “RP餐厅”的员工素质就是不一般,在齐刷刷的算出同一个电话号码之后,就准备让HZH,TZY去送快餐了,他们将自己居住的城市画了一张地图,已知在他们的地图上,有N个地方,而且他们目前处在标注为 ...
- 洛谷——P1176 路径计数2
P1176 路径计数2 题目描述 一个N \times NN×N的网格,你一开始在(1,1)(1,1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问到达(N,N)(N,N),即右下角有 ...
- 洛谷 P1176 路径计数2
P1176 路径计数2 题目描述 一个N×N的网格,你一开始在(1, 1),即左上角.每次只能移动到下方相邻的格子或者右方相邻的格子,问到达(N, N),即右下角有多少种方法. 但是这个问题太简单了, ...
- 洛谷——P1179 数字统计
https://www.luogu.org/problem/show?pid=1179 题目描述 请统计某个给定范围[L, R]的所有整数中,数字 2 出现的次数. 比如给定范围[2, 22],数字 ...
随机推荐
- openlayer3相关扩展
1 ol3扩展 http://viglino.github.io/ol-ext/ ,里面包含编辑-选择控件,字体,动画,canvas绘制等等实例 2 ol3空间拓扑关系库jsts,有jst衍生过来 h ...
- Android 图片Bitmap,drawable,res资源图片之间转换
一.知识介绍 ①res资源图片是放在项目res文件下的资源图片 ②BitMap位图,一般文件后缀为BMP,需要编码器编码,如RGB565,RGB8888等.一种逐像素的显示对象,其执行效率高,但缺点也 ...
- QT使用websocket进行长连接
一般我们用的最多的就是http请求,但是频繁的请求可能对服务造成的压力很大,所以今天谈谈websocket长连接,一句话:简单 1.什么是长连接? A:一次请求连接,终身使用,就可以长久的保持信息的交 ...
- RHEL/Centos7 安装图形化桌面(转)
RHEL/Centos7 安装图形化桌面 Linux是一个多任务的多用户的操作系统,好多linux爱好者在安装完linux后经常遇到一个问题——没有图形化桌面.今天小编在安装RHEL7的时候,一步 ...
- Cs231n课堂内容记录-Lecture 5 卷积神经网络介绍
Lecture 5 CNN 课堂笔记参见:https://zhuanlan.zhihu.com/p/22038289?refer=intelligentunit 不错的总结笔记:https://blo ...
- 三机互ping(自己总结)
主机与虚拟机互ping设置: 点击VMware下的[编辑]--[虚拟网络编辑器]设置如下: 屏幕剪辑的捕获时间: 2016/5/21 13:10 屏幕剪辑的捕获时间: ...
- #021 Java复习第一天
上学期在慧河工作室学习简单过java到面向对象就停止了 现在有事情又要用到java发现全忘了..... 快速复习一下 网课PPT 计算机: 硬件 + 软件 主要硬件: cpu :cpu是一个计算机的运 ...
- django自定义分页器
一 django 的分页器 1 批量创建数据 批量导入数据: Booklist=[] for i in range(100): Booklist.append(Book(title="boo ...
- How Cigna Tuned Its Spark Streaming App for Real-time Processing with Apache Kafka
Explore the configuration changes that Cigna’s Big Data Analytics team has made to optimize the perf ...
- 数据可视化的开源方案: Superset vs Redash vs Metabase (一)
人是视觉动物,要用数据把一个故事讲活,图表是必不可少的.如果你经常看到做数据分析同事,在SQL客户端里执行完查询,把结果复制/粘贴到Excel里再做成图表,那说明你的公司缺少一个可靠的数据可视化平台. ...