UVA 4080 Warfare and Logistics War with logistics (shortest path tree, variant)

Source: Internet
Author: User

Test instructions: Give an undirected graph, n points, M edge, not connected, can re-edge, can be extra edge. Two questions, the first question: the sum of the shortest distance between arbitrary points. Second question: You must delete an edge and ask the first question to make the result larger.

Ideas:

In fact, they are all in the process of finding the shortest path.

The first question can Floyd solve, also can sssp solve. Note that any two points, (A, B) and (b,a) are different and are counted.

Second ask to be poor lift delete each edge, ask first. In order to reduce the complexity, assuming that the shortest path with Dijkstra, then you can use the first question generated in the tree, a total of n trees, each tree at most n-1 edge, if the poor lifting of the edge is not in the tree, then the tree of all the path is constant, do not have to calculate, otherwise need to calculate. So it is necessary to record the path and save the whole tree's edge set, while preserving the sum of any two-point path of each tree.

With the structure can solve the heavy edge, the problem should not be many, to pay attention to various details, wrong on the re-dozen, perhaps faster.

  

1#include <bits/stdc++.h>2 #defineLL Long Long3 #definePII pair<int,int>4 #defineINF 0x7f7f7f7f5 using namespacestd;6 Const intn= -;7 intN, M, L, edge_cnt;8vector<int>Vect[n];9 Ten structnode One { A     int  from, to, Dis,tag; - node () {}; -Nodeint  from,intTo,intDisintTAG): from( from), to, dis (dis), tag (tag) {}; the}edge[2050]; -  - voidAdd_node (int  from,intTo,intDisinttag) - { +Edge[edge_cnt]=node ( from, to, DIS, tag); -vect[ from].push_back (edge_cnt++); + } A  at intDist[n], vis[n], path[n]; -LL Dijkstra (ints) - { -memset (Dist,0x7f,sizeof(Dist)); -memset (Vis,0,sizeof(Vis)); -      for(intI=0; i<=n; i++) path[i]=-1; in  -priority_queue<pii,vector<pii>,greater<pii> >que; todist[s]=0; +Que.push (Make_pair (0, s)); -  the      while(!que.empty ()) *     { $         intx=que.top (). Second;que.pop ();Panax Notoginseng         if(Vis[x])Continue; -vis[x]=1; the          for(intI=0; I<vect[x].size (); i++) +         { ANode e=Edge[vect[x][i]]; the             if(e.tag>0&& dist[e.to]>dist[e. from]+E.dis) +             { -path[e.to]=Vect[x][i]; $Dist[e.to]=dist[e. from]+E.dis; $ Que.push (Make_pair (dist[e.to], e.to)); -             } -         } the     } - WuyiLL sum=0; the      for(intI=1; i<=n; i++ ) -     { Wu         if(Dist[i]>=inf) sum+=l;//not reach, press L to calculate -         Elsesum+=Dist[i]; About     } $     returnsum; - } -  - LL Ans1[n]; A intcal () + { thememset (ANS1,0,sizeof(ans1)); -LL first=0; $unordered_set<int>Tree[n]; the      for(intI=1; i<=n; i++) the     { theans1[i]=Dijkstra (i); thefirst+=Ans1[i]; -         //Collecting Edges in          for(intk=1; k<=n; k++) the         { the             if(path[k]>=0)//Be aware of how to initialize About             { the Tree[i].insert (Path[k]); theTree[i].insert (path[k]^1); the             } +         } -     } the     //another questionBayiLL second=0; the      for(intI=0; i<edge_cnt; i+=2) the     { -edge[i].tag=edge[i+1].tag=0; -LL sum=0; the          for(intj=1; j<=n; J + +) the         { the             if(Tree[j].find (i) ==tree[j].end ())//It's a tree of J, to be counted . thesum+=Ans1[j]; -             Else thesum+=Dijkstra (j); the         } theSecond=Max (second, sum);94edge[i].tag=edge[i+1].tag=1; the     } theprintf"%lld%lld\n", first, second);//only 1 spaces the }98  About intMain () - {101Freopen ("Input.txt","R", stdin);102     intA, B, C;103      while(SCANF ("%d%d%d", &n, &m, &l) = =3)104     { theEdge_cnt=0;106Memset (Edge,0,sizeof(Edge));107          for(intI=0; i<=n; i++) vect[i].clear ();108          for(intI=0; i<m; i++)109         { thescanf"%d%d%d",&a,&b,&c);111             if(a==b)Continue; theAdd_node (A,b,c,1);113Add_node (B,a,c,1); the         } the cal (); the     }117     return 0;118}
AC Code

UVA 4080 Warfare and Logistics Wars and logistics (shortest path tree, deformation)

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.