Dijkstra 算法:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int n,m;
int ave[105][105],dis[105],s[105];
void dijkstra(int start)
{
for(int i = 1;i <= n;i++){
dis[i] = ave[start][i];
if(i == start) s[i] = 0;
else s[i] = 1;
}
dis[start] = 0;
int num = 1;
while(num < n){
int k,ans;
k = ans = inf;
for(int i = 1; i <= n;i++){
if(s[i] && dis[i] < ans) {
ans = dis[i];
k = i;
}
}
for(int i = 1;i <= n;i++){
if(s[i] && ans + ave[k][i] < dis[i] ){
dis[i] = ans + ave[k][i];
}
}
s[k] = 0;
num++;
}
}
int main()
{ int a,b,c;
while(cin>>n>>m&&n){
memset(ave,inf,sizeof(ave));
for(int i = 0 ;i < m ;i ++){
cin>>a>>b>>c;
ave[a][b] = ave[b][a] = c;
}
dijkstra(1);
cout<<dis[n]<<endl;
}
return 0;
}
可得模板为:
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define inf 0x3f3f3f3f
int n,m;
int ave[105][105],dis[105],s[105],path[105];
void dijkstra(int start)
{
for(int i = 1;i <= n;i++){
dis[i] = ave[start][i];
if(i == start) s[i] = 0;
else s[i] = 1;
if(ave[start][i] == inf) path[i] = -1;
else path[i] = start;
}
dis[start] = 0;
int num = 1;
while(num < n){
int k,ans;
k = ans = inf;
for(int i = 1; i <= n;i++){
if(s[i] && dis[i] < ans) {
ans = dis[i];
k = i;
}
}
for(int i = 1;i <= n;i++){
if(s[i] && ans + ave[k][i] < dis[i] ){
dis[i] = ans + ave[k][i];
path[i] = k;
}
}
s[k] = 0;
num++;
}
}
int main()
{ int a,b,c;
while(cin>>n>>m&&n){
memset(ave,inf,sizeof(ave));
for(int i = 0 ;i < m ;i ++){
cin>>a>>b>>c;
ave[a][b] = ave[b][a] = c;
}
dijkstra(1);
cout<<dis[n]<<endl;
int f = 0,p[105];
while(n != 1){
p[f++] = n;
n = path[n];
}
p[f] = 1;
cout<<"路径为:";
for(int i = f;i >= 0;i--){
cout<<p[i]<<" ";
}
cout<<endl;
}
return 0;
}
floyed算法:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
int n,m,ave[105][105];
void floyd()
{
int k,i,j;
for(k = 1; k <= n;k++){
for(i = 1 ;i <=n ;i++){
for(j = 1;j <= n; j++){
if(ave[i][j]>ave[i][k]+ave[k][j])
ave[i][j] = ave[i][k]+ave[k][j];
}
}
}
}
int main()
{
int a,b,c;
while(cin>>n>>m&&n){
memset(ave,inf,sizeof(ave));
for(int i = 0 ;i < m ;i ++){
cin>>a>>b>>c;
ave[a][b] = ave[b][a] = c;
}
floyd();
cout<<ave[1][n]<<endl;
}
return 0;
}
spfa算法:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<cstdlib>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define inf 0x3f3f3f3f
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
int n,m,ave[105][105],dis[105],flag[105];
queue<int>q;
void spfa(int start){
mem(flag,0);
mem(dis,inf);
dis[start] = 0;
q.push(start);
flag[start] = 1;
while(!q.empty()){
int x = q.front();
q.pop();
flag[x] = 0;
for(int i = 1;i <= n ;i++){
if(dis[i] > dis[x] + ave[x][i]) {
dis[i] = dis[x] + ave[x][i];
if(!flag[i]){
q.push(i);
flag[i] = 1;
}
}
}
}
}
int main()
{ int a,b,c;
while(cin>>n>>m&&n){
memset(ave,inf,sizeof(ave));
for(int i = 1; i <= m;i++){
cin>>a>>b>>c;
ave[a][b] = ave[b][a] = c;
}
spfa(1);
cout<<dis[n]<<endl;
}
return 0;
}
可得模板为:
int SPFA(int sour, int g[][501]) // sour 源点 ; g :图
{
bool flag[501]; //是否在队列
int dist[501]; //距离
int u[501]; //点的入队次数
queue<int> q;
memset(flag,0,sizeof(flag));
memset(pre,-1,sizeof(pre));
memset(u,0,sizeof(u));
for(int i = 0; i < n; i++)
dist[i] = INIF;
q.push(sour);
u[sour]++;
flag[sour] = true;
dist[sour] = 0;
while(!q.empty())
{
int v1,v2;
v1 = q.front();
q.pop();
flag[v1] = false;
for(int i = 0; i < n; i++)
{
v2 = i;
if(dist[v2] > dist[v1] + g[v1][v2])
{
dist[v2] = dist[v1] + g[v1][v2];
if(!flag[v2])
{
q.push(v2);
u[v2]++;
if(u[v2] > n)
break;
flag[v2] = true;
pre[v2] = v1;
}
}
}
}
return dist[din];
}
模板转载于https://blog.csdn.net/weixin_44368514/article/details/104300597