Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

为什么老是WA呢???

Posted by tiger2008 at 2008-05-31 22:59:04 on Problem 2075
#include <stdio.h>
#include <string.h>

#define MAX     32768
int main(int argc, char **argv)
{
        float           cables, value, min, result = 0.0;
        int             i, j, k, names, paths, row, col;
        char            buf1[25], buf2[25];

        scanf("%f", &cables);
        getchar();

        scanf("%d", &names);
        getchar();
        int             nodes[names], closest[names];
        char            name[names][25];
        float           lowcost[names], path[names][names];

        memset(nodes, 0, sizeof(nodes));
        memset(lowcost, 0, sizeof(nodes));
        memset(closest, 0, sizeof(nodes));
        memset(name, 0, sizeof(name));
        memset(path, 0, sizeof(path));

        for(i = 0; i < names; i++){
                scanf("%s", name[i]);
                getchar();
        }

        scanf("%d", &paths);
        getchar();
        for(i = 0; i < paths; i++){
                row = col = 0;
                scanf("%s %s %f", buf1, buf2, &value);
                getchar();
                for(j = 0; j < names; j++){
                        if(strcmp(buf1, name[j]) == 0){
                                row = j;
                        }
                        else if(strcmp(buf2, name[j]) == 0){
                                col = j;
                        }
                }
                path[row][col] = path[col][row] = value;
        }

        nodes[0] = 1;
        for(i = 1; i < names; i++){
                lowcost[i] = path[0][i];
                closest[i] = 0;
        }

        for(i = 1; i < names; i++){
                min = MAX;
                j = 0;
                for(k = 1; k < names; k++){
                        if(!nodes[k] && lowcost[k] < min){
                                min = lowcost[k];
                                j = k;
                        }
                }
                nodes[j] = 1;
                result += min;
                for(k = 1; k < names; k++){
                        if(!nodes[k] && path[j][k] && path[j][k] < lowcost[k]){
                                lowcost[k] = path[j][k];
                                closest[k] = j;
                        }
                }
        }

        if(result > cables){
                printf("Not enough cable\n");
        }
        else{
                printf("Need %.1f miles of cable\n", result);
        }

        return 0;
} 

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator