• 题意
    • 有n个点,np个供电点,nc个消费点,m条线路,接来题目先给出的是m条带权路径,然后是np个供电点和权值,接着就是nc个消费点和权值。
    • 求出给定的图最大能消费的总电量(就是求最大流)
  •  题解
    • 建一个超级源点,一个超级汇点。超级源点连接所以供电站,所以消费点连接超级汇点。跑一遍网络流即可。
    • 需要注意读入,用” %c”使得不读入空格
  • 代码
    • #include<algorithm>
      #include <iostream>
      #include   <fstream>
      #include  <cstdlib>
      #include  <cstring>
      #include  <cassert>
      #include   <cstdio>
      #include   <vector>
      #include   <string>
      #include    <cmath>
      #include    <queue>
      #include    <stack>
      #include      <set>
      #include      <map>
      using namespace std;
      #define P(a,b,c) make_pair(a,make_pair(b,c))
      #define rep(i,a,n) for (int i=a;i<=n;i++) #define per(i,a,n) for (int i=n;i>=a;i--)
      #define CLR(vis) memset(vis,0,sizeof(vis))
      #define MST(vis,pos) memset(vis,pos,sizeof(vis))
      #define pb push_back
      #define mp make_pair
      #define all(x) (x).begin(),(x).end()
      #define fi first
      #define se second
      #define SZ(x) ((int)(x).size())
      typedef pair<int,pair<int,int> >pii;
      typedef long long ll;
      typedef unsigned long long ull;
      const ll mod = 1000000007;
      ll gcd(ll a, ll b) {
      	return b ? gcd(b, a%b) : a;
      }
      template<class T>inline void gmax(T &A, T B) {
      	(A<B)&&(A=B);//if(B>A)A=B;
      }
      template<class T>inline void gmin(T &A, T B) {
      	(A>B)&&(A=B);//if(B<A)A=B;
      }
      template <class T>
      inline bool scan_d(T &ret) {
      	char c;
      	int sgn;
      	if(c=getchar(),c==EOF) return 0; //EOF
      	while(c!='-'&&(c<'0'||c>'9')) c=getchar();
      	sgn=(c=='-')?-1:1;
      	ret=(c=='-')?0:(c-'0');
      	while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0'); ret*=sgn; return 1; } inline void outt(int x) { if(x>9) outt(x/10);
      	putchar(x%10+'0');
      }
      const int inf=1<<29,N=50010,M=300010;
      int head[N],ver[M],edge[M],Next[M],d[N];
      int n,np,nc,m,s,t,tot,maxflow;
      queue<int>q;
      void add(int x,int y,int z) {
      	ver[++tot]=y;
      	edge[tot]=z;
      	Next[tot]=head[x];
      	head[x]=tot;
      	ver[++tot]=x;
      	edge[tot]=0;
      	Next[tot]=head[y];
      	head[y]=tot;
      }
      bool bfs() { //在残量网络上构造分层图
      	memset(d,0,sizeof(d));
      	while(q.size())q.pop();
      	q.push(s);
      	d[s]=1;
      	while(q.size()) {
      		int x=q.front();
      		q.pop();
      		for(int i=head[x]; i; i=Next[i])
      			if(edge[i]&&!d[ver[i]]) {
      				q.push(ver[i]);
      				d[ver[i]]=d[x]+1;
      				if(ver[i]==t)return 1;
      			}
      	}
      	return 0;
      }
      int dinic(int x,int flow) { //在当前分层图上增广
      	if(x==t)return flow;
      	int rest=flow,k;
      	for(int i=head[x]; i; i=Next[i])
      		if(edge[i]&&d[ver[i]]==d[x]+1) {
      			k=dinic(ver[i],min(rest,edge[i]));
      			if(!k)d[ver[i]]=0;
      			edge[i]-=k;
      			edge[i^1]+=k;
      			rest-=k;
      		}
      	return flow-rest;
      }
      
      void init() {
      	maxflow=0;
      	tot=1;
      	memset(head,0,sizeof(head));
      }
      int main() {
      	while(scanf("%d%d%d%d", &n, &np, &nc, &m)!=EOF) {
      		init();
      		int u,v,w;
      		char tmp;
      		s=0,t=n+1;
      		rep(i,1,m){
      			scanf(" %c%d%c%d%c%d", &tmp,&u,&tmp,&v,&tmp,&w);
      			++u,++v;
      			add(u,v,w);
      		}
      		rep(i,1,np){
      			scanf(" %c%d%c%d", &tmp,&u,&tmp,&w);
      			++u;
      			add(s,u,w);
      		}
      		rep(i,1,nc){
      			scanf(" %c%d %c%d", &tmp,&v,&tmp,&w);
      			++v;
      			add(v,t,w);
      		}
      		int flow=0;
      		while(bfs()){
      			while(flow=dinic(s,inf))maxflow+=flow;
      		}
      		printf("%d\n", maxflow);
      	}
      	return 0;
      }

发表评论

邮箱地址不会被公开。 必填项已用*标注