Luogu P1186 Marika

Source: Internet
Author: User

Portal

Thinking of solving problems

Topic description Touching. Explain that this is the person with the system game, the system cut a side, and then make this person to go the shortest path longest. The procedure is more natural, first run the shortest path, and then enumerate the path to run the quickest way, the maximum value. This complexity is $o (N^2log (m) $) because the shortest circuit is impossible to ring.

#include <iostream>#include<cstdio>#include<cstring>#include<cmath>#include<queue>using namespacestd;Const intMAXN =1005;Const intMAXM =1000005; inlineintRd () {intx=0, f=1;CharCh=GetChar ();  while(!isdigit (CH)) {f=ch=='-'?0:1; ch=GetChar ();}  while(IsDigit (CH)) {x= (x<<1) + (x<<3) +ch-'0'; ch=GetChar ();} returnf?x:-x;}intN,m,head[maxn],cnt=1, Fa[maxn],dis[maxn],ans;BOOLCUT[MAXM],VIS[MAXN];intto[maxm<<1],nxt[maxm<<1],val[maxm<<1];structnode{intW,id; FriendBOOL operator< (ConstNode A,ConstNode B) {        returnA.w>B.W; }};p Riority_queue<Node>Q;inlinevoidAddintBgintEdintW) {to[++cnt]=ed,nxt[cnt]=head[bg],val[cnt]=w,head[bg]=CNT;}voidDijkstraBOOLflag) {memset (DIS,0x3f,sizeof(DIS)); memset (Vis,false,sizeof(VIS)); Node now;now.id=1; now.w=0;d is[1]=0;    Q.push (now);  while(!Q.empty ()) {Node zz=q.top (); Q.pop ();intx=zz.id; if(DIS[X]!=ZZ.W | | vis[x])Continue; vis[x]=1;  for(RegisterintI=head[x];i;i=Nxt[i]) {            if(Cut[i])Continue;intu=To[i]; if(Dis[u]<=dis[x]+val[i])Continue; if(flag) fa[u]=x; Dis[u]=dis[x]+Val[i]; Now.id=u;now.w=Dis[u];        Q.push (now); }    }        if(flag)return; Ans=Max (Ans,dis[n]);}intMain () {n=rd (), M=rd ();intx, Y, Z  for(RegisterintI=1; i<=m;i++) {x=rd (), Y=rd (), z=Rd ();    Add (x, Y, z), add (y,x,z); } Dijkstra (1); intnow=n,k;  while(Fa[now]) { for(RegisterintI=head[now];i;i=Nxt[i])if(To[i]==fa[now]) {cut[i]=cut[i^1]=1; k=i; Break;} Dijkstra (0); now=fa[now];cut[k]=cut[k^1]=0; } printf ("%d", ans); return 0;}
View Code

Luogu P1186 Marika

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.