Uvalive 6885 Flowery Trails shortest-circuit enumeration

Source: Internet
Author: User

Topic Connection:

http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=129723

Test instructions

Give you an n-point m-Graph edge

1 to n There are multiple shortest circuits, asking you what the sum of all the edges

Exercises

SPFA on the shortest path of single source for 1,n

Enumerates whether an edge is the shortest edge

i.e. Benquan +disa[i] + disb[i] = Shortest circuit length, is the answer

#include <bits/stdc++.h>using namespacestd;Const intN = 1e6+ -, M = 1e6+Ten, mod = 1e9+7, INF = 1e9+ +; typedefLong Longll;intHead[n],t,vis[n],dis[n],n,m,disa[n],disb[n];structedge{intTo,value,next;} e[n*2];voidAddintUintVintW) {e[t].next=head[u],e[t].to=v,e[t].value=w;head[u]=t++;}voidinit () {T=0; memset (Head,-1,sizeof(head));}voidSPFA (intu) { for(intI=1; i<=n;i++) Dis[i] = inf, vis[i] =0; vis[1] =1; Dis[u]=0; Queue<int>Q;    Q.push (U);  while(!Q.empty ()) {        intK =Q.front (); Q.pop (); VIS[K]=0;  for(inti=head[k];i!=-1; i=E[i].next) {            intto =e[i].to; if(Dis[k] + E[i].value <Dis[to]) {Dis[to]= Dis[k] +E[i].value; if(!Vis[to])                    {Q.push (to); Vis[to]=1; }            }        }    }}intMain () { while(~SCANF ("%d%d",&n,&m)) {init ();  for(intI=1; i<=m;i++) {            intA,b,c; scanf ("%d%d%d",&a,&b,&c); A++,b++;            Add (A,B,C);        Add (B,A,C); } SPFA (1);  for(intI=1; i<=n;i++) Disa[i] =Dis[i];        SPFA (n);  for(intI=1; i<=n;i++) Disb[i] =Dis[i]; intMX = dis[1]; ll ans=0;  for(intI=1; i<=n;i++) {             for(intj=head[i];j!=-1; j=E[j].next) {                intto =e[j].to; intValue =E[j].value; if(Disa[i] + disb[to] + value = = mx) ans+=value; }} printf ("%lld\n", 2ll*ans); }}

Uvalive 6885 Flowery Trails shortest-circuit enumeration

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.