[POJ 1797] Heavy TRANSPORTATION[SPFA]

Source: Internet
Author: User

Topic Link: [POJ 1797]heavy TRANSPORTATION[SPFA]

The analysis:

For all paths from point 1 to N, the maximum size of the smallest capacity on the path.

Ideas for solving problems:

will be the shortest way of thinking change on the line. The Dis[i] Array records the smallest road capacity on the road from point 1 to I. Note Initialization of the DIS array.

Personal experience:

It was written because it was stuck in the initialization and tried several times.

The specific code is as follows:

#include <cstdio> #include <cstring> #include <iostream> #include <queue> using namespace std;

const int MAXN = 1E3 + 111;
    struct n{int to, cap;
    N (int A, int b) {to = A; cap = b;
}
};
Vector<n> G[MAXN];
int DIS[MAXN];

BOOL IN[MAXN];
    void SPFA (int s) {queue<int> q;
    Q.push (s);
        for (int i = 0; i < g[s].size (); ++i) {N &e = g[s][i];
        Dis[e.to] = E.cap;
        Q.push (e.to);
    In[e.to] = 1;
    } In[s] = 1;
        while (Q.size ()) {int cur = Q.front (); Q.pop (); in[cur] = 0;
            for (int i = 0; i < g[cur].size (); ++i) {N &e = g[cur][i];
                if (Dis[e.to] < min (Dis[cur], e.cap)) {dis[e.to] = min (dis[cur), e.cap);
                    if (!in[e.to]) {in[e.to] = 1;
                Q.push (e.to);
()}}} int main () {int T, n, M;    for (int kase = scanf ("%d", &t), Kase <= t; ++kase) {scanf ("%d%d", &n, &m);
        for (int i = 1; I <= n; ++i) g[i].clear ();
            while (M-) {int u, to, W;
            scanf ("%d%d%d", &u, &to, &w);
            G[u].push_back (N (to,w));
        G[to].push_back (N (u,w));
        } memset (dis, 0, sizeof (int) * (n + 10));
        memset (in, 0, sizeof (BOOL) * (n + 10));
        SPFA (1);
    printf ("Scenario #%d:\n%d\n\n", Kase, Dis[n]);
return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.