HDU 2544 最短路(floyd+bellman-ford+spfa+dijkstra队列优化)

时间:2023-01-20 09:45:26

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544

题目大意:找点1到点n的最短路(无向图)

练一下最短路。。。

dijkstra+队列优化:

 #include<iostream>
#include<functional>
#include<vector>
#include<queue>
using namespace std;
typedef pair<int, int> p;//first是最短距离,second是顶点编号
const int N = ;
const int INF = << ; struct edge {
int to, cost;//邻接的点,以及到该点的权值
}; vector<edge>eg[N];//邻接表
bool used[N];//表示是否已被使用过
int d[N];//最短距离
int V, E;//顶点数和边数 void dijistra(int s) {
//优先队列,按first从小到大顺序
priority_queue<p, vector<p>, greater<p> >q;
//初始化
for (int i = ; i <= V; i++) {
d[i] = INF;
used[i] = false;
}
d[s] = ; q.push(p(, s));
while (!q.empty()) {
p p1 = q.top();
q.pop();
int v = p1.second;
if (used[v]) continue;
used[v] = true;
for (int i = ; i<eg[v].size(); i++) {
edge e = eg[v][i];
if (d[e.to]>d[v] + e.cost) {
d[e.to] = d[v] + e.cost;
q.push(p(d[e.to], e.to));
}
}
}
} int main() {
while (cin >> V >> E && (V || E)) {
for(int i=;i<=V;i++){
eg[i].clear();
}
for (int i = ; i <= E; i++) {
int a, b, cost;
cin >> a >> b >> cost;
edge g1, g2;
g1.to = b, g2.to = a;
g1.cost = g2.cost = cost;
eg[a].push_back(g1);
eg[b].push_back(g2);
}
dijistra();
cout << d[V] << endl;
}
}

bellman-ford:

 /*
bellman-ford
*/
#include<iostream>
#include<cstring>
using namespace std;
const int N=;
const int INF=<<; struct edge{
int from,to,cost;
}es[N];//边 int d[N];//出发点到i的最短距离
int V,E;//顶点数、边数 //求解从顶点s出发到所有点的最短距离
void shortest_path(int s){
for(int i=;i<=V;i++) d[i]=INF;
d[s]=;
while(true){
bool update=false;
for(int i=;i<=E;i++){
edge e=es[i];
if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost){
d[e.to]=d[e.from]+e.cost;
update=true;
}
//双向
if(d[e.to]!=INF&&d[e.from]>d[e.to]+e.cost){
d[e.from]=d[e.to]+e.cost;
update=true;
} }
if(!update) break;
}
} int main(){
int n,m;
while(cin>>V>>E&&(V||E)){
for(int i=;i<=E;i++){
cin>>es[i].from>>es[i].to>>es[i].cost;
}
shortest_path();
cout<<d[V]<<endl;
}
}

floyd:

 /*floyd*/
#include<iostream>
#include<algorithm>
using namespace std;
const int N=;
const int INF=<<;
int map[N][N];//map[i][j]表示边i~j的距离 int V,E;//顶点数,边数 void floyd(){
for(int k=;k<=V;k++)
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
} int main(){
while(cin>>V>>E&&(V||E)){
for(int i=;i<=V;i++){
for(int j=;j<=V;j++){
if(i==j)
map[i][j]=;
else
map[i][j]=INF;
}
}
for(int i=;i<=E;i++){
int a,b,cost;
cin>>a>>b>>cost;
map[a][b]=map[b][a]=cost;
}
floyd();
cout<<map[][V]<<endl;
}
}

spfa:

 /*spfa*/
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;
const int N=;
const int INF=<<; int map[N][N];
int d[N];//距离起点最小距离
bool used[N];//点是否在队列中
int V,E;//顶点数,边数 //求解从顶点s出发到所有点的最短距离
void spfa(int s){
//初始化
for(int i=;i<=V;i++){
d[i]=INF;
used[i]=false;
}
d[s]=; queue<int>q;
q.push(s);
used[s]=true;
while(!q.empty()){
int k=q.front();
q.pop();
used[k]=false;
//此处实际上可以不用遍历所有点,能够用邻接表优化
for(int i=;i<=V;i++){
if(d[i]>d[k]+map[k][i]){
d[i]=d[k]+map[k][i];
//这个点更新后要入队,要判断是否已经在队列中
if(!used[i]){
q.push(i);
used[i]=true;
}
}
}
}
} int main(){
while(cin>>V>>E&&(V||E)){
//初始化
for(int i=;i<=V;i++)
for(int j=;j<=V;j++)
map[i][j]=(i==j?:INF); for(int i=;i<=E;i++){
int a,b,cost;
cin>>a>>b>>cost;
map[a][b]=map[b][a]=cost;
}
spfa();
cout<<d[V]<<endl;
}
}