HDU 3191 How Many Paths Are There(SPFA)

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 2
0 2 5
0 1 4
1 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-29 07:42:01

HDU 3191 How Many Paths Are There(SPFA)的相关文章

HDU 2722 Here We Go(relians) Again (spfa)

Here We Go(relians) Again Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 1   Accepted Submission(s) : 1 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description The Gorelians

hdu 1507 Uncle Tom&#39;s Inherited Land*(二分)

Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 1853    Accepted Submission(s): 769 Special Judge Problem Description Your old uncle Tom inherited a piece of land fr

HDU 5024 Wang Xifeng&#39;s Little Plot (搜索)

Wang Xifeng's Little Plot Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 157    Accepted Submission(s): 105 Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>)

hdu 4956 Poor Hanamichi BestCoder Round #5(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4956 Poor Hanamichi Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7    Accepted Submission(s): 4 Problem Description Hanamichi is taking part in

HDU 4001 To Miss Our Children Time (动态规划)

To Miss Our Children Time Problem Description Do you remember our children time? When we are children, we are interesting in almost everything around ourselves. A little thing or a simple game will brings us lots of happy time! LLL is a nostalgic boy

POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Description A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they have the same length, and

HDU - 4971 A simple brute force problem. (DP)

Problem Description There's a company with several projects to be done. Finish a project will get you profits. However, there are some technical problems for some specific projects. To solve the problem, the manager will train his employee which may

HDU 2491 Priest John&#39;s Busiest Day(贪心)

题目链接 题意: n场婚礼 给定每场婚礼的开始和结束时间,要求每场婚礼至少举行一半以上(不包括一半) 如果可行输出YES,否则输出NO 思路: 算出每场婚礼的至少要举行的时间t, 最早的结束时间mid 以最早结束时间mid为第一变量,开始时间s为第二变量从小到大排序 用cnt记录举办完每场婚礼的结束时间 分三种情况: ①加参前当婚礼的时间够不一半 ②果加参婚礼时婚礼已开始,结束时间就加上婚礼一半的时光 ③婚礼没开始,结束时间就是婚礼的最早结束时间mid 代码如下: #include<cstdio

HDU 5024 Wang Xifeng&#39;s Little Plot (bfs)

Problem Description <Dream of the Red Chamber>(also <The Story of the Stone>) is one of the Four Great Classical Novels of Chinese literature, and it is commonly regarded as the best one. This novel was created in Qing Dynasty, by Cao Xueqin.