hdu 1596 最短路

直接上代码

/*
* hdu1596/win.cpp
* Created on: 2011-12-5
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
typedef double typec;
const int SIZE = 1005;
const typec MAX = 0;
typec map[SIZE][SIZE];
int N;
typec dijistra(int s, int e) {
int i, j, k;
typec mind, D[SIZE];
bool visited[SIZE];
for (i = 0; i < N; i++) {
visited[i] = false;
D[i] = map[s][i];
}
visited[s] = 1;
D[s] = 1;
for (i = 1; i < N; i++) {
mind = MAX;
k = 0;
for (j = 0; j < N; j++) {
if (visited[j]) {
continue;
}
if (D[j] > mind) {
k = j;
mind = D[j];
}
}
visited[k] = true;
for (j = 0; j < N; j++) {
if (!visited[j]) {
if (D[k] * map[k][j] > D[j]) {
D[j] = D[k] * map[k][j];
}
}
}
}
return D[e];
}

int main() {
#ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
#endif
int Q, a, b;
typec ret;
while (scanf("%d", &N) == 1) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
scanf("%lf", &map[i][j]);
}
}
scanf("%d", &Q);
while (Q--) {
scanf("%d%d", &a, &b);
ret = dijistra(a - 1, b - 1);
if(ret <= 0) {
puts("What a pity!");
}else {
printf("%.3f\n", ret);
}
}
}
return 0;
}



你可能感兴趣的:(HDU)