hdu_1385 逆向SPFA求字典序最小最短路
来源:互联网 发布:vue.js 侧边栏导航 编辑:程序博客网 时间:2024/05/23 10:38
唉,被求最小的字典序搞死了,如果用dijkstra很好实现,但是用spfa就只有用逆向的写法了。
#include<iostream>#include<deque>#include<vector>#include<algorithm>#include<list>using namespace std;const int maxn=1<<7;const int inf=0x7fffffff;int a[maxn][maxn];int b[maxn];int d[maxn]; bool hash[maxn]; int pre[maxn]; int N,x,y,temp;int out[maxn];deque<int>q;inline void init(){ for(int i=1;i<=N;i++) { d[i]=inf; pre[i]=inf; hash[i]=false; } d[y]=0; q.clear(); return ;}void spfa(){ q.push_front(y); hash[y]=true; while(!q.empty()) { temp=q.back(); q.pop_back(); for(int i=1;i<=N;i++) { if(-1 == a[i][temp] || i==temp) { continue; } else { if( d[temp] + b[i] + a[i][temp] < d[i] ) { d[i]=d[temp] + b[i] + a[i][temp]; pre[i]=temp; if(hash[i]==false) { q.push_front(i); hash[i]=true; } } else if( d[temp] + b[i] + a[i][temp] == d[i] ) { if(temp < pre[i]) { pre[i]=temp; } } } } hash[temp]=false; } out[0]=x; temp=0; while( pre[out[temp]] != inf ) { out[temp+1]=pre[out[temp]]; temp++; } printf("From %d to %d :\nPath: ",x,y); for(int i=0;i<temp;i++) { printf("%d-->",out[i]); } printf("%d\n",out[temp]); printf("Total cost : %d\n\n",d[x]-b[x]); return ;}int main(){ while(cin>>N && N) { for(int i=1;i<=N;i++) { for(int j=1;j<=N;j++) { cin>>a[i][j]; } } for(int i=1;i<=N;i++) { cin>>b[i]; } while(cin>>x>>y) { if(-1==x && -1==y) { break; } if(x!=y) { init(); spfa(); } else { printf("From %d to %d :\n",x,y); printf("Path: %d\n",x); printf("Total cost : 0\n\n"); } } } return 0;}
- hdu_1385 逆向SPFA求字典序最小最短路
- HDU 3760 Ideal Path 最短路spfa+BFS 字典序最小的最短路
- uva 10537 - The Toll! Revisited (逆向思维+最短路+输出字典序最小路径)
- 字典序最小的最短路
- poj 2516 Minimum Cost(最小费用最大流 spfa算法求最短路)
- POJ2394 dijksta||spfa求最短路
- UVA11374 Airport Express(SPFA求最短路)
- spfa求最短路模板(邻接矩阵)
- HDU1385 【输出字典序最小的最短路】
- hdoj 1385 输出 最短路的最小字典序路径
- hdu 3760 Ideal Path 字典序最小的最短路
- poj 3967 Ideal Path 字典序最小最短路
- hdu 1535 Invitation Cards 最短路spfa+逆向建图
- BZOJ 1001 最短路求最小割
- HDU 5294(Tricks Device-最短路最小割)[Template:SPFA]
- hdu 5889 Barricade【最短路SPFA+最小割--------Dinic】
- [最小割最大流 || 最短路] roadblock Dinic && SPFA + SLE
- 【hdoj 5294】 Tricks Device 【最小割+最短路spfa】
- 关于寄存器
- 对于下载需要积分的看法
- hashmap,hashtabl,hashtree,linkedhashmap区别分析
- Linux --- vim
- 排序算法总结
- hdu_1385 逆向SPFA求字典序最小最短路
- NYOJ 172 小珂的图表
- ftp 服务器——bftpd 在 ubuntu 上的安装和简单配置
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页
- oracle 特殊符号'&'的处理
- Silverlight中DataGrid控件动态生成列并结合DataPager进行分页二
- 解析 Boost 库的文件名
- linux下进制转换的shell脚本
- android获取/更改gps和WIFI状态