HDU 3191 how many paths are there

Source: Internet
Author: User
How many paths are theretime limit: 1000 msmemory limit: 32768 kbthis problem will be judged on HDU. Original ID: 3191
64-bit integer Io format: % i64d Java class name: Main 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 shoshould have some changes as you cocould see, always riding with the same path is boring.
One day, oooccc1 got an idea! Why cocould 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 gglad 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 wocould 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. inputthere 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. outputfor 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
Solution to sourcehdu 2009-12 Programming Contest: Dijkstra requests Short Circuit
 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <climits> 7 #include <vector> 8 #include <queue> 9 #include <cstdlib>10 #include <string>11 #include <set>12 #include <stack>13 #define LL long long14 #define pii pair<int,int>15 #define INF 0x3f3f3f3f16 using namespace std;17 const int maxn = 55;18 struct arc {19     int to,w,next;20     arc(int x = 0,int y = 0,int z = -1) {21         to = x;22         w = y;23         next = z;24     }25 };26 arc e[10000];27 int head[maxn],d[maxn][2],cnt[maxn][2],tot,big,ed,n,m;28 bool vis[maxn][2];29 void add(int u,int v,int w){30     e[tot] = arc(v,w,head[u]);31     head[u] = tot++;32 }33 void dijkstra(){34     for(int i = 0; i <= n; i++){35         vis[i][0] = vis[i][1] = false;36         cnt[i][0] = cnt[i][1] = 0;37         d[i][0] = d[i][1] = INF;38     }39     d[big][0] = 0;40     cnt[big][0] = 1;41     while(true){42         int u,Min = INF,flag;43         for(int i = 0; i < n; i++){44             if(!vis[i][0] && d[i][0] < Min)45                 Min = d[u = i][flag = 0];46             else if(!vis[i][1] && d[i][1] < Min)47                 Min = d[u = i][flag = 1];48         }49         if(u == ed && flag || Min == INF) break;50         vis[u][flag] = true;51         for(int i = head[u]; ~i; i = e[i].next){52             int v = e[i].to;53             int w = d[u][flag] + e[i].w;54             if(d[v][0] > w){55                 if(d[v][0] < INF){56                     d[v][1] = d[v][0];57                     cnt[v][1] = cnt[v][0];58                 }59                 d[v][0] = w;60                 cnt[v][0] = cnt[u][flag];61             }else if(d[v][0] == w)62                 cnt[v][0] += cnt[u][flag];63             else if(d[v][1] > w){64                 d[v][1] = w;65                 cnt[v][1] = cnt[u][flag];66             }else if(d[v][1] == w) cnt[v][1] += cnt[u][flag];67         }68     }69     printf("%d %d\n",d[ed][1],cnt[ed][1]);70 }71 int main(){72     int u,v,w;73     while(~scanf("%d %d %d %d",&n,&m,&big,&ed)){74         memset(head,-1,sizeof(head));75         for(int i = tot = 0; i < m; i++){76             scanf("%d %d %d",&u,&v,&w);77             add(u,v,w);78         }79         dijkstra();80     }81     return 0;82 }
View code

 

HDU 3191 how many paths are there

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.