您的位置:首页 > 运维架构

Page Hopping UVA - 821

2017-10-28 10:36 253 查看
一道比较基础的题目,按照Floyd算法就可以解出来了,具体实现见如下代码(注意area[i][i]的情况要特殊判断):

#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<stack>
#include<queue>
#include<map>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<cstring>
#include<sstream>
#include<cstdio>
#include<deque>
#include<functional>
using namespace std;

const int Inf = 0x3f3f3f3f;

class Solve{
public:
int ea, eb;
int area[110][110];
int maxInd;
void Init(){
memset(area, Inf, sizeof(area));
area[ea][eb] = 1;
int a, b;
maxInd = -1;
while (cin >> a >> b){
if (a == 0 && b == 0) break;
area[a][b] = 1;
maxInd = max(maxInd, max(a, b));
}
}

double Deal(){
Init();
int amount = 0;
double sum = 0;
for (int k = 1; k <= maxInd; k++) area[k][k] = 0;
for (int k = 1; k <= maxInd; k++){
for (int i = 1; i <= maxInd; i++){
for (int j = 1; j <= maxInd; j++){
if (area[i][k] < Inf&&area[k][j] < Inf){
area[i][j] = min(area[i][j], area[i][k] + area[k][j]);
}
}
}
}
for (int i = 1; i <= maxInd; i++){
for (int j = 1; j <= maxInd; j++){
if (area[i][j] != Inf&&area[i][j]!=0){
sum += area[i][j];
amount++;
}
}
}
return sum / amount;
}
};

int main(){
Solve a;
int Case = 0;
while (cin >> a.ea >> a.eb){
Case++;
if (a.ea == 0 && a.eb == 0) break;
double res= a.Deal();
cout << "Case " << Case << ": average length between pages = " <<
setprecision(3) << fixed << res<<" clicks" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: