La 4080 (multi-source shortest path + edge modification + Shortest Path Tree)

Source: Internet
Author: User

Question link:Http://acm.hust.edu.cn/vjudge/problem/viewProblem.action? Id = 32266

Theme: ① First obtain the sum of the shortest paths between any two points, wherein the edge without connection is l ② delete any edge and obtain the global shortest path and the maximum value.

Solutions:

First, let's talk about the trade-off between Floyd and Dijkstra with priority queue Optimization in multi-source shortest. Floyd is a good shot. Dijkstra has the potential constant and flexibility (for example, when asking for the second question ).

The most annoying thing about this question is to enumerate and delete an edge. According to normal thinking, we need to re-Execute Dijkstra n times. The complexity has reached terrible (N * m ^ 2 * logn ), so is it necessary to re-execute the shortest path for the entire source every time you modify an edge?

The answer is no. You only need to build a shortest path tree.

Do not hide it by name. It is actually a two-dimensional array. belong [edge id] [s point] marks each edge when performing full-source Dijkstra for the first time, mark the content as the s point of this dij.

Note that the edge here refers to the input edge ID, not the edge in the graph (this is an undirected graph, and the input edge is added twice ).

When an edge is deleted by enumeration, if belong [edge id] [s point] = true, this edge is related to the single-source dij and must be re-dij. If it is false, it is irrelevant. The value is the value of dij for the first time.

The belong flag method: When a priority queue leaves the queue, it marks the front edge of the departure point. The specific method is to open a p array, each time the relax, record the Relax edge. After that, the p array can immediately retrieve the front edge of the queue.

This question has a duplicate edge, and the data structure of the duplicate edge is not supported, especially the adjacent table. We recommend that you chain forward stars.

The results of the other two questions exceed int32.

 

# Include "cstdio" # include "queue" # include "cstring" using namespace STD; # define maxn 155 # define maxp 2005 # define INF 1 <28 # define ll long longstruct edge {int next, to, D, ID;} e [maxp * 2]; struct status {int D, P; Status (int d, int P): D (D), P (P) {} bool operator <(const Status &) const {return D>. d ;}}; int N, M, L, Tol, head [maxn], d [maxn], p [maxn], DIS [maxn] [maxn]; ll ans1, ans2, W [maxn]; bool vis [maxn], belong [Max P] [maxn], del [maxp]; void addedge (int u, int V, int C, int ID) {e [tol]. id = ID; E [tol]. D = C; E [tol]. to = V; E [tol]. next = head [u]; head [u] = tol ++;} void pair stra1 (INT s) {memset (VIS, false, sizeof (VIS); memset (p, 0, sizeof (p); For (INT I = 1; I <= N; I ++) d [I] = (I = s? 0: INF); priority_queue <status> q; q. Push (status (0, S); While (! Q. empty () {status TT = Q. top (); q. pop (); int x = TT. p; If (vis [x]) continue; vis [x] = true; belong [p [x] [s] = true; for (INT I = head [X]; I! =-1; I = E [I]. next) {int v = E [I]. to; If (d [x] + E [I]. d <D [v]) {P [v] = E [I]. ID; d [v] = d [x] + E [I]. d; q. push (status (d [v], V) ;}}for (INT I = 1; I <= N; I ++) {If (d [I] = inf) {ans1 + = L; W [s] + = L;} else {ans1 + = d [I]; W [s] + = d [I] ;}} ll passed stra2 (INT s) {memset (VIS, false, sizeof (VIS); For (INT I = 1; I <= N; I ++) d [I] = (I = s? 0: INF); priority_queue <status> q; q. Push (status (0, S); While (! Q. empty () {status TT = Q. top (); q. pop (); int x = TT. p; If (vis [x]) continue; vis [x] = true; For (INT I = head [X]; I! =-1; I = E [I]. next) {If (DEL [E [I]. id]) continue; // The Edge marked as deleted skips int v = E [I]. to; If (d [x] + E [I]. d <D [v]) {d [v] = d [x] + E [I]. d; q. push (status (d [v], V) ;}} long TT = 0; For (INT I = 1; I <= N; I ++) {If (d [I] = inf) tt + = L; else tt + = d [I];} return tt;} int main () {int U, V, c; while (scanf ("% d", & N, & M, & L )! = EOF) {memset (Head,-1, sizeof (head); memset (belong, false, sizeof (belong); memset (Del, false, sizeof (DEL); memset (W, 0, sizeof (w); Tol = ans1 = ans2 = 0; For (INT I = 1; I <= m; I ++) {scanf ("% d", & U, & V, & C); addedge (u, v, C, I ); addedge (v, U, C, I) ;}for (INT I = 1; I <= N; I ++) returns stra1 (I); For (INT I = 1; I <= m; I ++) {ll TT = 0; del [I] = true; For (Int J = 1; j <= N; j ++) if (belong [I] [J]) tt + = constraint stra2 (j); else tt + = W [J]; del [I] = false; ans2 = max (ans2, TT);} printf ("% LLD \ n", ans1, ans2 );}}

 

2814660 Neopenx Uvalive 4080 Accepted 0 KB 409 MS C ++ 4.5.3 2990 B 2014-10-04 18:34:44  
 

 

La 4080 (multi-source shortest path + edge modification + Shortest Path Tree)

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.