NOIP2009 best Trade (BFS)

Source: Internet
Author: User

The positive solution is Tarjan, I did not write

With two BFs, the first BFS is done on the back of the original image, starting with N and finding all the points that can be reached from N.

The second BFS starts from the starting point, saving the minimum value mp[i] on each point to the N-point path.

Finally, the maximum value of w[i]-mp[i] can be calculated by traversing it again.

#include <cstdio> #include <iostream> #include <queue> #include <cstring> #define MAXN    100005using namespace std;struct t{int v; int next;}    Edge[500005],edge2[500005];int cnt,cnt2;int head[maxn],head2[maxn];void add_edge (int u,int v) {edge[cnt].v = v;    Edge[cnt].next = Head[u]; Head[u] = cnt++;}    void Add_edge2 (int u,int v)//save inverse diagram {edge2[cnt2].v = v;    Edge2[cnt2].next = Head2[u]; Head2[u] = cnt2++;} int Mp[maxn],w[maxn];bool able[maxn],vis[maxn];queue<int> myque;int n,m;void bfs1 () {for (int i = 1; I <= N; i+        +)//vis[i] Indicates whether I is in the queue, which avoids the effect of the ring in the diagram {Myque.push (i);    Vis[i] = 1;        } while (!myque.empty ()) {int u = myque.front ();        Vis[u] = 0;        Myque.pop ();            for (int i = head[u]; i =-1; i = edge[i].next) {int v = EDGE[I].V;                if (Mp[v] > Mp[u]) {Mp[v] = Mp[u];           if (!vis[v]) {vis[v] = 1;         Myque.push (v);    }}}}}void bfs2 () {Myque.push (n);    Able[n] = 1;        while (!myque.empty ()) {int u = myque.front ();        Myque.pop ();            for (int i = head2[u]; i =-1; i = edge2[i].next) {int v = EDGE2[I].V;                if (!able[v]) {able[v] = 1;            Myque.push (v);    }}}}int Main () {memset (head,-1,sizeof head);    memset (head2,-1,sizeof head2);    scanf ("%d%d", &n,&m);        for (int i = 1; I <= n; i++) {scanf ("%d", &w[i]);    Mp[i] = W[i];        } for (int i = 1; I <= m; i++) {int x, y, Z;        scanf ("%d%d%d", &x,&y,&z);        Add_edge (x, y);        Add_edge2 (Y,X);            if (z = = 2) {Add_edge (y,x);        Add_edge2 (x, y);    }} bfs2 ();//reverse, find can reach point bfs1 ();//forward, find I to n path maximum value int ans = 0; for (int i = 1; I <= n; i++) if (Able[i]) ans =Max (Ans,w[i]-mp[i]);    printf ("%d\n", ans); return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

NOIP2009 best Trade (BFS)

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.