| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
不只是G++, GCC也存在f和lf的问题使用%.3f输出就过了。顺便贴代码,堆优化的dijkstra,16MS
#include <stdio.h>
#include <math.h>
#define INF 9999999
#define max(x,y) (x>y)?x:y
typedef struct node {
int x, y;
} node;
node V[200];
int n, heap[201], inheap[200], searched[200], heap_tail;
double G[200][200], D[200];
void init()
{
int i, j;
heap_tail = 0;
for (i = 0; i < n; i++) {
D[i] = INF;
searched[i] = 0;
inheap[i] = 0;
}
}
void insert(int tgt)
{
int i;
for (i = ++heap_tail; D[heap[i/2]] > D[tgt]; i /= 2) {
if (i == 1) {
break;
}
heap[i] = heap[i/2];
} heap[i] = tgt; inheap[tgt] = 1;
}
int pop()
{
int i, child, top = heap[1], bottom = heap[heap_tail--];
inheap[top] = 0;
for (i = 1; i * 2 <= heap_tail; i = child) {
child = i * 2;
if (child != heap_tail && D[heap[child+1]] < D[heap[child]]) {
child ++;
}
if (D[heap[child]] < D[bottom]) {
heap[i] = heap[child];
}
else {
break;
}
}
heap[i] = bottom;
return top;
}
void ascend(int tgt)
{
int i;
for (i = 1; i <= heap_tail; i++) {
if (heap[i] == tgt) {
break;
}
}
for (; D[heap[i/2]] > D[tgt]; i/=2) {
if (i == 1) {
break;
}
heap[i] = heap[i/2];
}
heap[i] = tgt;
}
void dijkstra()
{
int curr, i;
double tmp;
D[0] = 0;
searched[0] = 1;
insert(0);
while (!searched[1] && heap_tail != 0) {
curr = pop();
searched[curr] = 1;
for (i = 0; i < n; i++) {
tmp = max(D[curr], G[curr][i]);
if (searched[i] || D[i] < tmp) {
continue;
}
D[i] = tmp;
if (!inheap[i]) {
insert(i);
}
else {
ascend(i);
}
}
}
}
int main(int argc, const char *argv[])
{
int i, j, scenario = 0;
while (scanf("%d", &n) != EOF && n != 0) {
init();
for (i = 0; i < n; i++) {
scanf("%d %d", &V[i].x, &V[i].y);
}
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
G[i][j] = sqrt(pow(V[i].x-V[j].x, 2) + pow(V[i].y-V[j].y, 2));
}
}
dijkstra();
printf("Scenario #%d\nFrog Distance = %.3f\n\n", ++scenario, D[1]); //使用%.3f输出就可以了
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator