文章
111
粉丝
69
获赞
180
访问
66.2k
稠密图用朴素Dijkstra算法,用链式前向星存储图
#include<bits/stdc++.h>
using namespace std;
const int N = 110;
int g[N][N];
int dist[N];
bool st[N];
int n, m;
int dijkstra()
{
for(int i = 2; i <= n; i ++)
dist[i] = INT_MAX / 2;
dist[1] = 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;
if(t == n) break;
st[t] = true;
for(int j = 1; j <= n; j ++)
dist[j] = min(dist[j], dist[t] + g[t][j]);
}
if(dist[n] == INT_MAX / 2) return -1;
return dist[n];
}
int main()
{
while(cin >> n >> m)
{
if(!n && !m) break;
memset(st, 0, sizeof st);
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= n; j ++)
g[i][j] = INT_MAX / 2;
while(m --)
{
int a, b, w;
cin >> a >> b >> w;
g[a][b] = g[b][a] = min(g[a][b]...
登录后发布评论
暂无评论,来抢沙发