BZOJ3931 [CQOI2015]网络吞吐量(最大流)

没啥好说的,有写过类似的,就是预处理出最短路上的边建容量网络。

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<queue>
  4 #include<algorithm>
  5 using namespace std;
  6 #define INF (1LL<<60)
  7 #define MAXN 1111
  8 #define MAXM 1111*1111*2
  9 
 10 struct Edge{
 11     int v,next;
 12     long long cap,flow;
 13 }edge[MAXM];
 14 int vs,vt,NE,NV;
 15 int head[MAXN];
 16 
 17 void addEdge(int u,int v,long long cap){
 18     edge[NE].v=v; edge[NE].cap=cap; edge[NE].flow=0;
 19     edge[NE].next=head[u]; head[u]=NE++;
 20     edge[NE].v=u; edge[NE].cap=0; edge[NE].flow=0;
 21     edge[NE].next=head[v]; head[v]=NE++;
 22 }
 23 
 24 int level[MAXN];
 25 int gap[MAXN];
 26 void bfs(){
 27     memset(level,-1,sizeof(level));
 28     memset(gap,0,sizeof(gap));
 29     level[vt]=0;
 30     gap[level[vt]]++;
 31     queue<int> que;
 32     que.push(vt);
 33     while(!que.empty()){
 34         int u=que.front(); que.pop();
 35         for(int i=head[u]; i!=-1; i=edge[i].next){
 36             int v=edge[i].v;
 37             if(level[v]!=-1) continue;
 38             level[v]=level[u]+1;
 39             gap[level[v]]++;
 40             que.push(v);
 41         }
 42     }
 43 }
 44 
 45 int pre[MAXN];
 46 int cur[MAXN];
 47 long long ISAP(){
 48     bfs();
 49     memset(pre,-1,sizeof(pre));
 50     memcpy(cur,head,sizeof(head));
 51     int u=pre[vs]=vs;
 52     long long flow=0,aug=INF;
 53     gap[0]=NV;
 54     while(level[vs]<NV){
 55         bool flag=false;
 56         for(int &i=cur[u]; i!=-1; i=edge[i].next){
 57             int v=edge[i].v;
 58             if(edge[i].cap!=edge[i].flow && level[u]==level[v]+1){
 59                 flag=true;
 60                 pre[v]=u;
 61                 u=v;
 62                 //aug=(aug==-1?edge[i].cap:min(aug,edge[i].cap));
 63                 aug=min(aug,edge[i].cap-edge[i].flow);
 64                 if(v==vt){
 65                     flow+=aug;
 66                     for(u=pre[v]; v!=vs; v=u,u=pre[u]){
 67                         edge[cur[u]].flow+=aug;
 68                         edge[cur[u]^1].flow-=aug;
 69                     }
 70                     //aug=-1;
 71                     aug=INF;
 72                 }
 73                 break;
 74             }
 75         }
 76         if(flag) continue;
 77         int minlevel=NV;
 78         for(int i=head[u]; i!=-1; i=edge[i].next){
 79             int v=edge[i].v;
 80             if(edge[i].cap!=edge[i].flow && level[v]<minlevel){
 81                 minlevel=level[v];
 82                 cur[u]=i;
 83             }
 84         }
 85         if(--gap[level[u]]==0) break;
 86         level[u]=minlevel+1;
 87         gap[level[u]]++;
 88         u=pre[u];
 89     }
 90     return flow;
 91 }
 92 int n;
 93 long long G[555][555],d[555];
 94 bool vis[555];
 95 void SPFA(){
 96     for(int i=1; i<=n; ++i) d[i]=INF,vis[i]=0;
 97     d[1]=0; vis[1]=1;
 98     queue<int> que;
 99     que.push(1);
100     while(!que.empty()){
101         int u=que.front(); que.pop();
102         for(int v=1; v<=n; ++v){
103             if(G[u][v]==-1) continue;
104             if(d[v]>d[u]+G[u][v]){
105                 d[v]=d[u]+G[u][v];
106                 if(!vis[v]){
107                     vis[v]=1;
108                     que.push(v);
109                 }
110             }
111         }
112         vis[u]=0;
113     }
114 }
115 int main(){
116     memset(G,-1,sizeof(G));
117     int m,a,b,c;
118     scanf("%d%d",&n,&m);
119     while(m--){
120         scanf("%d%d%d",&a,&b,&c);
121         if(G[a][b]==-1 || G[a][b]>c) G[b][a]=G[a][b]=c;
122     }
123     for(int i=1; i<=n; ++i) G[i][i]=0;
124     SPFA();
125     vs=1; vt=n+n; NV=vt+1; NE=0;
126     memset(head,-1,sizeof(head));
127     for(int i=1; i<=n; ++i){
128         scanf("%d",&a);
129         if(i==1 || i==n) addEdge(i,i+n,INF);
130         else addEdge(i,i+n,a);
131     }
132     for(int i=1; i<=n; ++i){
133         for(int j=1; j<=n; ++j){
134             if(G[i][j]==-1 || i==j || d[i]==INF || d[j]==INF) continue;
135             if(d[i]+G[i][j]==d[j]) addEdge(i+n,j,INF);
136         }
137     }
138     printf("%lld",ISAP());
139     return 0;
140 }

 

posted @ 2016-03-28 23:05  WABoss  阅读(255)  评论(0编辑  收藏  举报