哇咔咔,最喜欢这种算法了,算法简单,暴力解决;
可惜数据大点就解决不了问题了;
输入的数据是
第一行第一个数是city的数量n,第二个是路径数t,
接下来n行为a至b的距离
4 8
1 2 2
1 3 6
1 4 4
2 3 3
3 1 7
3 4 1
4 1 5
4 3 12
求第一个city到任意city的最短路径
1 #include<iostream> 2 #include<queue> 3 #include<map> 4 #include<algorithm> 5 #define inf 1000000000 6 using namespace std; 7 int k[1000][1000]; 8 int main() 9 { 10 int n, t; 11 cin >> n >> t; 12 for (int i = 1; i <= n; i++) 13 for (int j = 1; j <= n; j++) 14 if (i == j) k[i][j] = 0; 15 else k[i][j] = inf; 16 for (int i = 0; i < t; i++) 17 { 18 int a, b, c; 19 cin >> a >> b >> c; 20 k[a][b] = c; 21 } 22 for(int i=1;i<=n;i++) 23 for(int j=1;j<=n;j++) 24 for (int m = 1; m <= n; m++) 25 k[j][m] = min(k[j][m] , k[j][i] + k[i][m]); 26 for (int i = 2; i <= n; i++) 27 cout << k[1][i] << endl; 28 return 0; 29 }