Codeforces Round #257 Div.2 D or 450D Jzzhu and Cities "shortest circuit"

Source: Internet
Author: User
Tags server memory

Codeforces Round #257 Div.2 D or 450D Jzzhu and Cities "shortest circuit"
  • Topic link : Click to open

  • Main Topic :
    There are n cities in a country (city number 1~n), M-Highway and K-Railways, the capital of the city numbered 1, in order to save, unnecessary railways need to be shut down, asking how many railways are not needed to ensure that the capital is the shortest path to all the rest of the city.

  • Solution :
    This problem is more troublesome, to ensure that the capital to the rest of the city's shortest path, the maximum number of railways is not required, it must be from the shortest-circuited code up and down, we first consider the Dijkstra algorithm, assuming that at this time we deal with the graph does not have a heavy edge.

      • Because we want to make the most unnecessary railways, the edges of our shortest path should be as secure as possible, that is, when there are many short circuits from the capital to a city, we should choose the least of the railways. then we should add this judgment in the "slack" operation of the algorithm.
      • We can use a path[] array to record which two points between the edge is the shortest path edge, record this, we can in the traversal process to easily determine which railway is not needed, which railway is in the shortest path, in order to get the answer. Where Path[v]=u, indicates (U,V) this edge on the shortest path. Because there are no heavy edges, it is possible to determine an edge.
      • Since the previous conclusion is based on the graph does not have a heavy edge, but the topic will give the data with a heavy edge, so we should first remove the original data in the heavy edge. to remove the heavy edge, to leave the two cities between the minimum weight of the one, the road as far as possible , the removal of heavy edges, the removal of the railway should also be included in the answer.

    What makes me baffled is that I returned a mle! after I wrote the question. Let me think for a long time, the CF server memory limit has been 256MB, rarely this situation, it is not possible that the array is too large, I think for a while, suddenly thought it is likely that the Dijkstra algorithm in the priority queue of data too much caused by the memory exceeded the limit, so hurriedly added a limit. Sure enough, it's over.

      
















      

    Here is a Boolean array trainofpath[v] recorded to the V point of this side is not a railroad. If the road was originally a railroad, it is now the road that is updated, only to be updated; If you want to get a MLE code, if you are working on a highway that is currently being updated, some perverted data will cause the data to be pressed into the priority queue to be too large, resulting in a mle.
    The complete code is as follows:
      

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#define N 200010using namespace STD;Const Long Longinf=0x7fffffffffffffffLstructedge{Long LongU,V,W,NXT;BOOLIstrain;BOOL operator< (Constedge& o)Const{returnu<o.u| | (u==o.u && v<o.v) | | (u==o.u && v==o.v && w<o.w) | |    (u==o.u && v==o.v && w==o.w && istrain<o.istrain); }}e1[n*4],e[n*3];Long LongLast[n];typedefpair<Long Long,Long Long> PII;Long LongVLong LongC[i];Long LongPath[n];BOOLTrainofpath[n];p Riority_queue<pii, vector<PII>,greater<pii> > que;voidDijkstraLong Longs) {//memset (d,0x3f,sizeof (d));Fill (d+1, d+v+1, INF); d[s]=0; Que.push (PII (0, s)); while(!que.empty ()) {PII pp=que.top (); Que.pop ();Long LongU=pp.second;if(D[u]<pp.first)Continue; for(Long Longi=last[u];i!=-1; i=e[i].nxt) {Long LongV=E[I].V,W=E[I].W;BOOLIs=e[i].istrain;if(D[v]>d[u]+w | |            (d[v]==d[u]+w &&!is && trainofpath[v]))                {trainofpath[v]=is;                Path[v]=u;                D[v]=d[u]+w;            Que.push (PII (d[v],v)); }        }    }}intMain () {Long LongN,m,k;scanf("%i64d%i64d%i64d", &n,&m,&k); V=n;Long Longans=0;memset(last,-1,sizeof(last)); for(intI=0; i<=n;i++) {trainofpath[i]=true; } for(Long LongI=0; i<m;i++) {Long LongU,v,w;scanf("%i64d%i64d%i64d", &u,&v,&w);if(u>v) swap (U,V); e1[i].u=u,e1[i].v=v,e1[i].w=w,e1[i].istrain=false; } for(Long LongI=0; i<k;i++) {Long LongV,w;scanf("%i64d%i64d", &v,&w); e1[i+m].u=1, e1[i+m].v=v,e1[i+m].w=w,e1[i+m].istrain=true; } sort (e1,e1+m+k);Long Longedge1num=1; for(Long LongI=1; i<m+k;i++) {if(e1[i].u==e1[i-1].U && e1[i].v==e1[i-1].V) {if(E1[i].istrain) ans++;Continue;    } E1[edge1num++]=e1[i]; }Long Longedgenum=0; for(Long LongI=0; i<edge1num;i++) {Long LongU=E1[I].U,V=E1[I].V,W=E1[I].W;BOOLIs=e1[i].istrain;        e[edgenum].u=u,e[edgenum].v=v,e[edgenum].w=w,e[edgenum].istrain=is,e[edgenum].nxt=last[u],last[u]=edgenum++;    e[edgenum].v=u,e[edgenum].u=v,e[edgenum].w=w,e[edgenum].istrain=is,e[edgenum].nxt=last[v],last[v]=edgenum++; } Dijkstra (1); for(Long LongI=0; i<edgenum;i+=2)    {Long LongU=E[I].U,V=E[I].V;BOOLIs=e[i].istrain;if(!is)Continue;if(Path[v]==u | | path[u]==v)Continue;    ans++; }cout<<ans<<endl;return 0;}

(This time I tried a new feature markdown editor ^_^ not how to use, it looks ugly, but the code appears to be not as beautiful as it used to be .... may need to learn a new posture, hehe)

Codeforces Round #257 Div.2 D or 450D Jzzhu and Cities "shortest circuit"

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.