Sdut 2498-aoe Key Path (spfa + lexicographic path)

Source: Internet
Author: User
Key Path on AOE Time Limit: 1000 ms memory limit: 65536 k any questions? Click Here ^_^ Description

A directed graph without loops is called a directed graph (DAG.
AOE (activity on edge) Network: as the name suggests, an edge is used to represent the active network. Of course, it is also a Dag. Unlike AOV, the activity is represented on the edge, as shown in:

As shown above, there are 11 Activities (11 edges) and 9 events (9 vertices ). The entire project has only one start point and one completion point. That is, there is only one point with zero inbound (source point) and only one point with zero outbound (sink point ).
Key Path: The longest path length from the start point to the completion point. The path length is the time consumed by the edge activity. As shown in, 1 to 2 to 5 to 7 to 9 are key paths (there are more than one key path. Please output the smallest Lexicographic Order). The sum of weights is 18.

The input contains multiple groups of data. ensure that there are no more than 10 groups, and there is only one source point and sink point. Input a vertex number N (2 <= n <= 10000), edge number M (1 <= m <= 50000), and then input the start point SV, end point eV, weight W (1 <= SV, ev <= N, SV! = EV, 1 <= W <= 20 ). Data ensures graph connectivity. Output the weights and values of the key paths, and output the paths on the key paths from the source point (if there are multiple paths, output the smallest Lexicographic Order ). 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
Sample output
181 22 55 77 9
The longest path + the minimum path of the record Lexicographic Order (that is, if there are multiple longest-path outputs with the smallest Lexicographic Order, for example, 1-> 2-> 4 and 1-> 3-> 4 both conform to the longest path., when the output is 1-> 2-> 4), the main implementation is that, when dis [v] = dis [u] + W, determine the Lexicographic Order of the path to determine whether to update the path. Currently, only Qaq is determined by brute force.
#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[10010], s2[10010], ans[10010], dis[10010], in[10010], out[10010], path[10010], n, m, s, e;bool vis[10010];vector <pair<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(out));memset(vis, 0, sizeof(vis));memset(path, -1, sizeof(path));while (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();print();}return 0;}


 
 

Sdut 2498-aoe Key Path (spfa + lexicographic 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.