某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
3 3 0 1 1 0 2 3 1 2 1 0 2 3 1 0 1 1 1 2
2 -1
第一种方法:dijkstra:(未优化)
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+5;
const int INF=1e8+1;
typedef long long ll;
int cost[maxn][maxn];
int dis[maxn];
bool vis[maxn];
/***
dijkstra:
1.init cost[][]=INF;
2.scanf cost[][];
3.init dis[],vis[];
do
{
int v=-1;
4.
find_min dis[]==>v;
5.
update dis[];
}while (v!=-1);
****/
int n,m;
void dijkstra(int a)
{
fill(vis,vis+n,0);
fill(dis,dis+n,INF);
int i,j,k;
dis[a]=0;
while (1)
{
int v=-1;
////find_min////
for (i=0;i<n;i++)
{
if (!vis[i]&&(v==-1||dis[v]>dis[i]))
v=i;
}
if(v==-1)
break;
vis[v]=1;
////upadte_min///////
for (i=0;i<n;i++)
{
dis[i]=min(dis[i],dis[v]+cost[v][i]);
}
}
}
int main ()
{
int i,j,k;
int x,y,z;
while (cin>>n>>m)
{
////init///
for (i=0;i<n;i++)
for (j=0;j<n;j++)
cost[i][j]=INF;
////scanf////
for (i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
if (cost[x][y]>z)
cost[x][y]=cost[y][x]=z;
}
int st,ed;
cin>>st>>ed;
dijkstra(st);
if(dis[ed]==INF)
cout<<-1<<endl;
else
cout<<dis[ed]<<endl;
}
return 0;
}
第二种 : dijkstra(优化:优先队列)
#include <bits/stdc++.h>
using namespace std;
const int maxn=1e3+1;
const int INF=1e6+1;
typedef long long ll;
int dist[maxn];
int path[maxn];//上一个位置
typedef pair<int,int>pp;
struct node
{
int to,data;
node(int x,int y): to(x),data(y){}
};
vector<node>g[maxn];
int n,m;
///rememeber : using priority_queue to save time is a tool
/// dist[] is very important.
void dijkstra(int st)
{
fill(dist,dist+n+1,INF);
fill(path,path+n+1,-1);
int i,j,k;
priority_queue<pp,vector<pp>,greater<pp> >q;
// while (!q.empty())
// q.pop();
dist[st]=0;
q.push(pp(0,st)); // 1 is data, 2 is the pos
while (!q.empty())
{
pp p=q.top();
q.pop();
int v=p.second;
/// min
if(p.first>dist[v])
continue;
int l =g[v].size();
for (i=0;i<l;i++)
{
node e =g[v][i];
if(dist[e.to]>dist[v]+e.data)
{
dist[e.to]=dist[v]+e.data;
q.push(pp(dist[e.to],e.to));
path[e.to]=v;//更新上一个位置
}
}
}
}
vector<int>Path;
void get_path(int &ed)
{
for (int i=ed;path[i]!=-1;i=path[i])
{
Path.push_back(path[i]);
}
////翻转路径,原先是反向的
reverse(Path.begin(),Path.end());
}
int main()
{
int i,j,k;
int x,y,z;
while (cin>>n>>m)
{
for (i=0;i<m;i++)
{
scanf("%d%d%d",&x,&y,&z);
g[x].push_back(node(y,z));
g[y].push_back(node(x,z));
}
int st ,ed ;
cin>>st>>ed;
dijkstra(st);
if(dist[ed]==INF)
cout<<-1<<endl;
else
cout<<dist[ed]<<endl;
///init
for (i=0;i<n;i++)
g[i].clear();
Path.clear();
}
return 0;
}
第三种:spfa(可以判断负环)
#include <iostream>
#include<string>
#include <cstdlib>
#include <algorithm>
#include<cstdio>
#include <math.h>
#include <fstream>
#include<map>
#include <queue>
#include <stack>
#include <vector>
using namespace std;
int n,m;
const int INF= 0x3f3f3f3f;
const int maxn =1e3+5;
int Map[maxn][maxn];
bool vis[maxn];
int dist[maxn];
void spfa(int st)
{
queue<int>q;
fill(dist,dist+n,INF);
fill(vis,vis+n,0);
int i, j,k;
dist[st]=0;
vis[st]=1;
q.push(st);
while (!q.empty())
{
int u =q.front();
q.pop();
vis[u]=0;
for (i=0;i<n;i++)
if(dist[i]>dist[u]+Map[u][i])
{
dist[i]=dist[u]+Map[u][i];
q.push(i);
vis[i]=1;
// if ()//判断负环
// {
//
// }
}
}
}
int main ()
{
int i,j ,k;
int x,y,z;
std::ios::sync_with_stdio(0);
cin.tie(0);
while (cin>>n>>m)
{
for (i=0;i<n+1;i++)
for (j=0;j<n+1;j++)
Map[i][j]=INF;
for (i=0;i<m;i++)
{
cin>>x>>y>>z;
if (Map[x][y]>z)
Map[x][y]=Map[y][x]=z;
}
int st,ed;
cin>>st>>ed;
spfa(st);
if (dist[ed]!=INF)
cout<<dist[ed]<<endl;
else
cout<<-1<<endl;
}
return 0;
}
第四中:floyd 算法
算法的核心是 dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]),是否是当前两点之间的路径最短,或是同过其他带你进行的松弛后达到的最短。