HDU3191 【输出次短路条数】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3191
How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2128 Accepted Submission(s):
749
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.
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.
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.
题目大意:给定一张有向图,求出起点到终点的次短路条数。
思路:很简单的一道题次短路条数模板题, 但是WA了 看讨论说是不能用优先队列dij,但是我不想改了 上个错误模板代码。
代码如下:
1 #include<stdio.h> 2 #include<string.h> 3 #include<queue> 4 #define mem(a, b) memset(a, b, sizeof(a)) 5 const int MAXN = 55; 6 const int MAXM = 5000; 7 const int inf = 0x3f3f3f3f; 8 using namespace std; 9 10 int n, m, st, ed; 11 int head[MAXN], cnt; 12 int dis[2][MAXN], num[2][MAXN], vis[2][MAXN]; 13 14 struct Edge 15 { 16 int to, next, w; 17 }edge[MAXM]; 18 19 struct Node 20 { 21 int id, dis, p; 22 bool operator < (const Node &a)const 23 { 24 return dis > a.dis; 25 } 26 }no; 27 28 void add(int a, int b, int c) 29 { 30 cnt ++; 31 edge[cnt].to = b; 32 edge[cnt].w = c; 33 edge[cnt].next = head[a]; 34 head[a] = cnt; 35 } 36 37 void dij() 38 { 39 mem(vis, 0); 40 priority_queue<Node> Q; 41 for(int i = 0; i < n; i ++) 42 { 43 dis[0][i] = dis[1][i] = inf; 44 num[0][i] = num[1][i] = 0; 45 } 46 dis[0][st] = 0; 47 num[0][st] = 1; 48 no.p = 0, no.id = st, no.dis = 0; 49 Q.push(no); 50 while(!Q.empty()) 51 { 52 Node a = Q.top(); 53 Q.pop(); 54 if(vis[a.p][a.id]) 55 continue; 56 vis[a.p][a.id] = 1; 57 for(int i = head[a.id]; i != -1; i = edge[i].next) 58 { 59 int to = edge[i].to; 60 if(dis[0][to] > dis[a.p][a.id] + edge[i].w) 61 { 62 dis[1][to] = dis[0][to]; 63 dis[0][to] = dis[a.p][a.id] + edge[i].w; 64 num[1][to] = num[0][to]; 65 num[0][to] = num[a.p][a.id]; 66 no.p = 0, no.dis = dis[0][to], no.id = to; 67 Q.push(no); 68 no.p = 1, no.dis = dis[1][to], no.id = to; 69 Q.push(no); 70 } 71 else if(dis[0][to] == dis[a.p][a.id] + edge[i].w) 72 num[0][to] += num[a.p][a.id]; 73 else if(dis[1][to] > dis[a.p][a.id] + edge[i].w) 74 { 75 dis[1][to] = dis[a.p][a.id] + edge[i].w; 76 num[1][to] = num[a.p][a.id]; 77 no.p = 1, no.dis = dis[1][to], no.id = to; 78 Q.push(no); 79 } 80 else if(dis[1][to] == dis[a.p][a.id] + edge[i].w) 81 num[1][to] += num[a.p][a.id]; 82 } 83 } 84 } 85 86 int main() 87 { 88 while(scanf("%d%d%d%d", &n, &m, &st, &ed) != EOF) 89 { 90 mem(head, -1), cnt = 0; 91 for(int i = 1; i <= m; i ++) 92 { 93 int a, b, c;//有向图 94 scanf("%d%d%d", &a, &b, &c); 95 add(a, b, c); 96 } 97 dij(); 98 printf("%d %d\n", dis[1][ed], num[1][ed]); 99 } 100 return 0; 101 }