Sdut 2498-aoe Online critical path (spfa+ dictionary sequence Path)

Source: Internet
Author: User

AoE critical path on the web Time limit:1000ms Memory limit:65536k have questions? Dot here ^_^ Topic Description Narration

    < Span style= "padding:0px; margin:0px "> a non-circular, forward graph is called a non-circular graph ( directed acyclic Graph dag figure.
    AOE (activity on edge) network: As the name implies, use the edge to represent the active network, of course, it is also a DAG. Unlike AOV , activities are represented on the edge, for example, as seen in:
< Span style= "padding:0px; margin:0px ">
     < Span style= "padding:0px; margin:0px ">11 activity (11 edge), 9 events (9 < Span style= "padding:0px; margin:0px ">
< Span style= "padding:0px; margin:0px ">     Critical Path: Is the length of the longest path from the start point to the point of completion. The length of the path is the time spent on the edge. As seen,1 to 2 to 5 to 7 to 9 is the critical path (more than one key path, please output the smallest dictionary order), the weight of the value of the.

Input    There are multiple sets of data, guaranteed to be no more than ten Group, guaranteeing that there is only one source point and meeting point. Enter a top point N (2<=n<=10000), number of sides m (1<=m <=50000), next m line, input start SV , terminal EV, weight Value w (1<=sv,ev<=n,sv! = ev,1<=w <=20) . Data Assurance diagram connectivity. Outputthe weight of the critical path and the path on the critical path from the source point (assuming that there are more than one, output the smallest dictionary order). Demo sample Input
9 111 2 61 3 41 4 52 5 13 5 14 6 25 7 95 8 76 8 48 9 47 9 2
Demo sample Output
181 22 55) 77 9
 longest road + record dictionary order minimum path (that is, assuming that there are multiple longest-path output dictionary order the smallest of the example 1->2->4 and 1 ->3->4 all meet the longest road, then the output 1->2->4) the main implementation is in the slack, when dis[v]==dis[u]+w, infer the path of the dictionary to determine whether to update the path, now still only violence inference Qaq 
#include <iostream> #include <cstring> #include <cstdio> #include <cctype> #include <cstdlib > #include <algorithm> #include <set> #include <vector> #include <string> #include <cmath > #include <map> #include <queue>using namespace std; #define LL long longconst int INF = 0x3f3f3f3f;int s1[10 010], s2[10010], ans[10010], dis[10010], in[10010], out[10010], path[10010], N, M, S, E;bool vis[10010];vector <pair&lt ; int, int> > eg[50010];bool ok (int u, int v) {int p = V, num1 = 0;s1[num1++] = V;while (path[p]! =-1) {s1[num1++] = Path[p];p = path[p];} p = u;int num2 = 0;s2[num2++] = v;s2[num2++] = U;while (path[p]! =-1) {s2[num2++] = path[p];p = path[p];} int i = num1-1, j = num2-1;while (i >= 0 && J >= 0) {if (S1[i] > S2[j]) {return 1;} i--;j--;} return 0;} void Spfa () {queue <int> q;for (int i = 1; I <= n; i++) {dis[i] =-inf;} Dis[s] = 0; Q.push (s); while (! Q.empty ()) {int u = q.front (); Q.pop (); Vis[u] = 0;for (int i = 0; i < eg[u].size (); i++) {int v = eg[u][i].first;int W = eg[u][i].second;if (Dis[v] < Dis[u] + W) { DIS[V] = Dis[u] + w;path[v] = u;if (!vis[v]) {vis[v] = 1; Q.push (v);}} ElseIf (dis[v] = = Dis[u] + w && ok (U, v)) {path[v] = u; if (!vis[v]) {vis[v] = 1; Q.push (v);}}}} void print () {int p = e, num = 0;while (path[p]! =-1) {ans[num++] = path[p];p = path[p];} printf ("%d\n", Dis[e]); for (int i = num-1; i > 0; i--) {printf ("%d%d\n", Ans[i], ans[i-1]);} printf ("%d%d\n", Ans[0], E);} int main () {int u, V, c;while (~scanf ("%d%d", &n, &m)) {for (int i = 0; I <= N; i++) {eg[i].clear ();} memset (in, 0, sizeof (in)), memset (out, 0, sizeof), memset (Vis, 0, sizeof (VIS)), memset (path, 1, sizeof (path)); (m--) {scanf ("%d%d%d", &u, &v, &c); Eg[u].push_back (Make_pair (V, c)); in[v]++;out[u]++;} for (int i = 1; I <= n; i++) {if (!in[i]) {s = i;} if (!out[i]) {e = i;}} SPFA ();p rint ();} return 0;}


Sdut 2498-aoe Online critical path (spfa+ dictionary sequence Path)

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.