"Routine training"paths and Trees (codeforces Round 301 Div.2 E)

Source: Internet
Author: User

Test instructions and Analysis code
#include <bits/stdc++.h> #define MP make_pair#define PB emplace_back#define fi first#define se second#define ZERO (x ) memset ((x), 0, sizeof (x)) #define ALL (x) (x). Begin (), (x). End () #define REP (I, A, b) for (Reptype i = (a); I <= (b); ++i ) #define PER (I, a, b) for (Reptype i = (a); I >= (b); i) #define Quickio Ios::sync_with_stdio (FAL     SE);                      Cin.tie (0);    Cout.tie (0); using namespace std;using ll=long long;using reptype=int;struct edge{int u, v;    ll W;         Edge () {} edge (int _u, int _v, LL _w): U (_u), V (_v), W (_w) {} BOOL operator < (const edge& RHS) Const {        if (W==RHS.W) {return (U==RHS.U)?v<rhs.v:u<rhs.u;}    else {return w<rhs.w;} }};const int maxn=300005;vector<edge> edges;vector<int> G[maxn];voidadd_edge (int u, int v, ll W) {edges.    PB (U, V, W); G[u]. PB (int (edges.size ())-1);} ll Dist[maxn];int PRE[MAXN]; Pre:last Edgevoiddijkstra (int start) {memset (PRE, -1, sizeof (pre));    memset (Dist, 0x3f, sizeof (Dist));    Using P=pair<ll, int>; Priority_queue<p, Vector<p>, greater<> > PQ;    <dist, Pnt>: Dagen dist[start]=0;    Pq.push (MP (0, start));        while (!pq.empty ()) {Auto Now=pq.top (); Pq.pop ();        int u=now.se;        if (dist[u]<now.fi) {continue;}            Rep (i, 0, int (g[u].size ())-1) {int v=edges[g[u][i]].v;            ll W=EDGES[G[U][I]].W;                if (dist[v]>dist[u]+w) {dist[v]=dist[u]+w;                Pre[v]=g[u][i];            Pq.push (MP (dist[v], v));         } else if (dist[v]==dist[u]+w && edges[pre[v]].w>edges[g[u][i]].w) {pre[v]=g[u][i];}    }}}intmain () {int n, m;    scanf ("%d%d", &n, &m);        Rep (i, 1, m) {int u, v;        ll W;        scanf ("%d%d%lld", &u, &v, &w);        Add_edge (U, V, W);    Add_edge (V, U, W); } int stp; ScaNF ("%d", &AMP;STP);    Dijkstra (STP);    ll Ans=0;    Rep (i, 1, N) if (I!=STP) {ANS+=EDGES[PRE[I]].W;}    printf ("%lld\n", ans);    Rep (i, 1, N) if (I!=STP) {printf ("%d", pre[i]/2+1);}    printf ("\ n"); return 0;}

Daily training "paths and Trees (codeforces Round 301 Div.2 E)

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.