The priority of each connected block is calculated by ignoring the route, finding all the connected blocks in the graph, and then using the route topological ordering.
And then Dijkstra, prioritize the high-priority blocks.
PS: This question SPFA will tle
#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib > #include <algorithm> #include <vector> #include <queue> #define N 25003#define M 150004using namespace Std;inline int read () {int Ret=0;char ch=getchar (); bool Flag=0;while (ch< ' 0 ' | | ch> ' 9 ') {flag=ch== '-'; ch= GetChar ();} while (' 0 ' <=ch && ch<= ' 9 ') {Ret=ret*10-48+ch;ch=getchar ();} return Flag?-ret:ret;} struct Edge{int Adj,next,len;edge () {}edge (int _adj,int _next,int _len): Adj (_adj), Next (_next), Len (_len) {}} e[m];int N, G[n],m,di[n];void addedge (int u,int v,int W) {E[++m]=edge (v,g[u],w); g[u]=m;} int col[n],cnt;int q[n],qh,qt;int a[n],start[n];int degree[n];int level[n];void bfs () {Qh=qt=0;memset (col,0,sizeof ( COL)); for (int i=1;i<=n;++i) if (!col[i]) {q[++qt]=i;col[i]=++cnt;start[cnt]=qt;while (QH<QT) {int u=q[++qh];for (int i=di[u];i;i=e[i].next) {int v=e[i].adj;if (col[v]) Continue;col[v]=cnt;q[++qt]=v;}}} start[cnt+1]=qt+1;for (int i=1;i<=qt;++i) A[i]=q[i];memset (degree,0,sizeof (degree)); for (int. j=1;j<=n;++j) for (int i=g[j];i!=di[j];i=e[i].next) {int v= E[I].ADJ;++DEGREE[COL[V]];} qh=qt=0;for (int i=1;i<=cnt;++i) if (!degree[i]) q[++qt]=i;int now=0;while (QH<QT) {int u=q[++qh];level[u]=++now; for (int j=start[u];j<start[u+1];++j) for (int i=g[a[j]];i!=di[a[j]];i=e[i].next) {int v=e[i].adj;if (!--degree[col [v]]) q[++qt]=col[v];}} struct Heapnode{int pos,value; Heapnode () {}heapnode (int _pos,int _value):p os (_pos), value (_value) {}};inline bool operator > (const heapnode &x, Const Heapnode &y) {return (level[col[x.pos]]>level[col[y.pos]]| | Level[col[x.pos]]==level[col[y.pos]]&&x.value>y.value);} priority_queue
bzoj2200: [Usaco2011 Jan] roads and routes