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.

Sample Input

  1. 3 3 0 2
  2. 0 2 5
  3. 0 1 4
  4. 1 2 2

Sample Output

  1. 6 1

题解:题目已经说明题意了;我们可以记录最短路和短路的长度及其次数并不断更新他们。每次更新是无非5种情况:比最小值小,等于最小值,大于最小值小于次小值,等于次小值,大于次小值;

AC代码为:

/*

题意:给你N个点M条有向边,开始点s,终点e,求 s到e的次最短路,输出次最短路的长度和条数

*/

#include<bits/stdc++.h>

using namespace std;

#define MAX 1100

#define INF 999999999

struct edge

{

    int from,to,w;

};

vector<edge> edges;

vector<int> G[MAX];

int m,n;

int dis[MAX][2],vis[MAX][2],cnt[MAX][2];

int s,e;

void addedge(int x,int y,int w)

{

    edge a={x,y,w};

    edges.push_back(a);

    G[x].push_back(edges.size()-1);

 

}

void dijkstra(int x,int y)

{

    for(int i=0;i<=n;i++)

    {

        dis[i][0]=dis[i][1]=INF;

        cnt[i][0]=cnt[i][1]=INF;

    }

    dis[x][0]=0;

    cnt[x][0]=1;

    memset(vis,0,sizeof(vis));

    for(int i=1;i<=n*2;i++)

    {

        int min=INF,u=-1,flag;

        for(int j=0;j<n;j++)

        {

            if(!vis[j][0]&&min>dis[j][0])

            {

                min=dis[j][0];

                flag=0;

                u=j;

            }

            else if(!vis[j][1]&&min>dis[j][1])

            {

                min=dis[j][1];

                flag=1;

                u=j;

            }

        }

        if(u==-1) break;

        vis[u][flag]=1;

        

        for(int j=0;j<G[u].size();j++)

        {

            edge v=edges[G[u][j]];

            if(min+v.w<dis[v.to][0])

            {

                dis[v.to][1]=dis[v.to][0];

                cnt[v.to][1]=cnt[v.to][0];

                dis[v.to][0]=min+v.w;

                cnt[v.to][0]=cnt[u][flag];

            }

            else if(min+v.w==dis[v.to][0]) cnt[v.to][0]+=cnt[u][flag];

            else if(min+v.w==dis[v.to][1]) cnt[v.to][1]+=cnt[u][flag];

            else if(min+v.w<dis[v.to][1])

            {

                dis[v.to][1]=min+v.w;

                cnt[v.to][1]=cnt[u][flag];

            }

        }

        

    }

    printf("%d %d\n",dis[e][1],cnt[e][1]);

}

int main()

{

   while(scanf("%d %d %d %d",&n,&m,&s,&e)!=EOF)

   {    

        for(int i=0;i<=n;i++) G[i].clear();

        edges.clear();

        for(int i=1;i<=m;i++)

        {

            int x,y,w;

            scanf("%d %d %d",&x,&y,&w);

            addedge(x,y,w);

        }

        dijkstra(s,e);

   }

   return 0;

}

HDU3191-How many paths are there(次短路的长度及其个数)的更多相关文章

  1. hdu 3191 次短路的长度和个数

    http://acm.hdu.edu.cn/showproblem.php?pid=3191 求次短路的长度和个数 相关分析在这里http://blog.csdn.net/u012774187/art ...

  2. COJ 0579 4020求次短路的长度

    4020求次短路的长度 难度级别:C: 运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 在一个地图上共有N个路口(编号分别为1到N),R条道路( ...

  3. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

  4. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  5. Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  6. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  7. POJ 3463 有向图求次短路的长度及其方法数

    题目大意: 希望求出走出最短路的方法总数,如果次短路只比最短路小1,那也是可取的 输出总的方法数 这里n个点,每个点有最短和次短两种长度 这里采取的是dijkstra的思想,相当于我们可以不断找到更新 ...

  8. HDU 6181:Two Paths(次短路)

    Two Paths Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 153428/153428 K (Java/Others) Total S ...

  9. hdu3191+hdu1688(求最短路和次短路条数,模板)

    hdu3191题意:求出次短路的长度和条数 #include<iostream> #include<cstdio> #include<cstring> #inclu ...

随机推荐

  1. 力扣(LeetCode)翻转字符串里的单词 个人题解

    给定一个字符串,逐个翻转字符串中的每个单词. 示例 1: 输入: "the sky is blue" 输出: "blue is sky the" 示例 2: 输 ...

  2. 缓冲&缓存&对象池概念的理解

    一).缓冲 作用:缓解程序上下层之间的性能差异. 1).当上层组件的性能优于下层组件时加入缓冲机制可以减少上层组件对下 层组件的等待时间. 2).上层组件不需要等待下层组件接收全部数据,即可返回操作, ...

  3. Java程序性能优化之性能概述

    性能的基本概念 一).什么叫程序的性能? 程序运行所需的内存和时间. 二).性能的表现形式: 1).执行速度: 程序的反应是否迅速,响应时间是否足够短. 2).启动时间:程序从运行到可以处理正常业务所 ...

  4. 记一次Pod中java进程内存“异常”消耗

    背景 环境:openshift3.11 开发反映部署在容器中的java应用内存持续增长,只升不降,具体为: java应用部署在容器中,配置的jvm参数为-Xms1024m -Xmx1024m,容器me ...

  5. 【Luogu P4779】dijkstra算法的堆优化

    Luogu P4779 利用堆/优先队列快速取得权值最小的点. 在稠密图中的表现比SPFA要优秀. #include<iostream> #include<cstdio> #i ...

  6. day48天jQuary

    今日内容 jQuery jQuery引入 下载链接:[jQuery官网](https://jquery.com/),首先需要下载这个jQuery的文件,然后在HTML文件中引入这个文件,就可以使用这个 ...

  7. 初探three.js材质

    这节我们浅谈一下THREE的材质.材质就是物体的皮肤,决定物体的表面.THREE的材质有很多种,他们有的和到相机的距离有关,有的和面的法向量角度有关,有的不受光照的影响,有的受到光照的影响会产生反射效 ...

  8. Linux(CentOS7)修改默认yum源为国内的阿里云、网易yum源

    修改方式: echo 备份当前的yum源 mv /etc/yum.repos.d /etc/yum.repos.d.backup4comex echo 新建空的yum源设置目录 mkdir /etc/ ...

  9. Rust中的RefCell和内部可变性

    RefCell Rust在编译阶段会进行严格的借用规则检查,规则如下: 在任意给定时间,要么只能有一个可变引用,要么只能有多个不可变引用. 引用必须总是有效. 即在编译阶段,当有一个不可变值时,不能可 ...

  10. 痞子衡嵌入式:恩智浦i.MX RTxxx系列MCU启动那些事(4)- OTP及其烧写方法

    大家好,我是痞子衡,是正经搞技术的痞子.今天痞子衡给大家介绍的是恩智浦i.MX RTxxx系列MCU的OTP. 在i.MXRTxxx启动系列第二篇文章 Boot配置(ISP Pin, OTP) 里痞子 ...