题目描述
一天小明捧着一本世界地图在看,突然小明拿起笔,将他最爱的那些城市标记出来,并且随机的将这些城市中的某些用线段两两连接起来。
小明量出了每条线段的长度,现在小明想知道在这些线段组成的图中任意两个城市之间的最短距离是多少。
输入格式
输入包含多组测试数据。
每组输入第一行为两个正整数n(n<=10)和m(m<=n*(n-1)/2),n表示城市个数,m表示线段个数。
接下来m行,每行输入三个整数a,b和l,表示a市与b市之间存在一条线段,线段长度为l。(a与b不同)
每组最后一行输入两个整数x和y,表示问题:x市与y市之间的最短距离是多少。(x与y不同)
城市标号为1~n,l<=20。
输出格式
对于每组输入,输出x市与y市之间的最短距离,如果x市与y市之间非连通,则输出“No path”。
输入样例 复制
4 4
1 2 4
1 3 1
1 4 1
2 3 1
2 4
输出样例 复制
3
五种算法:
方法一:
dijkstra算法:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010,null = 0x3f3f3f3f;
int n,m,s,d;
int g[N][N];
int dist[N];
bool st[N];
void dikjstra()
{
memset(dist,0x3f,sizeof dist);
memset(st,false,sizeof st);
dist[s]=0;
for(int i=0;i<n;i++)
{
int t=-1;
for(int j=1;j<=n;j++)
if(!st[j] && (t==-1 || dist[t]>dist[j]))
t=j;
for(int j=1;j<=n;j++) dist[j]=min(dist[j],dist[t]+g[t][j]);
st[t]=true;
}
}
int main()
{
while(cin >> n >> m)
{
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b,w;
cin >> a >> b >> w;
g[a][b]=g[b][a]=min(g[a][b],w);
}
cin >> s >> d;
dikjstra();
if(dist[d] == null) puts("No path");
else cout << dist[d] << endl;
}
return 0;
}
方法二:
堆优化版的dijkstra算法:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
typedef pair<int,int> PII;
const int N = 20,null = 0x3f3f3f3f;
int n,m,s,d;
int h[N],w[N*N],e[N*N],ne[N*N],idx;
int dist[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void dikjstra()
{
memset(dist,0x3f,sizeof dist);
memset(st,false,sizeof st);
dist[s]=0;
priority_queue<PII,vector<PII>,greater<PII>> heap;
heap.push({0,s});
while(heap.size())
{
auto t=heap.top();
heap.pop();
int ver=t.second,distance=t.first;
if(st[ver]) continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>w[i]+distance)
{
dist[j]=w[i]+distance;
heap.push({dist[j],j});
}
}
}
}
int main()
{
while(cin >> n >> m)
{
memset(h,-1,sizeof h);
while(m--)
{
int a,b,w;
cin >> a >> b >> w;
add(a,b,w),add(b,a,w);
}
cin >> s >> d;
dikjstra();
if(dist[d] > null/2) puts("No path");
else cout << dist[d] << endl;
}
return 0;
}
方法三:
bellman_ford算法:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 20,null = 0x3f3f3f3f;
int n,m,s,d;
int dist[N];
struct edge
{
int a,b,w;
}edges[N*N];
void bellman_ford()
{
memset(dist,0x3f,sizeof dist);
dist[s]=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<2*m;j++)
{
int a=edges[j].a,b=edges[j].b,w=edges[j].w;
dist[b]=min(dist[b],dist[a]+w);
}
}
}
int main()
{
while(cin >> n >> m)
{
for(int i=0;i<2*m;i+=2)
{
int a,b,w;
cin >> a >> b >> w;
edges[i]={a,b,w};
edges[i+1]={b,a,w};
}
cin >> s >> d;
bellman_ford();
if(dist[d] > null/2) puts("No path");
else cout << dist[d] << endl;
}
return 0;
}
方法四:
spfa算法:
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 20,M = N *N,null = 0x3f3f3f3f;
int n,m,s,d;
int h[N],w[M],e[M],ne[M],idx;
int dist[N];
bool st[N];
void add(int a,int b,int c)
{
e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}
void spfa()
{
memset(st,false,sizeof st);
memset(dist,0x3f,sizeof dist);
dist[s]=0;
queue<int> ql;
ql.push(s);
st[s]=true;
while(ql.size())
{
auto t=ql.front();
ql.pop();
st[t]=false;
for(int i=h[t];i!=-1;i=ne[i])
{
int j=e[i];
if(dist[j]>dist[t]+w[i])
{
dist[j]=dist[t]+w[i];
if(!st[j])
{
st[j]=true;
ql.push(j);
}
}
}
}
}
int main()
{
while(cin >> n >> m)
{
memset(h,-1,sizeof h);
while(m--)
{
int a,b,w;
cin >> a >> b >> w;
add(a,b,w),add(b,a,w);
}
cin >> s >> d;
spfa();
if(dist[d] > null/2) puts("No path");
else cout << dist[d] << endl;
}
return 0;
}
方法五:
floyd算法:
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110,null = 0x3f3f3f3f;
int n,m;
int g[N][N];
void flody()
{
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
}
int main()
{
while(cin >> n >> m)
{
memset(g,0x3f,sizeof g);
while(m--)
{
int a,b,w;
cin >> a >> b >> w;
g[a][b]=g[b][a]=min(g[a][b],w);
}
flody();
int a,b;
cin >> a >> b;
if(g[a][b] == null) puts("No path");
else cout << g[a][b] << endl;
}
return 0;
}