Codeforces Round #303 (Div. 2) E. Paths and Trees (shortest path + deform minimum spanning tree)

Source: Internet
Author: User

Title Address: E. Paths and Trees
Simulate a CF, this is really too water. To play while doing. The last half-minute has been handed an e question. Unfortunately AK failed in the lore ....
First of all, the idea must first seek the shortest way, may be the most short-circuit side to pick out, and then the second step I originally wrote is directly using the smallest tree of the graph, so the failure of the lore ... Later found that this is not possible. Because the edge is a forward edge, and each point in the degree to ensure that only one line. So I made the edge of the smallest spanning tree into a forward edge, and then judged that the entry for each point was guaranteed to be 1. And then it's over.
The code is as follows:

#include <iostream>#include <string.h>#include <math.h>#include <queue>#include <algorithm>#include <stdlib.h>#include <map>#include <set>#include <stdio.h>#include <time.h>using namespace STD;#define LL __int64#define PI ACOs ( -1.0)#pragma COMMENT (linker, "/stack:1024000000")Const intMod=9901;Const intinf=0x3f3f3f3f;Const Doubleeqs=1e-9;Const intmaxn=300000+Ten;intHEAD[MAXN], CNT, HA[MAXN], VIS[MAXN]; LL D[MAXN];structnode{intU, V, next; LL W;} edge[maxn<<1];voidAddintUintV, LL W) {edge[cnt].v=v;        Edge[cnt].w=w;        Edge[cnt].next=head[u]; head[u]=cnt++;}voidSPFA (intST) { Queue<int>Q Q.push (ST);memset(Vis,0,sizeof(VIS)); d[st]=0; while(!q.empty ()) {intU=q.front ();                Q.pop (); vis[u]=0; for(inti=head[u];i!=-1; i=edge[i].next) {intV=EDGE[I].V;if(D[V]&GT;D[U]+EDGE[I].W) {D[V]=D[U]+EDGE[I].W;if(!vis[v]) {vis[v]=1;                                Q.push (v); }                        }                }        }}structnode1{intU, V, id; LL W;} FEI[MAXN];inttot=0, BIN[MAXN], IN[MAXN];intFind1 (intx) {returnBin[x]==x?x:bin[x]=find1 (Bin[x]);}BOOLCMP (Node1 F1, Node1 F2) {returnF1.W&LT;F2.W;}voidInitintN) { for(intI=1; i<=n;i++) bin[i]=i;memset(In,0,sizeof(in));}voidKrus (intNintm) {sort (fei,fei+tot,cmp);intI LL ans=0; Init (n); for(i=0; i<tot;i++) {intu=fei[i].u;intV=FEI[I].V;if(In[v])Continue;intF1=find1 (bin[fei[i].u]);intF2=find1 (BIN[FEI[I].V]);if(F1!=F2)                        {bin[f2]=f1;                        ANS+=FEI[I].W; ha[fei[i].id]=1;                in[v]++; }        }printf("%i64d\n", ans); for(intI=1; i<=m;i++) {if(Ha[i]) {printf("%d", i); }                }puts("");}intMain () {intN, M, I, J, u, V; LL W, ans; while(scanf("%d%d", &n,&m)!=eof) {memset(head,-1,sizeof(head)); Cnt=0; for(i=1; i<=n;i++) {d[i]= (LL)1e16; } for(i=1; i<=m;i++) {scanf("%d%d%i64d", &u,&v,&w);                        Add (U,V,W);                Add (V,U,W); }scanf("%d", &u); SPFA (U);memset(Ha,0,sizeof(ha)); ans=0; for(i=1; i<=n;i++) { for(j=head[i];j!=-1; j=edge[j].next) {intV =EDGE[J].V;if(D[i]+edge[j].w==d[v])                                        {fei[tot].u=i;                                        Fei[tot].v=v;                                        FEI[TOT].W=EDGE[J].W; fei[tot++].id=j/2+1;        }}} Krus (N,m); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Codeforces Round #303 (Div. 2) E. Paths and Trees (shortest path + deform minimum spanning 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.