HDU 3191 How Many Paths Are There
2017-06-03 09:05
351 查看
How Many Paths Are There
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1579 Accepted Submission(s): 567
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
[align=left]Sample Input[/align]
3 3 0 2
0 2 5
0 1 4
1 2 2
[align=left]Sample Output[/align]
6 1
题意:求次短路条数,利用dijkstra算法的变形即可求出
2017-01-27 12:00:25
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1579 Accepted Submission(s): 567
[align=left]Problem Description[/align]
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.
[align=left]Input[/align]
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.
[align=left]Output[/align]
For each case,please output the length and count for those second shortest paths in one line. Separate them with a single space.
[align=left]Sample Input[/align]
3 3 0 2
0 2 5
0 1 4
1 2 2
[align=left]Sample Output[/align]
6 1
题意:求次短路条数,利用dijkstra算法的变形即可求出
#include <iostream> #include <queue> #include <cstring> #define inf 0x3fffffff using namespace std; int mapp[55][55]; int n,m,s,e; bool vis[54][2]; int d[55][2],dp[55][2]; void dij(){ d[s][0]=0; dp[s][0]=1; while(true){ int mmax=inf; int v,flag; for(int i=0;i<n;i++){ if(!vis[i][0]&&mmax>d[i][0]){ mmax=d[i][0]; v=i; flag=0; } else if(!vis[i][1]&&mmax>d[i][1]){ mmax=d[i][1]; v=i; flag=1; } } if(v==e&flag==1){ return ; } if(mmax==inf){ return ; } vis[v][flag]=true; for(int i=0;i<n;i++){ if(!vis[i][0]&&d[v][flag]+mapp[v][i]<d[i][0]){//有更短的就向下移动(当前-->最短,最短-->次短,包括条数都要传递 d[i][1]=d[i][0]; d[i][0]=d[v][flag]+mapp[v][i]; dp[i][1]=dp[i][0]; dp[i][0]=dp[v][flag]; } else if(!vis[i][0]&&d[v][flag]+mapp[v][i]==d[i][0]){//更新条数 dp[i][0]+=dp[v][flag]; } else if(!vis[i][1]&&d[v][flag]+mapp[v][i]<d[i][1]){ d[i][1]=d[v][flag]+mapp[v][i]; dp[i][1]=dp[v][flag]; } else if(!vis[i][1]&&d[v][flag]+mapp[v][i]==d[i][1]){ dp[i][1]+=dp[v][flag]; } } } } void init(){ memset(vis,false,sizeof(vis)); memset(dp,0,sizeof(dp)); for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ mapp[i][j]=inf; } d[i][0]=inf; d[i][1]=inf; } } int main(int argc, char const *argv[]){ cin.sync_with_stdio(false); while(cin>>n>>m>>s>>e){ init(); int a,b,c; for(int i=0;i<m;i++){ cin>>a>>b>>c; mapp[a][b]=c; } dij(); cout<<d[e][1]<<" "<<dp[e][1]<<endl; } return 0; }
2017-01-27 12:00:25
相关文章推荐
- HDU 3191 How Many Paths Are There
- HDU 3191 How Many Paths Are There(SPFA)
- 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 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(次短路条数[模板])
- How Many Paths Are There(次最短路)
- hdu3191 How Many Paths Are There(次短路计数dp)