## How Many PathsAre There

TimeLimit: 2000/1000 MS (Java/Others)    Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 1820    Accepted Submission(s): 646

ProblemDescription

oooccc1 is a Software Engineerwho has to ride to the work place every Monday through Friday. For a longperiod, he went to office with the shortest path because he loves to sleeplate…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? Tiredat all the tasks he got, he got no time to carry it out. As a best friend ofhis, you’re going to help him!
Since oooccc1 is now getting up earlier, he is glad to take thosepaths, which are a little longer than the shortest one. To be precisely, youare going to find all the second shortest paths.
You would be given a directed graph G, together with the startpoint S which stands for oooccc’1 his house and target point E presents hisoffice. And there is no cycle in the graph. Your task is to tell him how longare these paths and how many there are.

Input

There are some cases. Proceed till the endof 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, Sstands 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 startpoint, 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 andcount for those second shortest paths in one line. Separate them with a singlespace.

Sample Input

3 3 0 2

0 2 5

0 1 4

1 2 2

SampleOutput

6 1

Author

ZSTU

Source

Recommend

lcy

（1）   dis[x]+w(x,v)<dis[v]，（找到一条比”最短路“更加短的路）

（2）dis[x]+w(x.v)==dis[v]，（找到最短路）

（3）dis[v]<dis[x]+w(x,v)<dis[v]，（找到一条比”最短“长，比”次短“短的路，更新”次短“），则更新dis[v]和cnt[v]；

（4）dis[x]+w(x,v)==dis[v]（找到次短路），

`#include<bits/stdc++.h>using namespace std;#define INF 0x3f3f3f3f#define M 55struct Edge{    int to,w;};struct node{    int v,dist;//v记录点（每个过程中开始点）    int tag;   //标记是最短路还是次短路    bool friend operator<(const node&a,const node&b)    {        if(a.dist!=b.dist)        return  a.dist>b.dist;        return a.v>b.v;    }};priority_queue<node> Q;vector<Edge>g[M];int dis[M],cnt[M];//dis[]最短路，dis[]次短路，cnt[]最短路个数，dis[]次短路个数bool vis[M];int Dijstra(int s, int e){    dis[s]=0;    cnt[s]=1;    node p,q;    p.dist=0;    p.tag=1;    p.v=s;    Q.push(p);    while(!Q.empty())    {        q=Q.top();        Q.pop();        if(vis[q.v][q.tag]) continue;        vis[q.v][q.tag]=1;        for(int i=0;i<g[q.v].size();i++)        {            int v=g[q.v][i].to;            int w=g[q.v][i].w;            if(!vis[v]&&dis[v]>q.dist+w)//找到一条比”最短路“更加短的路            {                if(dis[v]!=INF)            //就将把“最短“变为”次短“                {                    dis[v]=dis[v];                    cnt[v]=cnt[v];                    p.dist=dis[v];                    p.tag=2;                    p.v=v;                    Q.push(p);                }               dis[v]=q.dist+w;        //更新最短路               cnt[v]=cnt[q.v][q.tag];               p.tag=1;               p.dist=dis[v];               p.v=v;               Q.push(p);            }               else if(!vis[v]&&dis[v]==q.dist+w)  //  找到一条”最短“，更新”最短条数“               {                   cnt[v]+=cnt[q.v][q.tag];               }              else if(!vis[v]&&dis[v]>q.dist+w)    //找到一条比”最短“长，比”次短“短的路，更新”次短“              {                dis[v]=q.dist+w;                cnt[v]=cnt[q.v][q.tag];                p.tag=2;                p.dist=dis[v];                p.v=v;                Q.push(p);              }              else if(!vis[v]&&dis[v]==q.dist+w)  //找到一条”次短“，更新”次短条数“              {                cnt[v]+=cnt[q.v][q.tag];               }            }    }}int main(){   int n,m,s,e;    while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)  {      memset(cnt,0,sizeof(cnt));//初始化      memset(vis,0,sizeof(vis));      for(int i=0;i<M;i++)      {          g[i].clear();          dis[i]=dis[i]=INF;      }      for(int i=1;i<=m;i++)     {        Edge t;        int a,b,c;        scanf("%d%d%d",&a,&b,&c);        t.to=b;        t.w=c;        g[a].push_back(t);      }      Dijstra(s,e);    printf("%d %d\n",dis[e],cnt[e]);  }   return 0;}`