求次短路 经典啊!

来源:互联网 发布:中国矿工 软件 编辑:程序博客网 时间:2024/06/03 19:19

  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 20 2 50 1 41 2 2
 

Sample Output
6 1

思路是借鉴别人的,感觉不错。

/*题意:和普通的的最短路题目相识,不同的是这次早的不是最短路而是次短路。思路:利用dijkstra(),记录当前状态下的最短路和次短路如果找到了一个更短的路,那么就需要更新原来的最短路和次短路*/#include<iostream>#include<cstdio>#include<string.h>#define maxint 0x7f7f7f7f#define max 60using namespace std;int N,M,S,E;int x,y,w;int map[max][max];int dp[max][2];int dis[max][2];int vis[max][2];void init(){    for(int i=0; i<max; i++)    {        dis[i][0]=dis[i][1]=maxint;        dp[i][0]=dp[i][1]=0;        vis[i][0]=vis[i][1]=0;        for(int j=0; j<max; j++)            map[i][j]=maxint;    }}void dijkstra(){    int j,i;    dis[S][0]=0;    dp[S][0]=1;    for(i=0; i<N*2; i++)    {        int min=maxint;        int x=0;        int flag;        for(j=0; j<N; j++)        {            if(vis[j][0]==0&&dis[j][0]<min)            {                min=dis[j][0];                x=j;                flag=0;            }            else if(vis[j][1]==0&&dis[j][1]<min)            {                min=dis[j][1];                x=j;                flag=1;            }        }       // printf("min=%d\n",min);        vis[x][flag]=1;        if(min==maxint)            break;        for(j=0; j<N; j++)        {            if(min+map[x][j]<dis[j][0])            {                dis[j][1]=dis[j][0];                dis[j][0]=min+map[x][j];                dp[j][1]=dp[j][0];                dp[j][0]=dp[x][flag];            }            else if(min+map[x][j]==dis[j][0])            {                dp[j][0]+=dp[x][flag];            }            else if(min+map[x][j]<dis[j][1])            {                dis[j][1]=min+map[x][j];                dp[j][1]=dp[x][flag];            }            else if (min+map[x][j]==dis[j][1])                dp[j][1]+=dp[x][flag];        }    }}int main(){    while(scanf("%d%d%d%d",&N,&M,&S,&E)!=EOF)    {        init();        for(int i=0; i<M; i++)        {            scanf("%d%d%d",&x,&y,&w);            map[x][y]=w;        }        dijkstra();        printf("%d %d\n",dis[E][1],dp[E][1]);    }    return 0;}


原创粉丝点击