hdu4280 最大流模板题:邻接表形式dinic
额这题卡了dinic的时间==
不过还是强行用dinic过了,把stl语句全部换了,包括vector也改成了邻接表
还是比较慢,8000+ms,很奇怪的是,我的邻接表中加了当前弧优化竟然慢了1s===
至于sap,过两天再写吧==
1 #pragma comment(linker,"/STACK:16777216") 2 #include<stdio.h> 3 #include<string.h> 4 #define maxn 100005 5 #define maxm 200005 6 int minn(int x,int y) 7 { 8 return x<y?x:y; 9 } 10 struct Edge{ 11 int to,flow,Next; 12 }; 13 struct dinic{ 14 int s,t,n,now; 15 Edge edges[maxm]; 16 bool vis[maxn]; 17 int d[maxn],head[maxn],q[maxn]; 18 void init(int _s,int _t,int _n){ 19 s=_s; t=_t; n=_n; 20 memset(head,-1,sizeof(head)); 21 now=0; 22 } 23 void add(int from,int to,int flow){ 24 edges[now].to=to; edges[now].flow=flow; 25 edges[now].Next=head[from]; head[from]=now++; 26 27 edges[now].to=from; edges[now].flow=flow; 28 edges[now].Next=head[to]; head[to]=now++; 29 } 30 bool bfs(){ 31 memset(vis,0,sizeof(vis)); 32 int front=0,rear=0; 33 q[rear++]=s; 34 d[s]=0; vis[s]=1; 35 while (front<rear){ 36 int x=q[front++]; 37 for (int i=head[x];i!=-1;i=edges[i].Next){ 38 if (!vis[edges[i].to]&&edges[i].flow>0){ 39 vis[edges[i].to]=1; 40 d[edges[i].to]=d[x]+1; 41 q[rear++]=edges[i].to; 42 } 43 } 44 } 45 return vis[t]; 46 } 47 int dfs(int x,int a){ 48 if (x==t||a==0) return a; 49 int flow=0,f; 50 for (int i=head[x];i!=-1;i=edges[i].Next){ 51 if (d[x]+1==d[edges[i].to]&&edges[i].flow>0){ 52 int f=dfs(edges[i].to,minn(a-flow,edges[i].flow)); 53 edges[i].flow-=f; 54 edges[i^1].flow+=f; 55 flow+=f; 56 if (flow==a) return flow; 57 } 58 } 59 if (flow==0) d[x]=0; 60 return flow; 61 } 62 int maxflow(){ 63 int flow=0; 64 while (bfs()) 65 flow+=dfs(s,0x3f3f3f3f); 66 return flow; 67 } 68 }g; 69 int main() 70 { 71 int T,n,m,maxx,minx,x,y,w,i,east,west; 72 scanf("%d",&T); 73 while (T--){ 74 scanf("%d%d",&n,&m); 75 maxx=-0x3f3f3f3f; minx=0x3f3f3f3f; 76 for (i=1;i<=n;i++){ 77 scanf("%d%d",&x,&y); 78 if (x>maxx){ 79 east=i; 80 maxx=x; 81 } 82 if (x<minx){ 83 west=i; 84 minx=x; 85 } 86 } 87 g.init(west,east,n); 88 for (i=1;i<=m;i++){ 89 scanf("%d%d%d",&x,&y,&w); 90 g.add(x,y,w); 91 } 92 printf("%d\n",g.maxflow()); 93 } 94 return 0; 95 }
其实本来就是想回忆一下大白书上的dinic的,结果超时改到现在尼玛==
主要是这几个stl太慢,这是TLE的版本
1 #pragma comment(linker,"/STACK:16777216") 2 #include<stdio.h> 3 #include<string.h> 4 #include<queue> 5 #include<vector> 6 #include<algorithm> 7 #define maxn 100005 8 #define maxm 200005 9 using namespace std; 10 int minn(int x,int y) 11 { 12 return x<y?x:y; 13 } 14 struct Edge{ 15 int from,to,cap,flow; 16 }; 17 struct dinic{ 18 int s,t,n; 19 vector<Edge>edges; 20 vector<int>G[maxn]; 21 bool vis[maxn]; 22 int cur[maxn],d[maxn]; 23 void init(int _s,int _t,int _n){ 24 s=_s; t=_t; n=_n; 25 edges.clear(); 26 for (int i=0;i<=_n;i++) G[i].clear(); 27 } 28 void add(int from,int to,int cap){ 29 Edge e; 30 e.from=from; e.to=to; e.cap=cap; e.flow=0; 31 edges.push_back(e); 32 e.from=to; e.to=from; e.cap=cap; e.flow=0; 33 edges.push_back(e); 34 int m=edges.size(); 35 G[from].push_back(m-2); G[to].push_back(m-1); 36 } 37 bool bfs(){ 38 memset(vis,0,sizeof(vis)); 39 queue<int>q; 40 q.push(s); d[s]=0; vis[s]=1; 41 while (!q.empty()){ 42 int x=q.front(); q.pop(); 43 for (int i=0;i<G[x].size();i++){ 44 Edge& e=edges[G[x][i]]; 45 if (!vis[e.to]&&e.cap>e.flow){ 46 vis[e.to]=1; 47 d[e.to]=d[x]+1; 48 q.push(e.to); 49 } 50 } 51 } 52 return vis[t]; 53 } 54 int dfs(int x,int a){ 55 if (x==t||a==0) return a; 56 int flow=0,f; 57 for (int& i=cur[x];i<G[x].size();i++){ 58 Edge& e=edges[G[x][i]]; 59 if (d[x]+1==d[e.to]&&(f=dfs(e.to,minn(a,e.cap-e.flow)))>0){ 60 e.flow+=f; 61 edges[G[x][i]^1].flow-=f; 62 flow+=f; 63 a-=f; 64 if (a==0) break; 65 } 66 } 67 return flow; 68 } 69 int maxflow(){ 70 int flow=0; 71 while (bfs()){ 72 memset(cur,0,sizeof(cur)); 73 flow+=dfs(s,0x3f3f3f3f); 74 } 75 return flow; 76 } 77 }g; 78 int main() 79 { 80 int T,n,m,maxx,minx,x,y,w,i,east,west; 81 scanf("%d",&T); 82 while (T--){ 83 scanf("%d%d",&n,&m); 84 maxx=-0x3f3f3f3f; minx=0x3f3f3f3f; 85 for (i=1;i<=n;i++){ 86 scanf("%d%d",&x,&y); 87 if (x>maxx){ 88 east=i; 89 maxx=x; 90 } 91 if (x<minx){ 92 west=i; 93 minx=x; 94 } 95 } 96 g.init(west,east,n); 97 for (i=1;i<=m;i++){ 98 scanf("%d%d%d",&x,&y,&w); 99 g.add(x,y,w); 100 } 101 printf("%d\n",g.maxflow()); 102 } 103 return 0; 104 }