题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688

题目大意:给n个点,m条有向边。再给出起点s, 终点t。求出s到t的最短路条数+次短路条数。

思路:

1.最短路和次短路是紧密相连的,在最短路松弛操作中,当我们找到一条更短的路径,也就意味着之前的路径不再是最短路,而成为了次短路,利用这个关系可以实现状态的转移。

2.好久没写优先队列了,都忘记了加个 priority_queue, 这样才能写重载,才能排序。

注释在代码里:

 #include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
const int inf = 0x3f3f3f3f;
using namespace std; int n, m;
int tot, head[];
int dis[][], cnt[][];//dis数组记录最短路 0 和次短路 1 距离,cnt数组记录最短路 0 和次短路 1 条数
int vis[][]; struct Edge
{
int to, next;
int w;
}edge[]; void add(int a, int b, int w)
{
edge[++ tot].to = b;
edge[tot].w = w;
edge[tot].next = head[a];
head[a] = tot;
} struct Node//优先队列优化结构体,id为点的序号, dis为源点到该点的距离 p区分属于最短路还是次短路
{
int id, dis, p;
bool operator < (const Node &a)const
{
return dis > a.dis;
}
}no; void init()
{
mem(head, -), tot = ;
mem(vis, ); //代表源点到该点的距离未被确定
} void dij(int s, int t)
{
priority_queue<Node> Q;
while(!Q.empty())
Q.pop();
for(int i = ; i <= n; i ++) //初始化
{
dis[][i] = dis[][i] = inf;
cnt[][i] = cnt[][i] = ;
}
dis[][s] = ;
cnt[][s] = ;//源点到自己的最短路条数为1
no.p = , no.dis = , no.id = s;
Q.push(no);
while(!Q.empty())
{
Node a = Q.top();
Q.pop();
if(vis[a.p][a.id])
continue; //该p状态下已经确定过就跳过
vis[a.p][a.id] = ;
for(int i = head[a.id]; i != -; i = edge[i].next)
{
int to = edge[i].to;
if(dis[][to] > dis[a.p][a.id] + edge[i].w) //最短路可以更新(在0或1状态下找到一条更短的路)
{
dis[][to] = dis[][to]; //原来的最短路成为次短路
dis[][to] = dis[a.p][a.id] + edge[i].w;
cnt[][to] = cnt[][to];//次短路条数继承为原来的最短路条数
cnt[][to] = cnt[a.p][a.id];
no.p = , no.dis = dis[][to], no.id = to;
Q.push(no);
no.p = , no.dis = dis[][to], no.id = to;
Q.push(no);
}
else if(dis[][to] == dis[a.p][a.id] + edge[i].w)//最短路长度一样 更新最短路条数即可
{
cnt[][to] += cnt[a.p][a.id];
}
else if(dis[][to] > dis[a.p][a.id] + edge[i].w)//找到一条长度大于最短路但小于当前次短路的路径
{//将这条路变成次短路
dis[][to] = dis[a.p][a.id] + edge[i].w;
cnt[][to] = cnt[a.p][a.id];
no.p = , no.dis = dis[][to], no.id = to;
Q.push(no);
}
else if(dis[][to] == dis[a.p][a.id] + edge[i].w)
{
cnt[][to] += cnt[a.p][a.id];
}
}
}
} int main()
{
int T;
scanf("%d", &T);
while(T --)
{
init();
scanf("%d%d", &n, &m);
for(int i = ; i <= m; i ++)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
int s, t;
scanf("%d%d", &s, &t);
dij(s, t);
int ans = cnt[][t]; //ans代表最短路的条数
if(dis[][t] + == dis[][t])
ans += cnt[][t];
printf("%d\n", ans);
}
return ;
}

HDU 1688 Sightseeing 【输出最短路+次短路条数】的更多相关文章

  1. HDU 1688 Sightseeing&HDU 3191 How Many Paths Are There(Dijkstra变形求次短路条数)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  2. 【最短路】HDU 1688 Sightseeing

    题目大意 给出一个有向图(可能存在重边),求从\(S\)到\(F\)最短路的条数,如果次短路的长度仅比最短路的长度多1,那么再加上次短路的条数. 输入格式 第一行是数据组数\(T\). 对于魅族数据, ...

  3. HDU 1688 Sightseeing

    题目链接:Sightseeing 题意:求最短路和比最短路长度+1的所有路径条数. 附代码:用数组记录最短和次短路径的长度和条数,一次更新,直到没有边可以更新. #include <stdio. ...

  4. HDU 3191 次短路长度和条数

    http://www.cnblogs.com/wally/archive/2013/04/16/3024490.html http://blog.csdn.net/me4546/article/det ...

  5. hdu 1688 Sightseeing (最短路径)

    Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  6. poj 3463/hdu 1688 求次短路和最短路个数

    http://poj.org/problem?id=3463 http://acm.hdu.edu.cn/showproblem.php?pid=1688 求出最短路的条数比最短路大1的次短路的条数和 ...

  7. 2015多校第6场 HDU 5361 并查集,最短路

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5361 题意:有n个点1-n, 每个点到相邻点的距离是1,然后每个点可以通过花费c[i]的钱从i点走到距 ...

  8. HDU 3416 Marriage Match IV (求最短路的条数,最大流)

    Marriage Match IV 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/Q Description Do not si ...

  9. POJ---3463 Sightseeing 记录最短路和次短路的条数

    Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9247   Accepted: 3242 Descr ...

随机推荐

  1. error MSB6006: “cmd.exe”已退出,代码为 3。

    error MSB6006: “cmd.exe”已退出,代码为 3. 这两天调程序遇到一个奇怪的问题. C:\Program Files (x86)\MSBuild\Microsoft.Cpp\v4. ...

  2. JavaScript 运算符的优先级

    ㈠逗号(,)运算符 ⑴使用 , 可以分割多个语句,一般可以在声明多个变量时使用 , : ⑵使用 , 运算符同时声明多个变量    // var a , b , c ; ⑶可以同时声明多个变量并赋值   ...

  3. SpringBoot 测试类 @RunWith & @SpringBootTest

    @RunWith(SpringRunner.class) @SpringBootTest public class RabbitMqTest { @Autowired RabbitMqSender r ...

  4. 51 Nod 1110距离之和最小V3

    1110 距离之和最小 V3 1 秒 131,072 KB 40 分 4 级题 X轴上有N个点,每个点除了包括一个位置数据X[i],还包括一个权值W[i].点P到点P[i]的带权距离 = 实际距离 * ...

  5. 【线性代数】2-4:矩阵操作(Matrix Operations)

    title: [线性代数]2-4:矩阵操作(Matrix Operations) toc: true categories: Mathematic Linear Algebra date: 2017- ...

  6. 【CUDA 基础】6.5 流回调

    title: [CUDA 基础]6.5 流回调 categories: - CUDA - Freshman tags: - 流回调 toc: true date: 2018-06-20 21:56:1 ...

  7. Configure vyatta

    Username: vyatta Password: vyatta 配置网卡: 编辑: configure 内部网络IP地址配置:192.168.0.1 set interfaces ethernet ...

  8. mac 安装laravel

    安装laravel之前先安装composer 使用 curl 指令下载: curl -sS https://getcomposer.org/installer | php 或是沒有安裝 curl ,也 ...

  9. Elasticsearch 部署以及报错解决

    前言 Elasticsearch 是一个非常值得学习和使用的分布式存储 此次部署将采用 centos6.9 一.初步了解 ES 简谈概念 Elasticsearch 是一个开源的高扩展的分布式全文检索 ...

  10. Rocketmq异步发送消息

    package com.bfxy.rocketmq.quickstart; import java.util.List; import org.apache.rocketmq.client.excep ...