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

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2128    Accepted Submission(s):
749

Problem Description
  oooccc1 is a Software Engineer who has to ride to the
work place every Monday through Friday. For a long period, he went to office
with the shortest path because he loves to sleep late…Time goes by, he find that
he should have some changes as you could see, always riding with the same path
is boring.
  One day, oooccc1 got an idea! Why could I take another path?
Tired at all the tasks he got, he got no time to carry it out. As a best friend
of his, you’re going to help him!
  Since oooccc1 is now getting up earlier,
he is glad to take those paths, which are a little longer than the shortest one.
To be precisely, you are going to find all the second shortest paths.
  You
would be given a directed graph G, together with the start point S which stands
for oooccc’1 his house and target point E presents his office. And there is no
cycle in the graph. Your task is to tell him how long are these paths and how
many there are.
 
Input
There are some cases. Proceed till the end of
file.
The first line of each case is three integers N, M, S, E (3 <= N
<= 50, 0 <= S , E <N)
N stands for the nodes in that graph, M stands
for the number of edges, S stands for the start point, and E stands for the end
point.
Then M lines follows to describe the edges: x y w. x stands for the
start point, and y stands for another point, w stands for the length between x
and y.
All the nodes are marked from 0 to N-1.
 
Output
For each case,please output the length and count for
those second shortest paths in one line. Separate them with a single
space.
题目大意:给定一张有向图,求出起点到终点的次短路条数。
思路:很简单的一道题次短路条数模板题, 但是WA了 看讨论说是不能用优先队列dij,但是我不想改了 上个错误模板代码。
代码如下:
 #include<stdio.h>
#include<string.h>
#include<queue>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = ;
const int MAXM = ;
const int inf = 0x3f3f3f3f;
using namespace std; int n, m, st, ed;
int head[MAXN], cnt;
int dis[][MAXN], num[][MAXN], vis[][MAXN]; struct Edge
{
int to, next, w;
}edge[MAXM]; struct Node
{
int id, dis, p;
bool operator < (const Node &a)const
{
return dis > a.dis;
}
}no; void add(int a, int b, int c)
{
cnt ++;
edge[cnt].to = b;
edge[cnt].w = c;
edge[cnt].next = head[a];
head[a] = cnt;
} void dij()
{
mem(vis, );
priority_queue<Node> Q;
for(int i = ; i < n; i ++)
{
dis[][i] = dis[][i] = inf;
num[][i] = num[][i] = ;
}
dis[][st] = ;
num[][st] = ;
no.p = , no.id = st, no.dis = ;
Q.push(no);
while(!Q.empty())
{
Node a = Q.top();
Q.pop();
if(vis[a.p][a.id])
continue;
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)
{
dis[][to] = dis[][to];
dis[][to] = dis[a.p][a.id] + edge[i].w;
num[][to] = num[][to];
num[][to] = num[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)
num[][to] += num[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;
num[][to] = num[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)
num[][to] += num[a.p][a.id];
}
}
} int main()
{
while(scanf("%d%d%d%d", &n, &m, &st, &ed) != EOF)
{
mem(head, -), cnt = ;
for(int i = ; i <= m; i ++)
{
int a, b, c;//有向图
scanf("%d%d%d", &a, &b, &c);
add(a, b, c);
}
dij();
printf("%d %d\n", dis[][ed], num[][ed]);
}
return ;
}

HDU3191

 

HDU3191 【输出次短路条数】的更多相关文章

  1. HDU 1688 Sightseeing 【输出最短路+次短路条数】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思 ...

  2. 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 ...

  3. poj 3463 Sightseeing(次短路+条数统计)

    /* 对dij的再一次理解 每个点依旧永久标记 只不过这里多搞一维 0 1 表示最短路还是次短路 然后更新次数相当于原来的两倍 更新的时候搞一下就好了 */ #include<iostream& ...

  4. Spark Mllib里如何程序输出数据集的条数(图文详解)

    不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第17章 决策树多元分类UCI Covertype数据集

  5. Linux Find Out Last System Reboot Time and Date Command 登录安全 开关机 记录 帐号审计 历史记录命令条数

    Linux Find Out Last System Reboot Time and Date Command - nixCraft https://www.cyberciti.biz/tips/li ...

  6. HDU1688-POJ3463-Sightseeing(求次短路的条数)

    题意 求出最短路和次短路的条数,当次短路比最短路长度小1时,输出条数之和,反之输出最短路条数. 题解  dis1[],cnt1[],dis2[],cnt2[] 分别表示最短路的长度和条数,次短路的长度 ...

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

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

  8. Hibernate自定义数据库查询(排序、输出条数)

    Hibernate数据库操作类(eg:TexDAO.java) /* * queryString HQL语句,first开始条数, max输出条数 ,norder排序 * 例: List lis = ...

  9. 最短路和次短路的条数(dijstra算法或spfa算法)POJ3463

    http://poj.org/problem?id=3463 Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissio ...

随机推荐

  1. CF55D Beautiful numbers (数位dp)

    题目链接 题解 一个数能被一些数整除,那么一定被这些数的\(lcm\)整除 那么我们容易想到根据\(lcm\)设状态 我们可以发现有用的\(lcm\)只有\(48\)个 那么按照一般的数位\(dp\) ...

  2. Win内核原理与实现学习笔记3-windows系统结构

    1.概述 1.1windows采用了双模式(dual mode)结构来保护操作系统本身,以避免被应用程序的错误而波及.操作系统核心运行在内核模式(kernel mode)下,应用程序的代码运行在用户模 ...

  3. win7虚拟机MAC系统

    http://www.cnblogs.com/xiangshancuizhu/p/3379860.html 结果是一个周末的四分之三整进去还到处不行,然后剩下的四分之一卸载.

  4. 2016"百度之星" - 初赛(Astar Round2A)1005 BD String(HDU5694)——找规律、字符串对称、分治

    分析:按照题目所给的意思每次处理得到的新的字符串都是具有高度对称性的,举个例子,如题目所给的第三个字符串,最中间的是B然后两边分散开去,一边是B的话另外一边关于这个中心对称的那个位置一定是D,反过来同 ...

  5. navicat连接远程数据库报错'client does not support authentication protocol requested by server consider ...'解决方案

    [1.cmd终端连接远程mysql数据库方法] mysql -uhello -pworld   -h192.168.1.88 -P3306 -Dmysql_oa mysql -u用户名 -p密码 -h ...

  6. Java并发指南2:深入理解Java内存模型JMM

    本文转载自互联网,侵删   一:JMM基础与happens-before 并发编程模型的分类 在并发编程中,我们需要处理两个关键问题:线程之间如何通信及线程之间如何同步(这里的线程是指并发执行的活动实 ...

  7. sql注入笔记-mysql

    整理下sql相关知识,查漏补缺(长期更新) 1 常用语句及知识 information_schema包含了大量有用的信息,例如下图 mysql.user下有所有的用户信息,其中authenticati ...

  8. RISC-V riscv64-unknown-elf

    riscv64-unknown-elf 为 RISC-V指令集的交叉编译工具 以下环境在Liunx ubuntu x86_64 环境下进行,下面示例以生成32位文件为目标来操作使用. screen / ...

  9. webpack打包---报错内存溢出javaScript heap out of memory

    今天, npm run build打包时,又报内存溢出了.所以记录一下,之前查了博客有一些解释. “报错CALL_AND_RETRY_LAST Allocation failed - JavaScri ...

  10. maven打包遇到错误,Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.22.1:test

    对Pom文件进行配置(亲自尝试,已成功解决) <build> <plugins> <plugin> <groupId>org.apache.maven. ...