HDU 3191How Many Paths Are There(TOPE排序 求次短路及条数)

How Many Paths Are There

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1266    Accepted Submission(s): 437

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

Author

ZSTU

Source

HDU 2009-12 Programming Contest

题意:有n个点,m条有向边,构成有向无环图。问从S点到T点次短路是多少及路的条数。

#include<stdio.h>
#include<queue>
#include<string.h>
using namespace std;
const int N = 55;
const int INF = 1<<30;
struct EDG
{
    int to,next,dis;
} edg[N*N];
struct node
{
    int dis,id,mark;
    friend bool operator<(node aa,node bb)
    {
        return aa.dis>bb.dis;
    }
};
int eid,head[N];
int dis[N][2],dp[N][2],in[N];
void addEdg(int u,int v,int d)
{
    edg[eid].to=v;
    edg[eid].dis=d;
    edg[eid].next=head[u];
    head[u]=eid++;
}
void topesort(int s,int n)
{
    queue<int>q;
    for(int i=0; i<n; i++)
        if(in[i]==0&&i!=s)
        {
            q.push(i);
        }
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u]; i!=-1; i=edg[i].next)
        {
            int v=edg[i].to;
            in[v]--;
            if(in[v]==0&&v!=s)
                q.push(v);
        }
    }
    for(int i=0; i<n; i++)
        for(int j=0; j<2; j++)
            dis[i][j]=INF,dp[i][j]=0;

    q.push(s);
    dis[s][0]=0;
    dp[s][0]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int k=0; k<2; k++)
            for(int i=head[u]; i!=-1; i=edg[i].next)
            {
                int v=edg[i].to;
                if(dis[u][k]+edg[i].dis<dis[v][0])
                {
                    if(dis[v][0]!=INF)
                    {
                        dis[v][1]=dis[v][0];
                        dp[v][1]=dp[v][0];
                    }
                    dis[v][0]=dis[u][k]+edg[i].dis;
                    dp[v][0]=dp[u][k];
                }
                else if(dis[u][k]+edg[i].dis==dis[v][0])
                    dp[v][0]+=dp[u][k];
                else if(dis[u][k]+edg[i].dis<dis[v][1])
                {
                    dis[v][1]=dis[u][k]+edg[i].dis;
                    dp[v][1]=dp[u][k];
                }
                else if(dis[u][k]+edg[i].dis==dis[v][1])
                    dp[v][1]+=dp[u][k];
                if(in[v])
                {
                    in[v]--;
                    if(in[v]==0)
                        q.push(v);
                }
            }
    }
}
int main()
{
    int n,m,s,t,u,v,d;
    while(scanf("%d%d%d%d",&n,&m,&s,&t)>0)
    {
        eid=0;
        memset(head,-1,sizeof(head));
        memset(in , 0, sizeof(in));
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&d);
            addEdg(u,v,d);
            in[v]++;
        }
        topesort(s , n);
        printf("%d %d\n",dis[t][1],dp[t][1]);
    }
}

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

时间: 2024-07-31 15:54:32

HDU 3191How Many Paths Are There(TOPE排序 求次短路及条数)的相关文章

HDU 1688 Sightseeing 【输出最短路+次短路条数】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1688 题目大意:给n个点,m条有向边.再给出起点s, 终点t.求出s到t的最短路条数+次短路条数. 思路: 1.最短路和次短路是紧密相连的,在最短路松弛操作中,当我们找到一条更短的路径,也就意味着之前的路径不再是最短路,而成为了次短路,利用这个关系可以实现状态的转移. 2.好久没写优先队列了,都忘记了加个 priority_queue, 这样才能写重载,才能排序. 注释在代码里: 1 #includ

[ACM] hdu 1285 确定比赛名次 (拓扑排序)

确定比赛名次 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10358    Accepted Submission(s): 4046 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直

hdu 1285 确定比赛排名(拓扑排序)

确定比赛名次                                                                         Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)                                                                                        

HDU 1285 确定比赛名次(拓扑排序+优先队列)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 Problem Description 有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行比赛,比赛结束后,裁判委员会要将所有参赛队伍从前往后依次排名,但现在裁判委员会不能直接获得每个队的比赛成绩,只知道每场比赛的结果,即P1赢P2,用P1,P2表示,排名时P1在P2之前.现在请你编程序确定排名. Input 输入有若干组,每组中的第一行为二个数N(1<=N<

HDU 1285 确定比赛名次(拓扑排序模板)

题意还是比较容易理解的,关键要看到后面的:合条件的排名可能不是唯一的,此时要求输出时编号小的队伍在前: 思路:这道题就是拓扑排序的经典应用了,用队列做的考虑优先编号小的出队就可以了. 拓扑排序: 拓扑排序是对有向无回路图(DAG)顶点的一种排序,它使得如果存在从u到v的有向路径,那么满足序列中u在v前. 所以我们的算法可以描述为这样一个过程: 1.找到整个图中所有的度为0的点,将这些点压进队列(栈)中 2.从队列(栈)中取出一点,输出,将该点及它的边删除,找到它所指向的点,如果改点是一个原点(删

hdu 1052(田忌赛马 贪心算法,sort排序)

Tian Ji -- The Horse Racing Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 18155    Accepted Submission(s): 5281 Problem Description Here is a famous story in Chinese history. "That was about

hdu 1811 Rank of Tetris (拓扑排序+并查集)

Rank of Tetris Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4931 Accepted Submission(s): 1359 Problem Description自从Lele开发了Rating系统,他的Tetris事业更是如虎添翼,不久他遍把这个游戏推向了全球. 为了更好的符合那些爱好者的喜好,Lele又想了一个新点子:他

[ACM] hdu 3342 Legal or Not (拓扑排序)

Legal or Not Problem Description ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to excha

HDU 5131 Song Jiang&#39;s rank list(排序)

题意看样例就可以了啊. 简单的二级排序+暴力查询. Song Jiang's rank list Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/Others) Total Submission(s): 264    Accepted Submission(s): 137 Problem Description <Shui Hu Zhuan>,also <Water Margin&