3 solutions
-
1
类似模版题
#include<bits/stdc++.h> using namespace std; const int INF=0x3f3f3f3f; const int N=2e2+10; int f[N][N]; int n,m,k; void Floyd(){ for(int k=1;k<=n;k++){ for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ f[i][j]=min(f[i][j],f[i][k]+f[k][j]); } } } } int main(){ cin>>n>>m>>k; for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ f[i][j]=i==j?0:INF; } } int u,v,w; for(int i=1;i<=m;i++){ cin>>u>>v>>w; f[u][v]=min(f[u][v],w); } Floyd(); while(k--){ cin>>u>>v; if(f[u][v]>INF/2){ cout<<"impossible"<<endl; } else{ cout<<f[u][v]<<endl; } } return 0; }
-
0
#include<bits/stdc++.h> using namespace std; const int N = 2e2+10; const int INF = 0x3f3f3f3f; int n,m,k; int f[N][N]; void Floyd(){ for(int k = 1;k <= n; ++k) for(int i = 1;i <= n; ++i) for(int j = 1;j <= n; ++j) f[i][j] = min(f[i][j],f[i][k]+f[k][j]); } int main() { cin>>n>>m>>k; int u,v,w; for(int i = 1;i <= n; ++i) for(int j = 1;j <= n; ++j) f[i][j] = i==j?0:INF; for(int i = 1;i <= m; ++i){ cin>>u>>v>>w; f[u][v] = min(f[u][v],w); } Floyd(); while(k--){ cin>>u>>v; if(f[u][v] > INF / 2) cout<<"impossible"<<endl; else cout<<f[u][v]<<endl; } return 0; }
直接Floyd 不会超时,每次枚举分割点就好了
-
0
重要的事情说三遍:63 63 63 999999999 999999999 999999999
#include <bits/stdc++.h> using namespace std; int dis[250][250]; int n,m,k,f,x,y; int main(){ cin>>n>>m>>f; memset(dis,63,sizeof dis); //cout<<dis[1][1]; for(int i=1;i<=n;i++)dis[i][i]=0; for(int i=1;i<=m;i++){ int u,v,w; cin>>u>>v>>w; dis[u][v]=min(w,dis[u][v]); } for(k=1;k<=n;k++)//中转站0~k for(int i=1;i<=n;i++) //i为起点 for(int j=1;j<=n;j++) //j为终点 if(dis[i][j]>dis[i][k]+dis[k][j])//松弛操作 dis[i][j]=dis[i][k]+dis[k][j]; while(f--){ cin>>x>>y; if(dis[x][y]>999999999) cout<<"impossible"<<endl; else cout<<dis[x][y]<<endl; } return 0; }
- 1
Information
- ID
- 1850
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 1
- Tags
- # Submissions
- 141
- Accepted
- 39
- Uploaded By