hdu 3986 Harry Potter and the Final Battle

一个水题WA了60发,数组没开大,这OJ也不提示RE,光提示WA。。。。。。

思路:先求出最短路,如果删除的边不是最短路上的,那么对结果没有影响,要有影响,只能删除最短路上的边。所以枚举一下最短路上的边,每次求最短路即可。

#include<stdio.h>

#include<vector>

#include<string.h>

#include<queue>

#include<algorithm>

using namespace std;



const int maxn = 1000 + 50;

const int INF = 0x7FFFFFFF;

int n, m, anss;

vector<int>ljb[maxn];

vector<int>bbb[maxn][maxn];

int jz[maxn][maxn];

int cost[50000 + 50], flag[50000 + 50], ff[maxn], dist[maxn];

int s[maxn], path[maxn];



void SPFA()

{

    int i, ii;

    queue<int>Q;

    memset(ff, 0, sizeof(ff));

    for (i = 0; i <= n; i++) dist[i] = INF, s[i] = -1;

    ff[1] = 1; Q.push(1); dist[1] = 0; s[1] = 1;

    while (!Q.empty())

    {

        int h = Q.front(); Q.pop(); ff[h] = 0;

        for (i = 0; i < ljb[h].size(); i++)

        {

            for (ii = 0; ii < bbb[h][ljb[h][i]].size(); ii++)

            {

                if (flag[bbb[h][ljb[h][i]][ii]] ==0)

                {

                    if (dist[h] + cost[bbb[h][ljb[h][i]][ii]] < dist[ljb[h][i]])

                    {

                        dist[ljb[h][i]] = dist[h] + cost[bbb[h][ljb[h][i]][ii]];

                        s[ljb[h][i]] = h;

                        if (ff[ljb[h][i]] == 0)

                        {

                            ff[ljb[h][i]] = 1;

                            Q.push(ljb[h][i]);

                        }

                    }

                }

            }

        }

    }

}



int main()

{

    int sb;

    scanf("%d", &sb);

    while (sb--)

    {

        int i, j, u, v;

        scanf("%d%d", &n, &m);

        memset(flag, 0, sizeof(flag));

        for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) jz[i][j] = INF;

        for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) bbb[i][j].clear();

        for (i = 0; i <= n; i++) ljb[i].clear();

        for (i = 1; i <= m; i++)

        {

            scanf("%d%d%d", &u, &v, &cost[i]);

            if (jz[u][v] == INF) jz[u][v] = i, jz[v][u] = i;

            if (cost[i] < cost[jz[u][v]]) jz[u][v] = i, jz[v][u] = i;

            bbb[u][v].push_back(i);

            bbb[v][u].push_back(i);

            ljb[u].push_back(v);

            ljb[v].push_back(u);

        }

        SPFA();

        anss = -1;

        if (dist[n] != INF)

        {

            path[1] = n; int q = 2;

            while (1)

            {

                path[q] = s[path[q - 1]];

                if (path[q] == 1) break;

                q++;

            }

            for (i = 1; i <= q - 1; i++)

            {

                flag[jz[path[i]][path[i + 1]]] = 1;

                SPFA();

                if (dist[n] == INF){ anss = -1; break; }

                if (dist[n] > anss) anss = dist[n];

                flag[jz[path[i]][path[i + 1]]] = 0;

            }

        }

        printf("%d\n", anss);

    }

    return 0;

}

 

你可能感兴趣的:(final)