前辈们,为何不用SPFA会错1个数据

P1462 通往奥格瑞玛的道路

sun1yu1jia1 @ 2017-08-12 19:30:02

#include <cstdio>
#include <cstring>
using namespace std;
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#define Clear(x) memset(x, 0, sizeof(x))
struct SRoad {
    int iStart, iEnd;
    long long iLife;
    SRoad * pNext;
};
int iCityTot, iRoadTot;
long long iLifeTot, arrCharge[10010];
SRoad * arrRoad[10010];
SRoad * CreateRoad(int iStart = 0, int iEnd = 0, long long iLife = 0, SRoad * pNext = 0)
{
    SRoad * roadTmp = new SRoad;
    roadTmp->iStart = iStart;
    roadTmp->iEnd = iEnd;
    roadTmp->iLife = iLife;
    roadTmp->pNext = pNext;
    return roadTmp;
}
void AddRoad(int iStart, int iEnd, long long iLife)
{
    if (!arrRoad[iStart])
        arrRoad[iStart] = CreateRoad(iStart, iEnd, iLife);
    else
        arrRoad[iStart] = CreateRoad(iStart, iEnd, iLife, arrRoad[iStart]);
}
int arrResult[10010];
int Search(int iStart, int iLimit)
{
    int & iResult = arrResult[iStart];
    if (iResult || iStart == iCityTot)
        return iResult;
    iResult = 0x3f3f3f3f;
    SRoad * pTmp = arrRoad[iStart];
    while (pTmp)
    {
        if (arrCharge[pTmp->iEnd] <= iLimit)
        {
            int iTmp = Search(pTmp->iEnd, iLimit) + pTmp->iLife;
            iResult = MIN(iResult, iTmp);
        }
        pTmp = pTmp->pNext;
    }
    return iResult;
}
bool Solve(int iLimit)
{
    Clear(arrResult);
    if (arrCharge[1] > iLimit)
        return false;
    else
        return Search(1, iLimit) <= iLifeTot;
}
int main()
{
    int iChargeMax = -1;
    scanf("%d %d %lld", &iCityTot, &iRoadTot, &iLifeTot);
    for (int i = 1; i <= iCityTot; i++)
    {
        scanf("%lld", &arrCharge[i]);
        iChargeMax = MAX(iChargeMax, arrCharge[i]);
    }
    for (int i = 1; i <= iRoadTot; i++)
    {
        int iStart, iEnd; long long iLife;
        scanf("%d %d %lld", &iStart, &iEnd, &iLife);
        AddRoad(iStart, iEnd, iLife);
        AddRoad(iEnd, iStart, iLife);
    }
    int iLeft = 0, iRight = iChargeMax + 1, iMid, iAns = 0;
    while (iLeft + 1 < iRight)
    {
        iMid = iLeft + (iRight - iLeft) / 2;
        if (Solve(iMid))
            iAns = iRight = iMid;
        else
            iLeft = iMid;
    }
    if (iAns)
        printf("%d", iAns);
    else
        printf("AFK");
    return 0;
}

by vani_prcups @ 2017-09-09 13:14:59

好巧 我是用了SPFA错一个点


by OCBun @ 2018-05-17 21:17:30

同问(‘-’)


|