HDU - 4280 Island Transport(最大流)
生活随笔
收集整理的這篇文章主要介紹了
HDU - 4280 Island Transport(最大流)
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接:點(diǎn)擊查看
題目大意:給出n個(gè)島嶼,由m條無(wú)向邊連接而成,現(xiàn)在要從最西邊的島嶼到達(dá)最東邊的島嶼,問(wèn)最大流量為多少
題目分析:最大流模板題,只不過(guò)這個(gè)毒瘤題卡掉了dinic,只能去網(wǎng)上找了個(gè)SAP的模板,有一說(shuō)一,真的好用
代碼:
#include<iostream> #include<cstdlib> #include<string> #include<cstring> #include<cstdio> #include<algorithm> #include<climits> #include<cmath> #include<cctype> #include<stack> #include<queue> #include<list> #include<vector> #include<set> #include<map> #include<sstream> using namespace std;typedef long long LL;const int inf=0x3f3f3f3f;const int N=1e5+100;//點(diǎn)數(shù)的最大值const int M=2e5+100;//邊數(shù)的最大值struct Node {int from,to,next;int cap; }edge[M];int tol,head[N],dep[N],gap[N];//gap[x]=y :說(shuō)明殘留網(wǎng)絡(luò)中dep[i]==x的個(gè)數(shù)為yint n;//n是總的點(diǎn)的個(gè)數(shù),包括源點(diǎn)和匯點(diǎn)void init() {tol=0;memset(head,-1,sizeof(head)); }void addedge(int u,int v,int w) {edge[tol].from=u;edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];head[u]=tol++;edge[tol].from=v;edge[tol].to=u;edge[tol].cap=w;edge[tol].next=head[v];head[v]=tol++; } void BFS(int start,int end) {memset(dep,-1,sizeof(dep));memset(gap,0,sizeof(gap));gap[0]=1;int que[N];int front,rear;front=rear=0;dep[end]=0;que[rear++]=end;while(front!=rear){int u=que[front++];if(front==N)front=0;for(int i=head[u];i!=-1;i=edge[i].next){int v=edge[i].to;if(dep[v]!=-1)continue;que[rear++]=v;if(rear==N)rear=0;dep[v]=dep[u]+1;++gap[dep[v]];}} } int SAP(int start,int end) {int res=0;BFS(start,end);int cur[N];int S[N];int top=0;memcpy(cur,head,sizeof(head));int u=start;int i;while(dep[start]<n){if(u==end){int temp=inf;int inser;for(i=0;i<top;i++)if(temp>edge[S[i]].cap){temp=edge[S[i]].cap;inser=i;}for(i=0;i<top;i++){edge[S[i]].cap-=temp;edge[S[i]^1].cap+=temp;}res+=temp;top=inser;u=edge[S[top]].from;}if(u!=end&&gap[dep[u]-1]==0)//出現(xiàn)斷層,無(wú)增廣路break;for(i=cur[u];i!=-1;i=edge[i].next)if(edge[i].cap!=0&&dep[u]==dep[edge[i].to]+1)break;if(i!=-1){cur[u]=i;S[top++]=i;u=edge[i].to;}else{int min=n;for(i=head[u];i!=-1;i=edge[i].next){if(edge[i].cap==0)continue;if(min>dep[edge[i].to]){min=dep[edge[i].to];cur[u]=i;}}--gap[dep[u]];dep[u]=min+1;++gap[dep[u]];if(u!=start)u=edge[S[--top]].from;}}return res; }int main() { // freopen("input.txt","r",stdin); // ios::sync_with_stdio(false);int w;cin>>w;while(w--){init();int m,st,ed,mmin=inf,mmax=-inf;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++){int x,y;scanf("%d%d",&x,&y);if(x<mmin){mmin=x;st=i;}if(x>mmax){mmax=x;ed=i;}}while(m--){int u,v,w;scanf("%d%d%d",&u,&v,&w);addedge(u,v,w);}printf("%d\n",SAP(st,ed));}return 0; }?
總結(jié)
以上是生活随笔為你收集整理的HDU - 4280 Island Transport(最大流)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: (转)网络流-最大流 SAP算法(模板)
- 下一篇: HDU - 4292 Food(最大流+