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

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

Sample Output

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;

}

最新文章

  1. This task is currently locked by a running workflow and cannot be edited
  2. django模型
  3. easyui添加自定义验证规则
  4. 12-26 tableView的学习心得
  5. canvas绘制直线和多边形基本操作
  6. Oracle 提示密码过期问题:the password will expire
  7. BZOJ 1492 货币兑换Cash
  8. 火星A+B
  9. 转 Android:sp与dp(densityDpi与scaledDensity)
  10. Less合并
  11. 让自定义view宽高成比例显示
  12. 解决TensorFlow程序无限制占用GPU
  13. PHP mkdir新建目录
  14. [BZOJ 3498] [PA 2009] Cakes
  15. CF1139D Steps to One(DP,莫比乌斯反演,质因数分解)
  16. Qt 的坐标系统
  17. 使用promisify解决fs的回调地狱问题
  18. jquery 页面传值 汉字
  19. django models实际操作中遇到的一些问题
  20. wpf窗口阴影

热门文章

  1. [LC]643题 Maximum Average Subarray I(子数组最大平均数 I)
  2. [spark程序]统计人口平均年龄(HDFS文件)(详细过程)
  3. Install gitlab
  4. jdbc-mysql测试例子和源码详解
  5. Redis 4.0鲜为人知的功能将加速您的应用程序
  6. JavaScript笔记六
  7. python 安装resquest
  8. JAVA,Python代码是编译执行还是解释执行?
  9. CTF比赛时准备的一些shell命令
  10. Debug 利器:pstack &amp; strace