HDU 3191How Many Paths is there (tope sort for short circuit and number of bars)

Source: Internet
Author: User

How many Paths is thereTime 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 have 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 has 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 He, you ' re going-to-help him!
Since OOOCCC1 is now getting up earlier, he's glad to take those paths, which is a little longer than the shortest one. To is precisely, you is going to find all the second shortest paths.
You would is given a directed graph G, together with the start point S which stands for OOOCCC ' 1 he house and target Poin T E presents his office. And there is no cycle in the graph. Your task is to tell him how long was these paths and how many there was.
Inputthere is some cases. Proceed till the end of file.
The first line of all case is three integers n, M, S, E (3 <= N <=, 0 <= S, e <n)
N stands for the nodes in then 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 is marked from 0 to N-1.
Outputfor each case,please output of the length and count for those second shortest paths on one line. Separate them with a single space.

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

Sample Output
6 1

Authorzstu
Sourcehdu 2009-12 Programming Contest

Test instructions: There are n points, M-Bar has a forward edge, forming a direction-free graph. Ask the number of times the short circuit from S point to T Point is how much and how many roads.

#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]); }}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 3191How Many Paths is there (tope sort for short circuit and number of bars)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.