多源点最短路径
来源:互联网 发布:中兴下载软件应用 编辑:程序博客网 时间:2024/05/16 05:06
给出s个起点,给出t个终点,求出所有起点到终点的最短路中的最短的一个.
构建一个超级源点,与每一个起点相连,权值为0;构建一个超级终点,与每一个终点相连,权值为0。然后求超级源点到超级终点的最短路径。
#include<iostream>#include<queue>using namespace std;int first[1010], dist[1010];int point[202010], weight[202010], next[202010];int q[50000];bool f[1010];int n,m,s,t,tot;void addEdge(int x, int y, int w) //边集数组保存图{ tot++; point[tot] = y; next[tot] = first[x]; first[x] = tot; weight[tot] = w; tot++; point[tot] = x; next[tot] = first[y]; first[y] = tot; weight[tot] = w;}void spfa_slf(){ int h = 0; int t = 0; q[t] = 0; f[0] = true; while (h <= t) { int now = q[h]; h++; f[now] = false; int k = first[now]; while (k != 0) { int New = point[k]; if (dist[now] + weight[k] < dist[New]) { dist[New] = dist[now] + weight[k]; if (!f[New]) { t++; q[t] = New; f[New] = true; } } k = next[k]; } }}int main(){ while (cin>>n>>m>>s>>t && n && m && s && t) { tot = 0; for (int i=1; i<=m; i++) { int x, y, w; cin>>x>>y>>w; addEdge(x,y,w); } for (int i=1; i<=s; i++) //构建超级源点 { int x; cin>>x; addEdge(0,x,0); } for (int i=1; i<=t; i++) //构建超级终点 { int x; cin>>x; addEdge(x,n+1,0); } for (int i=0; i<=n+1; i++) { dist[i] = (1<<30); f[i] = false; } dist[0] = 0; spfa_slf(); //spfa求最短路径 if (dist[n+1] < (1<<30)) cout<<dist[n+1]<<endl; else cout<<"What a pity!"<<endl; for (int i=0; i<=n+1; i++) first[i] = dist[i] = f[i] = 0; for (int i=0; i<=tot; i++) point[i] = next[i] = weight[i] = 0; } return 0;}
0 0
- 多源点最短路径
- 多源点最短路径问题
- 单源点最短路径
- 单源点最短路径
- 单源点最短路径
- 单源点最短路径
- 单源点最短路径
- 单源点最短路径
- 单源点最短路径
- 最短路径多源点Flod-Warshall算法
- 最短路径算法1—多源点Floyd
- 算法基础 - 多源点最短路径(Floyd算法)
- Floyd Warshell 算法求解多源点最短路径
- HDU 2923 Einbahnstrasse(最短路径,多源点到单源点)
- 单源点最短路径----Dijkstra算法
- 单源点最短路径(dijkstra算法)
- Dijkstra求解单源点最短路径
- 单源点的最短路径
- HDU-4081-Qin Shi Huang's National Road System(次小生成树)
- 基础卷_异常篇 第10集 异常--总结
- iOS 硬件 定位 - 前后台
- Linux中的shell
- 在文本框中输入一个大于零的数字
- 多源点最短路径
- linux先进程通信 管道方式和消息队列
- HDU 1242 Rescue(BFS+【优先队列】)
- scala学习第二弹:函数式编写思想
- iOS 硬件 定位 - 前台
- iOS 硬件
- 输入框和div拖动
- 多线程技术在数据实时采集分析中的应用
- 浅谈对S2SH,SSM框架的理解