http://acm.timus.ru/problem.aspx?space=1&num=1400
简单floyd 不过让我不明白的是 为什么 c++可以过 但是java却过不了呢 郁闷
代码:
#include<iostream> #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> #include<vector> #include<set> #include<map> #include<string> #include<queue> #include<stack> #include <iomanip> using namespace std; #define LL long long const int INF=0x3f3f3f3f; //priority_queue<int,vector<int>,greater<int> >qt; const int N=505; int dist[N][N]; int main() { //freopen("data.in","r",stdin); int n,m; cin>>n>>m; for(int i=0;i<n;++i) for(int j=0;j<n;++j) dist[i][j]=INF; for(int i=0;i<n;++i) { int l=(i-1+n)%n; int r=(i+1)%n; dist[i][i]=min(dist[i][i], 0); dist[i][l]=min(dist[i][l], 1); dist[i][r]=min(dist[i][l], 1); if(i-m>=0) { dist[i][i-m]=min(dist[i][l], 1); } if(i+m<n) { dist[i][i+m]=min(dist[i][l], 1); } } for(int l=0;l<n;++l){ for(int i=0;i<n;++i){ for(int j=0;j<n;++j){ if(dist[i][j]>dist[i][l]+dist[l][j]){ dist[i][j]=dist[i][l]+dist[l][j]; } } } } double Mean=(double)INF; int k=-1; for(int i=0;i<n;++i){ double tmp=0.0; for(int j=0;j<n;++j){ tmp=tmp+(double)dist[j][i]; } if(tmp/n<Mean){ Mean=tmp/n; k=i; } } printf("Mean = %.2f\n",Mean); for(int i=0;i<n;++i) { cout<<dist[i][k]; if(i==n-1||(i+1)%m==0) cout<<endl; else cout<<" "; } //cin>>n; return 0; }