求次短路 经典啊!
来源:互联网 发布:中国矿工 软件 编辑:程序博客网 时间:2024/06/03 19:19
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.
Sample Input
3 3 0 20 2 50 1 41 2 2
Sample Output
6 1
思路是借鉴别人的,感觉不错。
/*题意:和普通的的最短路题目相识,不同的是这次早的不是最短路而是次短路。思路:利用dijkstra(),记录当前状态下的最短路和次短路如果找到了一个更短的路,那么就需要更新原来的最短路和次短路*/#include<iostream>#include<cstdio>#include<string.h>#define maxint 0x7f7f7f7f#define max 60using namespace std;int N,M,S,E;int x,y,w;int map[max][max];int dp[max][2];int dis[max][2];int vis[max][2];void init(){ for(int i=0; i<max; i++) { dis[i][0]=dis[i][1]=maxint; dp[i][0]=dp[i][1]=0; vis[i][0]=vis[i][1]=0; for(int j=0; j<max; j++) map[i][j]=maxint; }}void dijkstra(){ int j,i; dis[S][0]=0; dp[S][0]=1; for(i=0; i<N*2; i++) { int min=maxint; int x=0; int flag; for(j=0; j<N; j++) { if(vis[j][0]==0&&dis[j][0]<min) { min=dis[j][0]; x=j; flag=0; } else if(vis[j][1]==0&&dis[j][1]<min) { min=dis[j][1]; x=j; flag=1; } } // printf("min=%d\n",min); vis[x][flag]=1; if(min==maxint) break; for(j=0; j<N; j++) { if(min+map[x][j]<dis[j][0]) { dis[j][1]=dis[j][0]; dis[j][0]=min+map[x][j]; dp[j][1]=dp[j][0]; dp[j][0]=dp[x][flag]; } else if(min+map[x][j]==dis[j][0]) { dp[j][0]+=dp[x][flag]; } else if(min+map[x][j]<dis[j][1]) { dis[j][1]=min+map[x][j]; dp[j][1]=dp[x][flag]; } else if (min+map[x][j]==dis[j][1]) dp[j][1]+=dp[x][flag]; } }}int main(){ while(scanf("%d%d%d%d",&N,&M,&S,&E)!=EOF) { init(); for(int i=0; i<M; i++) { scanf("%d%d%d",&x,&y,&w); map[x][y]=w; } dijkstra(); printf("%d %d\n",dis[E][1],dp[E][1]); } return 0;}
- 求次短路 经典啊!
- POJ3255_Roadlocks_dijkstra求次短路
- Dijkstra求最短路与次短路
- POJ3255 Roadblocks dij求次短路 [模板]
- Poj 3255(Dijkstra求次短路)
- POJ 3225 Roadblocks 求次短路
- poj-3463-Sightseeing-求次短路
- poj 3255 Roadblocks Dijkstra求次短路
- poj 3255 spfa(求次短路)
- POJ3255-Roadblocks-dijkstra求次短路
- a* 求次短路 《路由选择问题》
- POJ 3255 Roadblocks 求次短路
- poj 3255 求次短路路径
- POJ3255---求次短路(用Dijkstra)
- POJ3463 Sightseeing(dijkstra求最短路+次短路)
- 次短路
- 次短路
- 次短路
- Linux/Unix 新手和专家教程
- VC ADO 数据库操作 SQL语句执行
- C/C++ 笔试、面试题目大汇总
- linux之cut命令
- Nginx源码分析-内存池
- 求次短路 经典啊!
- ARM基础:系统调用与软件中断SWI的实现
- hibernate+spring延迟加载问题
- VS2008中不能添加基类为CButton等类的问题
- 分类回归树 C&R Tree
- 关于几种多线程模型的探讨
- Nginx源码分析-Epoll模块
- void指针
- 名人博客