HDU 3191 How Many Paths Are There(SPFA)

来源:互联网 发布:傅园慧用哪个软件直播 编辑:程序博客网 时间:2024/04/29 10:56

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1339    Accepted Submission(s): 468


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.
 

Sample Input
3 3 0 20 2 50 1 41 2 2
 

Sample Output
6 1
 

题意描述:

给出n个点,m条边组成的有向图。求次短路和次短路的个数。


解题思路:

最短路我们可以求,但是次短路怎么求呢?我们可以把记录最短路长度的mincost数组和记录最短路个数的dp数组变成二维的,1表示最短路,2表示次短路。若遇到比最短路还短的路,则把最短路的信息赋值给次短路,再改变最短路的信息。
另外我们更新mincost数组的时候会遇到以下四种情况:
1)从当前点出发到达i的某条路的长度比到达i点的最短路短:把最短路信息赋值给次短路,把这条路的信息赋值给最短路;
2)从当前点出发到达i的某条路的长度与到达i点的最短路的长度一致:把到达当前点且长度最短的种类数加到到达i点的最短路的种类个数上,即dp[i][1]+=dp[now.pos][now.ismin],其中now.pos表示当前点,now.ismin表示是否为最短路;
3)从当前点出发到达i的某条路的长度与到达i点的最短路长,但同时比次短路短:更新次短路的信息;
4) 从当前点出发到达i的某条路的长度与到达i点的最次路的长度一致:与2)类似,只是把dp[i][1]改为dp[i][2]。



参考代码:

#include<stack>#include<queue>#include<cmath>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;const double eps=1e-6;const int INF=0x3f3f3f3f;const int MAXN=52;struct point{    int pos,cost, ismin;    bool operator < (const point &p) const//每次从优先队列中取出集合中最短路最大的点    {        if(p.cost!=cost)            return p.cost<cost;        return p.pos<pos;    }    point(int pos,int cost,int ismin)    {        this->pos=pos;        this->cost=cost;        this->ismin=ismin;    }};int n,m,s,e,mincost[MAXN][3],dp[MAXN][3],edge[MAXN][MAXN];bool used[MAXN][3];void SPFA(){    memset(mincost,INF,sizeof(mincost));    memset(dp,0,sizeof(dp));    memset(used,false,sizeof(used));    priority_queue<point>q;//优先队列    mincost[s][1]=0;    dp[s][1]=1;    q.push(point(s,0,1));    while(!q.empty())    {        point now=q.top();        q.pop();        if(used[now.pos][now.ismin])continue;        used[now.pos][now.ismin]=true;        for(int i=0; i<n; i++)        {            if(edge[now.pos][i]==INF)                continue;            if(!used[i][1]&&now.cost+edge[now.pos][i]<mincost[i][1])//从当前点出发到达i的某条路的长度比到达i点的最短路短            {                mincost[i][2]=mincost[i][1];//更新次短路                dp[i][2]=dp[i][1];                q.push(point(i,mincost[i][2],2));                            mincost[i][1]=now.cost+edge[now.pos][i];//更新最短路                dp[i][1]=dp[now.pos][now.ismin];                q.push(point(i,mincost[i][1],1));            }            else if(!used[i][1]&&now.cost+edge[now.pos][i]==mincost[i][1])//从当前点出发到达i的某条路的长度与到达i点的最短路的长度一致            {                dp[i][1]+=dp[now.pos][now.ismin];            }            else if(!used[i][2]&&now.cost+edge[now.pos][i]<mincost[i][2])//从当前点出发到达i的某条路的长度与到达i点的最短路长,但同时比次短路短            {                mincost[i][2]=now.cost+edge[now.pos][i];                dp[i][2]=dp[now.pos][now.ismin];                q.push(point(i,mincost[i][2],2));            }            else if(!used[i][2]&&now.cost+edge[now.pos][i]==mincost[i][2])//从当前点出发到达i的某条路的长度与到达i点的最次路的长度一致            {                dp[i][2]+=dp[now.pos][now.ismin];            }        }    }}int main(){#ifndef ONLINE_JUDGE    freopen("in.txt","r",stdin);#endif // ONLINE_JUDGE    while(scanf("%d%d%d%d",&n,&m,&s,&e)!=EOF)    {        memset(edge,INF,sizeof(edge));        for(int i=1; i<=m; i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            edge[a][b]=c;//有向图        }        SPFA();        printf("%d %d\n",mincost[e][2],dp[e][2]);    }    return 0;}


2 0
原创粉丝点击