HDU 3191 How Many Paths Are There
2014-09-29 23:16
387 查看
How Many Paths Are There
Time Limit: 1000msMemory Limit: 32768KB
This 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 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
Source
HDU 2009-12 Programming Contest解题:Dijkstra求次短路
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <vector> #include <queue> #include <cstdlib> #include <string> #include <set> #include <stack> #define LL long long #define pii pair<int,int> #define INF 0x3f3f3f3f using namespace std; const int maxn = 55; struct arc { int to,w,next; arc(int x = 0,int y = 0,int z = -1) { to = x; w = y; next = z; } }; arc e[10000]; int head[maxn],d[maxn][2],cnt[maxn][2],tot,big,ed,n,m; bool vis[maxn][2]; void add(int u,int v,int w){ e[tot] = arc(v,w,head[u]); head[u] = tot++; } void dijkstra(){ for(int i = 0; i <= n; i++){ vis[i][0] = vis[i][1] = false; cnt[i][0] = cnt[i][1] = 0; d[i][0] = d[i][1] = INF; } d[big][0] = 0; cnt[big][0] = 1; while(true){ int u,Min = INF,flag; for(int i = 0; i < n; i++){ if(!vis[i][0] && d[i][0] < Min) Min = d[u = i][flag = 0]; else if(!vis[i][1] && d[i][1] < Min) Min = d[u = i][flag = 1]; } if(u == ed && flag || Min == INF) break; vis[u][flag] = true; for(int i = head[u]; ~i; i = e[i].next){ int v = e[i].to; int w = d[u][flag] + e[i].w; if(d[v][0] > w){ if(d[v][0] < INF){ d[v][1] = d[v][0]; cnt[v][1] = cnt[v][0]; } d[v][0] = w; cnt[v][0] = cnt[u][flag]; }else if(d[v][0] == w) cnt[v][0] += cnt[u][flag]; else if(d[v][1] > w){ d[v][1] = w; cnt[v][1] = cnt[u][flag]; }else if(d[v][1] == w) cnt[v][1] += cnt[u][flag]; } } printf("%d %d\n",d[ed][1],cnt[ed][1]); } int main(){ int u,v,w; while(~scanf("%d %d %d %d",&n,&m,&big,&ed)){ memset(head,-1,sizeof(head)); for(int i = tot = 0; i < m; i++){ scanf("%d %d %d",&u,&v,&w); add(u,v,w); } dijkstra(); } return 0; }
View Code
相关文章推荐
- HDU_3191_How Many Paths Are There(次短路条数)
- hdu 3191 How Many Paths Are There(次短路+条数)
- HDU-3191 How Many Paths Are There(有向图次短路条数)
- 【HDU】3191 How Many Paths Are There 次短路计数
- HDU 3191 次短路 How Many Paths Are There
- hdu 3191 How Many Paths Are There(求次短路径和次短路径条数)
- HDU-3191 How Many Paths Are There 次最短路
- hdu 3191 How Many Paths Are There (次短路径数)
- HDU-3191-How Many Paths Are There
- hdu 3191 How Many Paths Are There(次短路) 很纠结题目。。。
- HDU 3191 How Many Paths Are There
- HDU 3191 How Many Paths Are There(SPFA)
- HDU 3191 How Many Paths Are There 【次短路计数】
- hdu 1688 Sightseeing (次短路及次短路数)&&pku3255 Roadblocks &&3191 How Many Paths Are There
- HDU 3191 How Many Paths Are There
- hdu 3191 How Many Paths Are There
- HDU 3139 How Many Paths Are There
- hdu6181 How Many Paths Are There(次短路条数[模板])
- hdu3191 How Many Paths Are There(次短路计数dp)
- How Many Paths Are There(次最短路)