codeforce-601a. The Routes (Shortest way)

Source: Internet
Author: User

Test instructions

Give you n points to show n stations, cars and trains, cars can only take the road, the train can only take the railway.

Then I'll give you a two-way road that represents a railroad connection between the two points.

And then tell you that if there are no railways between the two points, then it is a road connection.

Ask you what the minimum time it takes for both the car and the train to reach their destination (two modes of transport cannot reach the same city at the same time except for the destination).

If there is a means of transport can not reach on the output-1;

Ideas:

If there is a railroad between 22 points, then there is no road, then the car cannot reach, then-1;

If this is not the case, then either there is a railway connection starting point, or there is a road, so there must be a 1 between the two,

So ask for the shortest path of the other (certainly not collide). (ask for two times the shortest line).

Complexity of N2;

1#include <stdio.h>2#include <algorithm>3#include <iostream>4#include <stdlib.h>5#include <vector>6#include <queue>7#include <cstdio>8#include <string.h>9 voidDjintn);Ten Const intv=99999999; Onetypedefstructpp A { -     intx; -     inty; the } SS; - using namespacestd; - intflag[ -]; - intjj[ -][ -]; + intd[ -]; - intMainvoid) + { A     intn,i,j,k,p,q,n,m; at      while(SCANF ("%d%d", &n,&m)! =EOF) -     { -memset (Flag,0,sizeof(flag)); -          for(i=0; i< the; i++) -         { -              for(j=0; j< the; J + +) in             { -jj[i][j]=V; to             } +         } -          for(i=0; i<m; i++) the         { *scanf"%d%d",&p,&q); $jj[p][q]=1;Panax Notoginsengjj[q][p]=1; -         } the DJ (N); +         intss=D[n]; A          for(i=0; i< the; i++)//find the path to the car the         { +              for(j=0; j< the; J + +) -             { $                 if(jj[i][j]==V) $                 { -jj[i][j]=1; -                 } the                 Elsejj[i][j]=V; -             }Wuyi         } the DJ (N); -         intvv=D[n]; Wu         intuu=Max (VV,SS); -         if(uu>=V) About         { $printf"-1\n"); -         } -         Elseprintf"%d\n", UU); -  A     } +     return 0; the } -  $ voidDjintN//Shortest Path N2 algorithm the { theFill (d,d+n+1, V); theFill (flag,flag+n+1,0); thed[1]=0; -     inti,j,k,p,q; in      while(true) the     { the         intl=-1; About          for(i=1; i<=n; i++) the         { the             if(flag[i]==0&& (l==-1|| d[i]<D[l])) the             { +L=i; -             } the         }Bayi         if(l==-1) the         { the              Break; -         } -flag[l]=1; the          for(i=1; i<=n; i++) the         { theD[i]=min (d[i],d[l]+jj[l][i]); the         } -     } the  the}

codeforce-601a. The Routes (Shortest way)

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.