无法为spoj MMINPAID找到错误的答案测试用例

时间:2015-08-09 14:07:19

标签: algorithm depth-first-search breadth-first-search

问题链接:http://www.spoj.com/problems/MMINPAID/

获取WA提交。 我已经使用bfs到达第N个节点并使用位掩码计算路径中节点的最小成本。但是,在运行大量测试用例并与已接受的解决方案进行比较后,无法找到失败的测试用例。 代码:

#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>

using namespace std;

const int MAXN = 15, INF = 1 << 29;

struct node {
    int c, p, r;
};

struct node data[MAXN][MAXN];
vector<int> g[MAXN];
int N, M, dist[MAXN][1 << 11];

int bfs() {
    for (int i = 0; i < MAXN; i++)
        for (int k = 0; k < (1 << 11); k++) 
            dist[i][k] = INF;
    queue< pair< pair <int, int> , int > > q;
    int v = 1, path = (1 << 1), cost = 0;
    dist[v][path] = 0;
    q.push(make_pair(make_pair(v, path), cost));
    while (!q.empty()) {
        int curv = q.front().first.first;
        int curpath = q.front().first.second;
        int curcost = q.front().second;
        q.pop();

        for (int i = 0; i < g[curv].size(); i++) {
            int nv = g[curv][i];
            int d1 = curcost + data[curv][nv].r;
            int d2 = INF;
            if (curpath & (1 << data[curv][nv].c)) {
                d2 = curcost + data[curv][nv].p;
            }
            int d3 = min(d1, d2);
            int npath = curpath | (1 << nv);
            if (d3 < dist[nv][npath]) {
                dist[nv][npath] = d3;
                q.push(make_pair(make_pair(nv, npath), d3));
            }
        }
    }
    int res = INF;
    for (int i = 0; i < (1 << 11); i++) {
        res = min(res, dist[N][i]);
    }
    return res;
}

int main() {
    scanf("%d %d", &N, &M);
    for (int i = 0; i < M; i++) {
        int a, b, c, p, r;
        scanf("%d %d %d %d %d", &a, &b, &c, &p, &r);
        g[a].push_back(b);
        data[a][b] = (struct node) {c, p, r};
    }
    int ret = bfs();
    if (ret == INF) printf("impossible\n");
    else printf("%d\n", ret);

    return 0;
}

1 个答案:

答案 0 :(得分:3)

我认为问题可能是您的invalid calling object结构假定从a到b最多只有一条道路。

然而,问题是:

  

可能有多条道路连接一个城市与另一个城市。