1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
| #include<bits/stdc++.h> #define re register typedef long long ll; typedef unsigned long long ull; template<class T> inline void read(T &x) { x=0; char ch=getchar(),t=0; while(ch<'0'||ch>'9') t|=ch=='-',ch=getchar(); while(ch>='0'&&ch<='9') x=(x<<3)+(x<<1)+(ch^48),ch=getchar(); x*=t?-1:1; } template<class T,class ...Arc> inline void read(T &x,Arc &...arc){ read(x),read(arc...); } template<class T> inline void write(T x) { if(x<0) putchar('-'),x=-x; if(x>9) write(x/10); putchar(x%10+48); } inline void write(char c){ putchar(c); } inline void write(bool x){ putchar(x?'1':'0'); } inline void write(char *s){ while(*s!='\0') putchar(*s++); } inline void write(const char *s){ while(*s!='\0') putchar(*s++); } template<class T,class ...Arc> inline void write(T x,Arc ...arc){ write(x),write(arc...); } template<class T> inline bool checkMax(T &x,T y){ return x<y?x=y,1:0; } template<class T> inline bool checkMin(T &x,T y){ return x>y?x=y,1:0; } const int MAXN=1e3+10,MAXC=12,MAXV=3e3+10,MAXE=1e6+10; const int Inf=0x3f3f3f3f; const ll INF=0x3f3f3f3f3f3f3f3f; int N,S,T; struct Net { int next,to; ll val,cost; }Edge[MAXE<<1]; int Head[MAXV],Cur[MAXV],Total=1; inline void addEdge(int u,int v,int w,int c) { Edge[++Total]=(Net){Head[u],v,w,c};Head[u]=Total; Edge[++Total]=(Net){Head[v],u,0,-c};Head[v]=Total; } ll Dist[MAXV],ret; bool Vis[MAXV]; inline bool Spfa() { std::memset(Dist,0x3f,sizeof(Dist)), std::memset(Vis,0,sizeof(Vis)); std::memcpy(Cur,Head,sizeof(Head)); std::queue<int>Q;Q.push(S); Dist[S]=0; while(!Q.empty()) { int u=Q.front();Q.pop(); Vis[u]=0; for(int e=Head[u],v;e;e=Edge[e].next) { v=Edge[e].to; if(Edge[e].val&&checkMin(Dist[v],Dist[u]+Edge[e].cost)) if(!Vis[v]) Q.push(v),Vis[v]=1; } } return Dist[T]!=INF; } ll Dfs(int x,ll inf) { if(x==T) return inf; ll flow=0;Vis[x]=1; for(int e=Cur[x],v;e&&flow<inf;e=Edge[e].next) { v=Edge[e].to;Cur[x]=e; if(!Vis[v]&&Edge[e].val&&Dist[v]==Dist[x]+Edge[e].cost) { ll k=Dfs(v,std::min(Edge[e].val,inf-flow)); if(k) { Edge[e].val-=k,Edge[e^1].val+=k,flow+=k; ret+=k*Edge[e].cost; } } } Vis[x]=0; return flow; } inline ll SSP() { ll r=0,flow; while(Spfa()) while((flow=Dfs(S,INF))) r+=flow; return r; } int main() { freopen("offsheet.in","r",stdin); freopen("offsheet.out","w",stdout); read(N); S=0,T=N*3+4; for(int i=1,x,y,c;i<=N;++i) { read(x,y,c); addEdge(S,i,c,0); addEdge(i,N*2+1,Inf,x+y),addEdge(i,N*2+2,Inf,x-y), addEdge(i,N*2+3,Inf,-x+y),addEdge(i,N*2+4,Inf,-x-y); } for(int i=1,x,y,c;i<=N;++i) { read(x,y,c); addEdge(i+N,T,c,0); addEdge(N*2+1,i+N,Inf,-x-y),addEdge(N*2+2,i+N,Inf,-x+y); addEdge(N*2+3,i+N,Inf,x-y),addEdge(N*2+4,i+N,Inf,x+y); } SSP(); write(-ret); return 0; }
|