/* 忽然大悟,补上第三题解题报告,膜拜给程序的C++神牛 解题步骤: (1).找环,把它压成点,不要问我为什么,压成点后不要问我买入卖出如何处理 (2).反复执行(1),直到不满足条件 (3).此时我们得到一个Dag(有向无环图) (4).SPFA (5).AC. */ #include #define NMax 50000 #define MMax 100000 using namespace std; struct edge{ int e; char c; edge *next; }epool[MMax<<1],*etop; edge *E[NMax]; int N,price[NMax]; char vi[NMax],vi2[NMax]; int stack[NMax]; void dfs1(){ int tp,x; vi[stack[0]=0]=1;tp=1; while (tp){x=stack[--tp]; for (edge *p=E[x];p;p=p->next)if (p->c!=0 && vi[p->e]==0) vi[stack[tp++]=p->e]=1; } } void dfs2(){ int tp,x; vi2[stack[0]=N-1]=1;tp=1; while (tp){x=stack[--tp]; for (edge *p=E[x];p;p=p->next)if (p->c!=1 && vi2[p->e]==0) vi2[stack[tp++]=p->e]=1; } } int best[NMax]; void dfs3(int u){ int tp,x; if (best[u]next)if (p->c!=1 && vi[p->e] && vi2[p->e] && best[p->e]e]=price[u]; } } int rk[NMax]; void qs(int b,int e){int j,t; if (b>=e)return; t=rk[b];rk[b]=rk[(b+e)>>1];rk[(b+e)>>1]=t; for (int i=j=b+1;i<=e;i++)if (price[rk[i]]>price[rk[b]] || price[rk[i]]==price[rk[b]] && rk[i]e=y;etop->next=E[x];E[x]=etop;etop->c=1;etop++; etop->e=x;etop->next=E[y];E[y]=etop;etop->c=0;etop++; }else{ etop->e=y;etop->next=E[x];E[x]=etop;etop->c=2;etop++; etop->e=x;etop->next=E[y];E[y]=etop;etop->c=2;etop++; } } for (int i=0;iret)ret=best[i]-price[i]; fprintf(fout,"%d\n",ret); fclose(fin);fclose(fout); return 0; }