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;
}
#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;
}
相关文章推荐
- Uva 821 - Page Hopping
- UVa 821 Page Hopping【Floyd】
- UVA 821 Page Hopping 网页跳跃(BFS,简单)
- UVA 821 Page Hopping——最短路
- 【Uva 821】Page Hopping 翻译+题解
- 821 - Page Hopping (Floyd)
- UVA Page Hopping(Floyd算法)
- UVa #821 Page Hopping , UVa #820 Internet Bandwidth (习题11-1,11-3)
- UVA 10801 Lift Hopping
- UVA 821 Page Hopping (Floyd)
- UVA - 821 - Page Hopping(floyd算法)
- uva 10801 Lift Hopping
- 习题11-7 UVa 10801 Lift Hopping SPFA最短路
- UVA 821 - Page Hopping (flody应用)
- UVa 821 网页跳跃(Floyd)
- Lift Hopping - UVa 10801 Floyd
- [floyd]UVa821 - Page Hopping
- uva821 Page Hopping( Floyd )
- UVA821 PageHopping (Floyd)
- uva 821 floyd