一道ACM的题目:计算A地到B地的最大人流量

来源:互联网 发布:mac 10.12.6 无法验证 编辑:程序博客网 时间:2024/04/20 21:47
题目如下:

Problem D     Berlin

 

Theadministration of a well-known football team has made a study about the lack ofsupport in international away games. This study  has concluded that the onlyreason for this lack of support is the difficulty in organizing the travelarrangements. To help solving  this  problem, the  administration  has asked  you  to build  a  program that computes  the  maximum number  of  people that  can  fly from  a  departure city  to  a destination city,  using  the  available places  in  regular flights  in  a given  day,  and arriving at or before a given time. Whentraveling from one city to another, a person may  make multiple  transfers.  Each transfer  is,  at least,  30  minutes long,  i.e.,  the departure time  should  be, at  least  30 minutes  after  the arrival  time.  Given a  set  of flights for a single day and the latestarrival time, your program should compute the maximum number of persons thatcan fly, directly or indirectly, from a departure city to a destination city,arriving at or before the latest arrival time.

Input

Thefirst line contains an integer (smaller or equal to 150) indicating the numberof cities that have flight connections. The second line contains a stringindicating the city of departure. The third line contains a string indicatingthe destination city. The fourth line contains the latest arrival time, in theformat HHMM, where HH is the hour in the day (from 00 to 23) and MM is theminute in the hour (from 00 to 59). The fifth line contains an integer N(smaller or equal to 5000), with the number of existing flights. Each of thefollowing N lines contains the info for each flight. Each such line containstwo strings and three integers, separated by blank spaces, O E C D A, where Oand E are, respectively, the origin and destination of a flight, C is thenumber of available places in the flight (from 0 to 300), and D and A are thedeparture and arrival times in the previously defined format HHMM. All flightsstart and end in the same day. City names may have up to 8 characters.

Output

Theoutput consists of one single line with an integer stating the maximum numberof people that can fly from the origin to the destination city, using the givenflights and arriving at or before the given latest arrival time.

Sample Input

4

lisbon

berlin

1500

9

lisbon london6 1000 1100

london lisbon 6 1130 1230

lisbon paris5 1000 1100

paris lisbon 4 1130 1230

london paris 1 1130 1300

london berlin 2 1340 1510

berlinlondon 2 13001430

paris berlin 10 1330 1500

berlinparis 9 13001430

SampleOutput

6


大意:

一个有名的足球队部门作了一项有关于国际运动缺乏支持的研究。研究表明这种缺乏支持的唯一原因是组织行程安排上的困难。为了解决这个问题,该部门要你建立一个计算从起发城市到指定城市最大人流量的程序,在给定的某天,正常的航班,选择可到达的城市,正常到达或者提前到达。当从一个城市到另一个城市,某人可能选择多种行程;每一种行程都至少需要30分钟,也就是说,至少得到达30分钟之后才能重新启程。在某天设置一个航班和最迟可到达的时间,你的程序要计算所飞的最大人数,直接或间接地到达目的地,正常到达或提前到达。

输入:

第一行包含一个整数(小于等于150)表示相互可到达的城市数目,

第二行包含一个字符表示起程地,

第三行包含一个字符表示目的地,

第四行包含最迟可到时间,以HHMM的格式,HH是一天的时间(从0023),MM是一个小时的分钟数(从0059)。

第五行包含一个整数N(小于等于5000),可用的航班的数目,包含每一种航班的信息。每一个这种形式的路线包含两个字符和三个整数,由空白空间分隔,O E C D AOE代表航班的起发地和目的地,C代表飞机中可用的座位(从0300),DA代表先前已定好的离开和到达的时间。所有的航班在同一天起飞和结束。城市名字可达到8个字符。

输出:

要求输出结果中只输出一个整数标明从起发地到目的地的最大人流量,用给定的航班,预期到达或先于最迟到达时间到达。


由于测试时每次输入数据很麻烦,我直接从文件读取了。
程序如下:
// ACM.c : main file
// (c) 2009 wang.xw

#include "acm.h"

int main(int argc, char* argv[])
{
    // initialize linked list
    pgCityHead = Init();
    if (!pgCityHead)
    {
        printf("Memory error./n");
        return 0;
    }

    // get information
    if (!getinfo())
    {
        printf("Input error./n");
        return 0;
    }

    Request();

    if (giShowCitysinfo)
    {
        PrintCity();
        printf("-----------------------------------------------------/n");
    }

    // recursive function
    MoveToDest(gcStartCity, 0, MAXPEOPLENUM);

    // print result
    printf("MAX Number: %d./n", giResultNum);

    // free memory
    FreeCity(pgCityHead);
    printf("Press any key to exit.");
    getchar();
    return 0;
}

pCityNode Init()
{
    pCityNode pMyCityNode;
    pMyCityNode = (pCityNode)malloc(sizeof(CityNode));
    if (pMyCityNode)
    {
        strcpy(pMyCityNode->cCityName, "City");
        pMyCityNode->next = NULL;
        pMyCityNode->TravelInfo = NULL;
    }
    return pMyCityNode;
}

void FreeCity(pCityNode pMyCityNode)
{
    pCityNode pTmpCityNode;

    if (!pMyCityNode)
    {
        return;
    }
    FreeTravel(pMyCityNode->TravelInfo);

    pTmpCityNode = pMyCityNode->next;

    free(pMyCityNode);
    pMyCityNode = NULL;

    if (pTmpCityNode)
    {
        FreeCity(pTmpCityNode);
    }

    return;
}

void FreeTravel(pTravelInfoNode pMyTravelInfoNode)
{
    pTravelInfoNode pTmpTIN;

    if (!pMyTravelInfoNode)
    {
        return;
    }
    pTmpTIN = pMyTravelInfoNode->next;

    free(pMyTravelInfoNode);
    pMyTravelInfoNode = NULL;

    if (pTmpTIN)
    {
        FreeTravel(pTmpTIN);
    }

    return;
}

void Request()
{
    char cTmp;
    printf("To Show Information of the Citys and Flights (Y/N) ?");
    scanf("%c", &cTmp);
    if (cTmp == 'Y' || cTmp == 'y')
    {
        giShowCitysinfo = 1;
    }
    fflush(stdin);

    printf("To Show Information of How to Fly (Y/N) ?");
    scanf("%c", &cTmp);
    if (cTmp == 'Y' || cTmp == 'y')
    {
        giShowHowToFly = 1;
    }
    fflush(stdin);

    system("cls");
    return;
}

// end of ACM.c
// ACM.H : main head file
// (c) 2009 wang.xw

#ifndef _ACM_H_
#define _ACM_H_

#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <stdlib.h>

#define MAXNAMENUM 10
#define MAXPEOPLENUM 32766

typedef struct tagTravelInfoNode
{
    char cDestCity[8];
    int iStartTime;
    int iEndTime;
    int iCapacity;
    struct tagTravelInfoNode *next;
}TravelInfoNode, *pTravelInfoNode;

typedef struct tagInputInfo
{
    char cFirstCity[8];
    char cSecondCity[8];
    int iPeopleNum;
    int iStartTime;
    int iEndTime;
}InputInfo, *pInputInfo;

typedef struct tagCityNode
{
    char cCityName[8];
    struct tagTravelInfoNode *TravelInfo;
    struct tagCityNode *next;
}CityNode, *pCityNode;

pCityNode pgCityHead;
char gcStartCity[MAXNAMENUM];
char gcEndCity[MAXNAMENUM];
int giLatestTime;
int giCityNum;
int giResultNum;
int giShowCitysinfo;
int giShowHowToFly;

#include "getinfo.h"
#include "deal.h"

void Request();
pCityNode Init();
void FreeCity(pCityNode);
void FreeTravel(pTravelInfoNode);

#endif

// end of ACM.H

// deal.c : make city link list,show how to fly
// (c) 2009 wang.xw

#include "deal.h"

int InsertCity(pInputInfo pMyInputInfo)
{
    pCityNode pTmpCityNode;
    pTmpCityNode = pgCityHead->next;
    while (pTmpCityNode)
    {
        if (!strcmp(pTmpCityNode->cCityName, pMyInputInfo->cFirstCity))
        {
            return InsertTravel(pTmpCityNode, pMyInputInfo);
        }
        pTmpCityNode = pTmpCityNode->next;
    }

    pTmpCityNode = (pCityNode)malloc(sizeof(CityNode));
    --giCityNum;
    if (!pTmpCityNode)
    {
        printf("Memory error./n");
        return 0;
    }
    else
    {
        strcpy(pTmpCityNode->cCityName, pMyInputInfo->cFirstCity);
        pTmpCityNode->TravelInfo = NULL;
        pTmpCityNode->next = pgCityHead->next;
        pgCityHead->next = pTmpCityNode;
        return InsertTravel(pTmpCityNode, pMyInputInfo);
    }
}

int InsertTravel(pCityNode pMyCityNode, pInputInfo pMyInputInfo)
{
    int iCnt = 0;
    pTravelInfoNode pMyTravelInfoNode;
    pTravelInfoNode pTmpTIN;
    pTravelInfoNode pPreTIN;

    pMyTravelInfoNode = (pTravelInfoNode)malloc(sizeof(TravelInfoNode));
    if (!pMyTravelInfoNode)
    {
        printf("Memory error./n");
        return 0;
    }

    strcpy(pMyTravelInfoNode->cDestCity, pMyInputInfo->cSecondCity);
    pMyTravelInfoNode->iStartTime = pMyInputInfo->iStartTime;
    pMyTravelInfoNode->iEndTime = pMyInputInfo->iEndTime;
    pMyTravelInfoNode->iCapacity = pMyInputInfo->iPeopleNum;
   
    if (!pMyCityNode->TravelInfo)
    {
        pMyTravelInfoNode->next = NULL;
        pMyCityNode->TravelInfo = pMyTravelInfoNode;
    }
    else
    {
        pTmpTIN = pMyCityNode->TravelInfo;
        pPreTIN = pTmpTIN;
        while (pTmpTIN && pTmpTIN->iStartTime < pMyTravelInfoNode->iStartTime)
        {
            ++iCnt;
            pPreTIN = pTmpTIN;
            pTmpTIN = pTmpTIN->next;
        }
        if (!iCnt)
        {
            pMyTravelInfoNode->next = pMyCityNode->TravelInfo;
            pMyCityNode->TravelInfo = pMyTravelInfoNode;
        }
        else
        {
            pMyTravelInfoNode->next = pPreTIN->next;
            pPreTIN->next = pMyTravelInfoNode;
        }
    }
    return 1;
}

int MoveToDest(char *Local, int iStartTime, int iPeopleNum)
{
    pCityNode pMyCityNode;
    pTravelInfoNode pMyTravelInfoNode;
    int iStartTimeNew;
    int iPeopleNumNew;
    int iResult = 0;

    if (!strcmp(Local, gcEndCity))
    {
        iResult = iPeopleNum;
        giResultNum += iResult;
        return iResult;
    }

    pMyCityNode = FindCity(Local);
    if (!pMyCityNode)
    {
        printf("City Name error./n");
        exit(0);
    }
    pMyTravelInfoNode = pMyCityNode->TravelInfo;

    if (!iStartTime)
    {
        iStartTimeNew = iStartTime;
    }
    else
    {
        if (((iStartTime + 30) % 100) >= 60)
        {
            iStartTimeNew = iStartTime + 70;
        }
        else
        {
            iStartTimeNew = iStartTime + 30;
        }

    }

    while (pMyTravelInfoNode && pMyTravelInfoNode->iCapacity > 0)
    {

        if (iPeopleNum > pMyTravelInfoNode->iCapacity)
        {
            iPeopleNumNew = pMyTravelInfoNode->iCapacity;
        }
        else
        {
            iPeopleNumNew = iPeopleNum;
        }

        if (pMyTravelInfoNode->iStartTime >= iStartTimeNew &&
            pMyTravelInfoNode->iEndTime <= giLatestTime &&
            strcmp(pMyTravelInfoNode->cDestCity, gcStartCity))
        {
            iResult = MoveToDest(pMyTravelInfoNode->cDestCity,
                                 pMyTravelInfoNode->iEndTime,
                                 iPeopleNumNew);
            if (iResult)
            {
                if (giShowHowToFly)
                {
                    printf("From %-8s to %-8s start %-3d end %-3d num %3d /n",
                        Local,
                        pMyTravelInfoNode->cDestCity,
                        pMyTravelInfoNode->iStartTime,
                        pMyTravelInfoNode->iEndTime,
                        iPeopleNumNew);
                    if (!strcmp(Local, gcStartCity))
                    {
                        printf("-----------------------------------------------------/n");
                    }
                }
                pMyTravelInfoNode->iCapacity -= iResult;
                iPeopleNum -= iResult;
            }
        }
        pMyTravelInfoNode = pMyTravelInfoNode->next;
    }
    return iResult;
}

pCityNode FindCity(char *Local)
{
    pCityNode pMyCityNode;
    pMyCityNode = pgCityHead->next;
    while (pMyCityNode)
    {
        if (!strcmp(pMyCityNode->cCityName, Local))
        {
            break;
        }
        pMyCityNode = pMyCityNode->next;
    }
    return pMyCityNode;
}

// end of deal.c

// deal.h
// (c) 2009 wang.xw

#ifndef _DEAL_H_
#define _DEAL_H_

#include "acm.h"

int InsertCity(pInputInfo);
int InsertTravel(pCityNode, pInputInfo);
int MoveToDest(char *Local, int iStartTime, int iPeopleNum);
pCityNode FindCity(char *Local);

#endif

// end of deal.h

// getinfo.c : obtain information from user
// (c) 2009 wang.xw

#include "getinfo.h"

int getinfo()
{
    int iLatestHour;
    int iLatestMinute;
    int iFlightNum;
    int iCnt;
    int iAbortNum = 0;
    pInputInfo pMyInputInfo;
    FILE *pInput;

    pInput = fopen("input.txt", "r");
    if (!pInput)
    {
        printf("File error./n");
        exit(0);
    }

    fscanf(pInput, "%d", &giCityNum);
    fscanf(pInput, "%s", gcStartCity);
    fscanf(pInput, "%s", gcEndCity);
    fscanf(pInput, "%d", &giLatestTime);
    fscanf(pInput, "%d", &iFlightNum);

    iLatestHour = giLatestTime / 100;
    iLatestMinute = giLatestTime % 100;

    if (giCityNum > 150 || giCityNum <= 0)
    {
        printf("City Number error./n");
            return 0;
    }
    if (strlen(gcStartCity) > 8 || strlen(gcEndCity) > 8)
    {
        printf("City Name length error./n");
            return 0;
    }
    if (!(iLatestHour >= 0 && iLatestHour <= 23) || !(iLatestMinute >= 0 && iLatestMinute <= 59))
    {
        printf("Latest Time error./n");
        return 0;
    }
    if (iFlightNum > 5000 || iFlightNum <= 0)
    {
        printf("Flight Number error./n");
        return 0;
    }

    pMyInputInfo = (pInputInfo)malloc(sizeof(InputInfo));
    if (!pMyInputInfo)
    {
        printf("Memory error./n");
        return 0;
    }

    for (iCnt = 0; iCnt < iFlightNum; ++iCnt)
    {
        fscanf(pInput, "%s%s%d%d%d", pMyInputInfo->cFirstCity,
                                     pMyInputInfo->cSecondCity,
                                     &pMyInputInfo->iPeopleNum,
                                     &pMyInputInfo->iStartTime,
                                     &pMyInputInfo->iEndTime);

        if (pMyInputInfo->iPeopleNum > 0 && pMyInputInfo->iPeopleNum <= 300
            && (pMyInputInfo->iStartTime / 100 >= 0 && pMyInputInfo->iStartTime / 100 < 24)
            && (pMyInputInfo->iStartTime % 100 >= 0 && pMyInputInfo->iStartTime % 100 < 60)
            && (pMyInputInfo->iEndTime / 100 >= 0 && pMyInputInfo->iEndTime / 100 < 24)
            && (pMyInputInfo->iEndTime % 100 >= 0 && pMyInputInfo->iEndTime % 100 < 60)
            && pMyInputInfo->iStartTime < pMyInputInfo->iEndTime)
        {
            InsertCity(pMyInputInfo);
        }
        else
        {
            ++iAbortNum;
        }
    }

    if (iAbortNum)
    {
        printf("Abort %d record(s)/n", iAbortNum);
    }
    free(pMyInputInfo);
    pMyInputInfo = NULL;
    fclose(pInput);
    if (giCityNum < 0)
    {
        printf("City Number error./n");
        return 0;
    }
    return 1;
}

void PrintCity()
{
    pCityNode pMyCityNode;
    pMyCityNode = pgCityHead->next;
    while (pMyCityNode)
    {
        printf("City Name : %-8s/n",
            pMyCityNode->cCityName);
        PrintTravel(pMyCityNode->cCityName, pMyCityNode->TravelInfo);
        pMyCityNode = pMyCityNode->next;
    }
    return;
}

void PrintTravel(char *Local, pTravelInfoNode pMyTravelInfoNode)
{
    pTravelInfoNode pTemTravelInfoNode;
    pTemTravelInfoNode = pMyTravelInfoNode;
    while (pTemTravelInfoNode)
    {
        printf("%-8s %-8s %-3d  %-3d  %-6d /n",
            Local,
            pTemTravelInfoNode->cDestCity,
            pTemTravelInfoNode->iStartTime,
            pTemTravelInfoNode->iEndTime,
            pTemTravelInfoNode->iCapacity);
        pTemTravelInfoNode = pTemTravelInfoNode->next;
    }
    return;
}

// end of getinfo.c

// getinfo.H
// (c) 2009 wang.xw

#ifndef _GETINFO_H_
#define _GETINFO_H_

#include "acm.h"

int getinfo();
void PrintCity();
void PrintTravel(char *, pTravelInfoNode);

#endif

// end of getinfo.H


大半天的时间作出来的,很可能有问题,欢迎指出。