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

spfa 0ms

Posted by WalX at 2016-05-28 16:04:43 on Problem 1062
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>

using namespace std;

const int maxn = 100 + 10;
const int inf = 1000000 + 10;

struct Edge
{
    int to;
    int cost;
    
    Edge(int t, int c)
        :to(t), cost(c) {}
};

int lev[maxn];
vector<Edge> G[maxn];
int d[maxn];
bool vis[maxn];

void spfa(int n, int start, int end)
{
    queue<int> que;
    fill(d, d + n + 1, inf);
    memset(vis + 1, false, n);
    d[0] = 0;
    que.push(0);
    vis[0] = true;
    
    while (!que.empty()) {
        int v = que.front();
        que.pop();
        vis[v] = false;
        for (int u = 0; u < G[v].size(); u++) {
            Edge e = G[v][u];
            if (lev[e.to] >= start && lev[e.to] <= end) {
                if (d[v] + e.cost < d[e.to]) {
                    d[e.to] = d[v] + e.cost;
                    if (!vis[e.to]) {
                        que.push(e.to);
                        vis[e.to] = true;
                    }
                }
            }
        }
    }
}

int main()
{
    int m, n;
    scanf("%d%d", &m, &n);
    
    for (int i = 1; i <= n; i++) {
        int p, l, x;
        scanf("%d%d%d", &p, &l, &x);
        lev[i] = l;
        G[0].push_back(Edge(i, p));
        while (x--) {
            int t, v;
            scanf("%d%d", &t, &v);
            G[t].push_back(Edge(i, v));
        }
    }
    
    int ans = inf;
    for (int i = lev[1] - m; i <= lev[1]; i++) {
        spfa(n, i, i + m);
        ans = min(ans, d[1]);
    }
    printf("%d\n", ans);
    
    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