题目链接: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. JS BOM基础 全局对象 window location history screen navigator

    全局变量声明的两种方式:1,window.变量名=值;2,var 变量名=值; 全局函数声明的两种方式:1,window.函数名=function(){}2,function 函数名=function ...

  2. select([[data],fn])

    select([[data],fn]) 概述 当 textarea 或文本类型的 input 元素中的文本被选择时,会发生 select 事件.大理石平台生产厂 这个函数会调用执行绑定到select事 ...

  3. dubbo配置文件

    <dubbo:service/> 服务配置,用于暴露一个服务,定义服务的元信息,一个服务可以用多个协议暴露,一个服务也可以注册到多个注册中心.<dubbo:reference/> ...

  4. Oracle 物理结构(三) 文件-参数文件

    一.参数文件介绍 Oracle中的参数文件是一个包含一系列参数以及参数对应值的操作系统文件.它们是在数据库实例启动时候加载的, 决定了数据库的物理 结构.内存.数据库的限制及系统大量的默认值.数据库的 ...

  5. 添加tag

    创建tag git tag -a V1 -m 'release 1' 创建了本地一个版本v1,同时添加注释 release 1 查看tag git tag 显示注释 git show V1 本地tag ...

  6. LibreOJ #110. 乘法逆元

    二次联通门 : LibreOJ #110. 乘法逆元 /* LibreOJ #110. 乘法逆元 求一个数在模意义下的所有逆元 */ #include <cstdio> void read ...

  7. HTML标签---学习笔记

    第一章 HTML标准结构学习: 顶层标签:html 投标签:head 主题标签:boby <html> <head> <meta charset="utf-8& ...

  8. Flask 生成下载文件

    1 后台程序直接生成文件内容 from flask import make_response @app.route('/testdownload', methods=['GET']) def test ...

  9. 【java中的final关键字】

    转自:https://www.cnblogs.com/xiaoxi/p/6392154.html 一.final关键字的基本用法 在Java中,final关键字可以用来修饰类.方法和变量(包括成员变量 ...

  10. flask 自定义转换器

    from flask import Flask from flask import url_for from flask import redirect from werkzeug.routing i ...