bzoj2200: [Usaco2011 Jan] roads and routes

Source: Internet
Author: User

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&LT;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&LT;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

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.