标签:最短时间 chmod min 科技 bottom 所在地 结束 span title
时间限制:5000/1000 MS(Java / Others)内存限制:32768/32768 K(Java /其他)
提交总数:106773接受提交内容:45898
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 const int inf = 1e9; 7 int mapp[110][110], d[110], n; 8 bool vis[110] = {false}; 9 10 void dij(int s) { 11 fill(d, d + 110, inf); 12 13 for (int i = 1; i <= n; i++){ 14 vis[i]= false; 15 d[i]=mapp[1][i]; 16 } 17 d[s] = 0; 18 for (int i = 0; i < n; i++) { 19 int mn = inf, v = -1; 20 for (int j = 1; j <= n; j++) { 21 if (!vis[j] && d[j] < mn) { 22 v = j; 23 mn = d[j]; 24 } 25 } 26 if (v == -1) 27 return; 28 vis[v] = true; 29 for (int j = 1; j <= n; j++) { 30 if (!vis[j] && d[j] > d[v] + mapp[v][j]) { 31 d[j] = d[v] + mapp[v][j]; 32 } 33 } 34 } 35 } 36 37 int main() { 38 int m, a, b, c; 39 while (cin >> n >> m && n + m) { 40 fill(mapp[0], mapp[0] + 110 * 110, inf); 41 for (int i = 0; i < m; i++) { 42 cin >> a >> b >> c; 43 mapp[a][b] = mapp[b][a] = c; 44 } 45 dij(1); 46 cout << d[n] << endl; 47 } 48 49 return 0; 50 }
弗洛伊德
1 #include <iostream> 2 #include <cstring> 3 #include <algorithm> 4 5 using namespace std; 6 const int inf = 1e9; 7 int mapp[110][110], d[110], n, m;//mapp用来存图,d用来存各个顶点到当前的距离,n为点数 8 bool vis[110] = {false}; 9 10 int main() { 11 while (cin >> n >> m && n + m) { 12 fill(mapp[0], mapp[0] + 110 * 110, inf); 13 fill(d, d + 110, inf); 14 int a, b, c; 15 for (int i = 1; i <= n; i++) { 16 mapp[i][i] = 0; 17 } 18 for (int i = 1; i <= m; i++) { 19 cin >> a >> b >> c; 20 mapp[a][b] = mapp[b][a] = c; 21 } 22 for (int k = 1; k <= n; k++) { 23 for (int i = 1; i <= n; i++) { 24 for (int j = 1; j <= n; j++) 25 mapp[i][j] = min(mapp[i][j], mapp[i][k] + mapp[k][j]); 26 } 27 } 28 cout << mapp[1][n] << endl; 29 } 30 return 0; 31 }
标签:最短时间 chmod min 科技 bottom 所在地 结束 span title
原文地址:https://www.cnblogs.com/T-I-N/p/11216581.html