2 条题解
-
0
# include <bits/stdc++.h> using namespace std; const int N=1e2+5; const int INF=0x3f3f3f3f; int u,v,w,dst[N][N],n,m; int main(){ cin>>n>>m; memset(dst,INF,sizeof(dst)); for(int i = 1;i<=m;i++){ cin>>u>>v>>w; dst[u][v]=min(dst[u][v],w); dst[v][u]=min(dst[v][u],w); } for(int i = 1;i<=n;i++)dst[i][i]=0; for(int k = 1;k<=n;k++) for(int i = 1;i<=n;i++) for(int j = 1;j<=n;j++) dst[i][j]=min(dst[i][j],dst[i][k]+dst[k][j]); for(int i = 1;i<=n;i++){ for(int j = 1;j<=n;j++) cout<<dst[i][j]<<' '; cout<<'\n'; } return 0; } -
0
`#include #include using namespace std;
const int MAXN = 105; const long long INF = 0x3f3f3f3f3f3f3f3fLL;
long long dist[MAXN][MAXN];
int main() { ios::sync_with_stdio(false); cin.tie(0);
int n, m; cin >> n >> m; for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { dist[i][j] = INF; } dist[i][i] = 0; } for (int i = 0; i < m; i++) { int u, v; long long w; cin >> u >> v >> w; if (w < dist[u][v]) { dist[u][v] = w; dist[v][u] = w; } } for (int k = 1; k <= n; k++) { for (int i = 1; i <= n; i++) { if (dist[i][k] == INF) continue; for (int j = 1; j <= n; j++) { if (dist[k][j] == INF) continue; dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]); } } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= n; j++) { cout << dist[i][j]; if (j < n) cout << ' '; } cout << '\n'; } return 0;} `
- 1
信息
- ID
- 3410
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 7
- 标签
- 递交数
- 96
- 已通过
- 19
- 上传者