Key--[jsoi2007] Important city Floyd: Shortest-circuit count

Source: Internet
Author: User

~ ~ ~ Noodles ~ ~ ~

Exercises

In fact, the feeling is still relatively good, the first eye to think of Floyd statistics the shortest number of bars,

Note that for any two-point x, Y, Floyd will enumerate all the transit points for which the shortest possible

So we can directly separate statistics for all two-tuple, the shortest way must pass the transit point,

Finally, the result of all statistics is traversed, and a bool array is used to mark whether a place has been a transit point

Then iterate through the bool array again, and if it is a transit point, the output can be

Note that there are multiple short circuits that do not necessarily mean that the shortest path between the two points has no key points,

Because these short circuits may have one (or more) common points, the common point becomes the key

First look at how I counted:

1                 if(F[i][j] > F[i][k] +F[k][j])2                 {3tot=0;//error!!!! If we find a shorter one, it's clear.4F[I][J] = F[i][k] +F[k][j];5Q[i][j][++tot] =K;6G[I][J] =1;7                 }8                 Else if(F[i][j] = = F[i][k] +F[k][j])9                 {Tennow = g[i][k] *G[k][j]; One                     if(now)//Prevent 1 ~ 3 with 1 for relay this situation A                     { -G[I][J] + =Now ; -tot =0; the                     } -}

Each time a shorter path is found, it clears the queue and re-counts the shortest short line + put in the transit point + mark the shortest number of 1

If you find a path that is as long as the current shortest,

If it's not on your own, then I'll remove all the transit marks (why is that right?). Why don't you miss it? )

Well, it seems to be a little bit clear.

This time there will be two things:

1, there is a completely different shortest-circuit (that is, two of the shortest circuit without common points)

Obviously there is no key point, because two of the shortest circuit will not affect each other, broken one can go another,

So it can be deleted.

2, there is a different shortest path, but this short-circuit with the previous short-circuit has common use point

At this point, although there are key points, but we can still delete.

Why?

Here I think for a long time, in fact, this is:

Note that if this is the case, it must be similar to the diagram:

Where the red circle is clearly the key point,

At this point there are multiple shortest paths (we assume that all the routes in the graph are the shortest)

Then s---> t will not record any key,

But we can observe a wonderful nature!

That is, the presence of the key must be caused by the uniqueness of a path,

Example of s---> 2

If the path of s---> 2 is not unique, then 1 will not be a key point,

So what does this mean?

means that although s---> t are not counted to 1, but s---> 2 can be counted!

Because s---> 2 o'clock have no side to disturb them, this path is unique!

So we can still delete it, because it is enough to have one side to count this key point.

In doing this problem when I was caught in a misunderstanding, although said to be more mentally retarded, but still say it:

This is the path:

1---> 2---> 3---> 4

Why is the shortest-path count not counted to 2?

In fact, because K is placed in the outermost enumeration, so that the 2 for the transit time, 3 has not become a transit point,

Therefore 2---> 4 is not in fact, so at this time will not be counted to any shortest way, until k = = 3 o'clock will be counted to the shortest way.

1#include <bits/stdc++.h>2 using namespacestd;3 #defineR Register int4 #defineAC 2105 #defineINF 21390621436 #defineGetChar () *o++7 /*Although for a chain of time two endpoints may not count all the relays? 8 but the small ones will still be counted?*/9 Charread[5000100],*o=READ;Ten intn,m; One intF[ac][ac],g[ac][ac]; A intQ[ac][ac][ac],head[ac][ac]; - BOOLZ[ac]; -InlineintRead () the { -     intx=0;CharC=GetChar (); -      while(C >'9'|| C <'0') c=GetChar (); -      while(c >='0'&& C <='9') x=x*Ten+c-'0', c=GetChar (); +     returnx; -  + } A  atInlinevoidUpmin (int&a,intb) - { -     if(b < a) A =b; - } -  - voidPre () in { -     inta,b,c; toN=read (), m=read (); +Memset (F,127,sizeof(f)); -      for(R i=1; i<=n;i++) f[i][i]=0; the      for(R i=1; i<=m;i++) *     { $A=read (), B=read (), c=read ();Panax Notoginseng         if(C <F[a][b]) -         { theF[A][B] = F[b][a] =C; +G[A][B] = G[b][a] =1; A         } the     } + } -  $ #defineTot Head[i][j] $ voidWork () - { -     intNow ; the      for(R k=1; k<=n;k++) -          for(R i=1; i<=n;i++)Wuyi         { the             if(F[i][k] = = INF)Continue; -              for(R j=1; j<=n;j++) Wu             { -                 if(F[k][j] = = INF)Continue; About                 if(F[i][j] > F[i][k] +F[k][j]) $                 { -tot=0;//error!!!! If we find a shorter one, it's clear. -F[I][J] = F[i][k] +F[k][j]; -Q[i][j][++tot] =K; AG[I][J] =1; +                 } the                 Else if(F[i][j] = = F[i][k] +F[k][j]) -                 { $now = g[i][k] *G[k][j]; the                     if(now)//Prevent 1 ~ 3 with 1 for relay this situation the                     { theG[I][J] + =Now ; thetot =0; -                     } in                 } the             }             the         } About } the  the voidGetans () the { +     BOOLDone=false; -      for(R i=1; i<=n;i++) the          for(R j=1; j<=n;j++)Bayi         { the             if(TOT) the             { -                  for(R k=1; k<=tot;k++) -Z[q[i][j][k]] =true; theDone =true; the             } the         } the      for(R i=1; i<=n;i++)  -         if(Z[i]) printf ("%d", i); the     if(!done) printf ("No Important cities.\n"); the } the 94 intMain () the { the //freopen ("in.in", "R", stdin); theFread (READ,1,5000000, stdin);98 pre (); About Work (); - Getans ();101 //fclose (stdin);102     return 0;103}

 

Key--[jsoi2007] Important city Floyd: Shortest-circuit count

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.