这题需要很好的理解Dij。

在Dij的基础上,每个点多一个次短路的长度和数量进行控制。

那么在队列中,最短路控制时出现n次,次短路控制出现n次。注意松弛条件中val值和最短路、次短路的关系。

这题需要了解的是,对vis[i][0]控制确定每个点的最短路长度,对vis[i][1]控制确定每个点的次短路长度。

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
#define MAXN 10010
#define inf 100000000
int T,n,m,s,t,temp;
int head[MAXN],dis[MAXN][2],vis[MAXN][2],num[MAXN][2];
struct Node{
int a;
int kind;
int len;
friend bool operator <(Node a,Node b)
{
return a.len>b.len;
}
}start,v,tem; struct Edge{
int u,len,next;
}edge[2*MAXN]; int Dijstra(int s,int t)
{
priority_queue<Node> que;
memset(vis,0,sizeof(vis));
for(int i=0;i<MAXN;i++)
{
dis[i][0]=inf;
dis[i][1]=inf;
num[i][0]=0;
num[i][1]=0;
}
while(!que.empty())que.pop();
start.a=s;
start.kind=0;
start.len=0;
que.push(start);
dis[s][0]=0;
num[s][0]=1;
while(!que.empty())
{
v=que.top();
que.pop();
if(vis[v.a][v.kind])continue;
vis[v.a][v.kind]=1;
for(int i=head[v.a];i!=-1;i=edge[i].next)
{
int u=edge[i].u;
int len=edge[i].len;
int val=v.len+len;
if(val<dis[u][0])
{
if(dis[u][0]<dis[u][1])
{
dis[u][1]=dis[u][0];
num[u][1]=num[u][0];
tem.a=u;
tem.len=dis[u][1];
tem.kind=1;
que.push(tem);
}
dis[u][0]=val;
num[u][0]=num[v.a][0];
tem.a=u;
tem.len=val;
tem.kind=0;
que.push(tem);
}
else if(val==dis[u][0])
{
num[u][0]+=num[v.a][0];
}
else if(val<dis[u][1])
{
dis[u][1]=val;
num[u][1]=num[v.a][v.kind];
tem.a=u;
tem.kind=1;
tem.len=val;
que.push(tem);
}
else if(val==dis[u][1])
{
num[u][1]+=num[v.a][v.kind];
}
} }
if(dis[t][0]+1==dis[t][1])
return num[t][0]+num[t][1];
else return num[t][0];
} void addEdge(int v,int w,int c)
{
edge[temp].u=w;
edge[temp].len=c;
edge[temp].next=head[v];
head[v]=temp;
temp++;
}
int main()
{
scanf("%d",&T);
while(T--)
{
temp=0;
scanf("%d%d",&n,&m);
memset(head,-1,sizeof(head));
for(int i=0;i<m;i++)
{
int v,w,c;
scanf("%d%d%d",&v,&w,&c);
addEdge(v,w,c);
}
scanf("%d%d",&s,&t);
int ans=Dijstra(s,t);
printf("%d\n",ans);
}
return 0;
}

poj3463 最短路和比最短路长1的路径数的更多相关文章

  1. hdu 1595 find the longest of the shortest【最短路枚举删边求删除每条边后的最短路,并从这些最短路中找出最长的那条】

    find the longest of the shortest Time Limit: 1000/5000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  2. [模板] 次短路 | bzoj1726-[Usaco2006Nov]Roadblocks第二短路

    简介 所谓次短路, 顾名思义, 就是第二短路. :P 1到n的次短路长度必然产生于:1到x的最短路 + edge(x,y) + y到n的最短路 简单证明一下: 设 \(dis(i,j)\) 表示 \( ...

  3. CodeForces - 449B 最短路(迪杰斯特拉+堆优化)判断最短路路径数

    题意: 给出n个点m条公路k条铁路. 接下来m行 u v w      //u->v 距离w 然后k行 v w         //1->v 距离w 如果修建了铁路并不影响两点的最短距离, ...

  4. 最长绝对文件路径——算法面试刷题1(google),字符串处理,使用tree遍历dfs类似思路

    假设我们通过以下的方式用字符串来抽象我们的文件系统: 字符串"dir\n\tsubdir1\n\tsubdir2\n\t\tfile.ext"代表了: dir subdir1 su ...

  5. 最长的文件路径 Longest Absolute File Path

    2018-07-30 22:05:52 问题描述: 问题求解: 本题个人感觉还是挺有意思的,题目要求的是最长的文件路径,其实是需要keep tracking路径长度,如果出现文件则需要进行比较,看是否 ...

  6. The Super Powers UVA 11752 分析分析 求无符号长整形以内的数满足至少可以用两种不同的次方来表示。比如64 = 2^6 = 8^2; 一个数的1次方不算数。

    /** 题目:The Super Powers UVA 11752 链接:https://vjudge.net/contest/154246#problem/Y 题意:求无符号长整形以内的数满足至少可 ...

  7. [luogu] P4551 最长异或路径(贪心)

    P4551 最长异或路径 题目描述 给定一棵\(n\)个点的带权树,结点下标从\(1\)开始到\(N\).寻找树中找两个结点,求最长的异或路径. 异或路径指的是指两个结点之间唯一路径上的所有边权的异或 ...

  8. luoguP4551最长异或路径

    P4551最长异或路径 链接 luogu 思路 从\(1\)开始\(dfs\)求出\(xor\)路径.然后根据性质\(x\)到\(y\)的\(xor\)路径就是\(xo[x]^xo[y]\) 代码 # ...

  9. LeetCode 687. Longest Univalue Path 最长同值路径 (C++/Java)

    题目: Given a binary tree, find the length of the longest path where each node in the path has the sam ...

随机推荐

  1. 测试环境-memcached安装与说明

    一,下载memcached和libevent安装包: 1,Memcache用到了libevent这个库用于Socket的处理,所以还需要安装libevent (这两个包百度上都能找到) 二,安装lib ...

  2. HashMap遍历和使用

    map的几种遍历方式: Map< String, String> map = new HashMap<>(); map.put("aa", "@s ...

  3. CocoaPods的一些略为高级一丁点的使用【转】

    记得我刚开始做iOS开发的时候,是没有项目依赖管理工具.当需要引入第三方库的时候是相当麻烦的,不是直接拷贝库近来,就是添加依赖工程,直到CocoaPods出来才改变这个状况.项目依赖管理不是Cocoa ...

  4. node中使用es6/7/8 --- 支持性与性能

    前言 这几年react.vue的快速发展,越来越多的前端开始讲es6的代码运用在项目中,因为我们可以通过babel进行转译为低版本的js以便于运行在所有浏览器中,import.export.let.箭 ...

  5. RabbitMQ安装以及java使用(一)

    最近闲来无事,整理下基础知识,本次安装 1.RabbitMQ版本是3.6.10 2.操作系统是centOS 7 64位  虚拟机IP:192.168.149.133 1.安装更新系统环境依赖 yum ...

  6. F数圈圈

    Description 幼儿园的小朋友对数字其实不是很感兴趣,他们更感兴趣的是形状,现在给你一个数字,小朋友都会数出其中一共有多少圆圈圈 Input 一个数字n长度不超过19位 Output 输出其中 ...

  7. Centos 7.3 安装mysql5.7.19 各种调试就不多说了

    mysql 5.7.19linux-glibc2.12 (x86_64) 安装 1.在安装目录进行解压2.mv解压目录为mysql3.创建mysql的用户 useradd -s /sbin/nolon ...

  8. jQuery.Gantt

    使用jQuery.Gantt过程中,遇到的问题!

  9. alter system [switch logfile]与[archive log current]的区别

    --alter system [switch logfile]与[archive log current]的区别 ------------------------------------------- ...

  10. Oracle Basic Ready Notes

    1. Select the standard database block size. This is specified at database creation by the DB_BLOCK_S ...