标签:tar space can sizeof std alt end size 邻接表
第一种是不优化的dijiesitela算法,用邻接表输入。
#include<cstdio> const int maxm = 105; const int INF = 1e5 + 5; #include<algorithm> #include<cstring> #include<queue> using namespace std; int mapp[maxm][maxm]; bool vis[maxm]; int dp[maxm]; char ch[maxm]; int v; queue<int> q; void Dijstela(int star, int endd){ int minn; int index; memset(vis, 0, sizeof(vis)); vis[star] = 1; dp[star] = 0; for(int i = 0; i < v; i++){ dp[i] = mapp[star][i]; } for(int i = 0; i < v - 1; i++){ minn = INF; for(int j = 0; j < v; j++) { if(dp[j] < minn && !vis[j])找到最小的 { index = j; minn = dp[j]; } } if(minn == INF) break; //说明这些点的集合与其他点之间没有联通的地方。 q.push(index); vis[index] = 1; for(int j = 0; j < v; j++)更新每个点的最短路径 { if(dp[index] + mapp[index][j] < dp[j] && !vis[j]) dp[j] = dp[index] + mapp[index][j]; } } } int main(){ scanf("%s", ch); memset(dp, 0, sizeof(dp)); v = strlen(ch); for(int i = 0; i < v; i++){ if(i == v - 1) printf("%c\n", ch[i]); else printf("%c ", ch[i]); } for(int i = 0; i < v; i++){ for(int j = 0; j < v; j++){ scanf("%d", &mapp[i][j]); } } for(int i = 0; i < v; i++){ for(int j = 0; j < v; j++){ mapp[i][j] = mapp[i][j] == 0 ? INF : mapp[i][j]; } } q.push(3); Dijstela(3, 0); printf("%d\n", dp[0] == INF ? -1 : dp[0]); while(!q.empty()){ int yuan = q.front(); q.pop(); printf("%c ", yuan + ‘A‘); } return 0; }
标签:tar space can sizeof std alt end size 邻接表
原文地址:https://www.cnblogs.com/downrainsun/p/9735458.html